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
|
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");
|