aboutsummaryrefslogtreecommitdiff
path: root/services/docker/mail-server/relay/config.ts
blob: ec7e7d480f01aaee07ca051d2b87fe370db8a314 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { transformProperties } from "./util.ts";

export const APP_PREFIX = "crupest";
export const APP_NAME = "mailserver";

interface ConfigItemDef {
  env: string;
  description: string;
  default?: string;
  secret?: boolean;
}

export const CONFIG_DEFS = {
  mailDomain: {
    env: "MAIL_DOMAIN",
    description: "the part after `@` of an address",
  },
  dataPath: {
    env: "DATA_PATH",
    description: "path to save app persistent data",
  },
  ldaPath: {
    env: "LDA_PATH",
    description: "full path of lda executable",
    "default": "/dovecot/libexec/dovecot/dovecot-lda",
  },
  inboundFallback: {
    env: "INBOUND_FALLBACK",
    description: "comma separated addresses used as fallback receipts",
    "default": "",
  },
  awsInboundPath: {
    env: "AWS_INBOUND_PATH",
    description: "(random set) path for aws sns",
  },
  awsInboundKey: {
    env: "AWS_INBOUND_KEY",
    description: "(random set) http header Authorization for aws sns",
  },
  awsRegion: { env: "AWS_REGION", description: "aws region" },
  awsAccessKeyId: { env: "AWS_USER", description: "aws access key id" },
  awsSecretAccessKey: {
    env: "AWS_PASSWORD",
    description: "aws secret access key",
    secret: true,
  },
  awsMailBucket: {
    env: "AWS_MAIL_BUCKET",
    description: "aws s3 bucket saving raw mails",
    secret: true,
  },
} as const satisfies Record<string, ConfigItemDef>;

type ConfigDefs = typeof CONFIG_DEFS;
type ConfigKey = keyof ConfigDefs;
type ConfigMap = {
  [K in ConfigKey]: ConfigItemDef & { value: string };
};

function getFullEnvKey(key: string): string {
  return `${APP_PREFIX.toUpperCase()}_${APP_NAME.toUpperCase()}_${key}`;
}

function resolveAppConfigItem(def: ConfigItemDef): string {
  const envKey = getFullEnvKey(def.env);
  const value = Deno.env.get(envKey);
  if (value != null) return value;
  if (def.default != null) return def.default;
  throw new Error(
    `Required env ${envKey} (${def.description}) is not set.`,
  );
}

export class Config {
  #config = transformProperties(
    CONFIG_DEFS,
    (def) => ({ ...def, value: resolveAppConfigItem(def) }),
  ) as ConfigMap;

  readonly HTTP_HOST = "0.0.0.0";
  readonly HTTP_PORT = 2345;
  readonly SMTP_HOST = "127.0.0.1";
  readonly SMTP_PORT = 2346;

  get<K extends ConfigKey>(key: K): ConfigMap[K] {
    return this.#config[key];
  }

  getValue(key: ConfigKey): string {
    return this.get(key).value;
  }

  getValueList(key: ConfigKey, separator: string = ","): string[] {
    const value = this.getValue(key);
    if (value.length === 0) return [];
    return value.split(separator);
  }

  [Symbol.for("Deno.customInspect")]() {
    return Object.entries(this.#config).map(([key, item]) =>
      `${key} [env: ${getFullEnvKey(item.env)}]: ${
        item.secret === true ? "***" : item.value
      }`
    ).join("\n");
  }
}

const config = new Config();
export default config;
export const getConfigValue = config.getValue.bind(config);