diff options
Diffstat (limited to 'services/docker/mail-server/aws-sendmail/config.ts')
-rw-r--r-- | services/docker/mail-server/aws-sendmail/config.ts | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/services/docker/mail-server/aws-sendmail/config.ts b/services/docker/mail-server/aws-sendmail/config.ts new file mode 100644 index 0000000..0212029 --- /dev/null +++ b/services/docker/mail-server/aws-sendmail/config.ts @@ -0,0 +1,56 @@ +import { createSingleton, transformProperties } from "./util.ts"; + +export const APP_PREFIX = "crupest"; +export const APP_NAME = "mailserver"; + +interface ConfigItemDef { + env: string; + description: string; + default?: string; +} + +export const CONFIG_DEFS = { + awsAccessKeyId: { env: "AWS_USER", description: "aws access key id" }, + awsSecretAccessKey: { + env: "AWS_PASSWORD", + description: "aws secret access key", + }, + awsMailBucket: { + env: "AWS_MAIL_BUCKET", + description: "aws s3 bucket saving raw mails", + }, +} as const satisfies Record<string, ConfigItemDef>; + +type ConfigDefs = typeof CONFIG_DEFS; +type ConfigKey = keyof ConfigDefs; +type ConfigMap = { + [K in ConfigKey]: ConfigDefs[K] & { value: string }; +}; + +function resolveAppConfigItem(def: ConfigItemDef): string { + const envKey = + `${APP_PREFIX.toUpperCase()}_${APP_NAME.toUpperCase()}_${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 { + private config = transformProperties( + CONFIG_DEFS, + (def) => ({ ...def, value: resolveAppConfigItem(def) }), + ) as ConfigMap; + + get<K extends ConfigKey>(key: K): ConfigMap[K] { + return this.config[key]; + } + + getValue(key: ConfigKey): string { + return this.get(key).value; + } +} + +export const [getConfig, setConfig] = createSingleton<Config>("Config"); |