-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.ts
70 lines (56 loc) · 1.99 KB
/
config.ts
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
import { readFile } from 'fs/promises';
export interface IHostConfig {
'record_type'?: string,
'record_ttl'?: number,
'shared_secret': string,
'dns_provider': string
}
export interface IConfig {
'auth_backends': {
'predefined': {
'users': Record<string, string>
}
},
'update_endpoint': string,
'hosts': Record<string, IHostConfig>,
'dns_providers': Record<string, any>
}
export class Loader {
private static config: IConfig;
static async load(): Promise<IConfig> {
if (!Loader.config) {
const json:Buffer = await readFile(process.env.CONFIG_FILE || './config.json');
const config = JSON.parse(json.toString());
Loader.validate(config);
Loader.config = config;
}
return Loader.config;
}
static validate(config: IConfig) {
let provider: string, host: IHostConfig;
if (!config.dns_providers) {
config.dns_providers = {};
}
if (!config.dns_providers.default) {
config.dns_providers.default = {
dns_provider: 'route53'
};
}
for (let provider in config.dns_providers) {
let providerConfig = config.dns_providers[provider];
if (!('dns_provider' in providerConfig)) {
throw new Error(`Expected key dns_providers.${provider}.dns_provider not found in config.json`);
}
if (!['cloudflare', 'route53'].includes(providerConfig.dns_provider)) {
throw new Error(`Unsupported dns provider ${providerConfig.dns_provider}`);
}
}
for (const hostname in config.hosts) {
host = config.hosts[hostname];
host.dns_provider = host.dns_provider || 'default';
if (!(host.dns_provider in config.dns_providers)) {
throw new Error(`Expected key dns_providers.${host.dns_provider} not found in config.json`);
}
}
}
}