-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathconfig.ts
179 lines (153 loc) · 5.84 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import './utils/dotenv-config';
import dotenv from 'dotenv';
import { bool, cleanEnv, makeValidator, num, str } from 'envalid';
export class Config {
private static _websocketPort?: number;
private static _httpPort?: number;
private static _uploadLimit?: number;
private static _useDb?: boolean;
private static _dbUser?: string;
private static _dbPassword?: string;
private static _dbName?: string;
private static _dbLogging?: boolean;
private static _dbHost?: string;
private static _dbPort?: number;
public static get websocketPort(): number {
this.throwIfNotInitialized();
return this._websocketPort!;
}
public static get httpPort(): number {
this.throwIfNotInitialized();
return this._httpPort!;
}
public static get uploadLimit(): number {
this.throwIfNotInitialized();
return this._uploadLimit!;
}
public static get useDb(): boolean {
this.throwIfNotInitialized();
return this._useDb!;
}
public static get dbUser(): string {
this.throwIfNotInitialized();
return this._dbUser!;
}
public static get dbPassword(): string {
this.throwIfNotInitialized();
return this._dbPassword!;
}
public static get dbName(): string {
this.throwIfNotInitialized();
return this._dbName!;
}
public static get dbLogging(): boolean {
this.throwIfNotInitialized();
return this._dbLogging!;
}
public static get dbHost(): string {
this.throwIfNotInitialized();
return this._dbHost!;
}
public static get dbPort(): number {
this.throwIfNotInitialized();
return this._dbPort!;
}
private static createTCPPortValidator() {
return makeValidator((x) => {
const int = Number.parseInt(x);
if (!Number.isInteger(int) || !(int >= 0 && int < 2 ** 16)) {
throw new TypeError('Expected a valid port number');
}
return int;
});
}
// This uses the condition that is also used by envalid (see https://github.com/af/envalid/blob/main/src/validators.ts#L39)
private static isTrue(input: string | undefined): boolean {
const lowercase = input?.toLowerCase();
return lowercase === 'true' || lowercase === 't' || lowercase === '1';
}
private static parseVariables() {
const tcpPortValidator = this.createTCPPortValidator();
return cleanEnv(process.env, {
DFM_WEBSOCKET_PORT: tcpPortValidator({ default: 3200 }),
DFM_WEBSOCKET_PORT_TESTING: tcpPortValidator({ default: 13200 }),
DFM_HTTP_PORT: tcpPortValidator({ default: 3201 }),
DFM_HTTP_PORT_TESTING: tcpPortValidator({ default: 13201 }),
DFM_UPLOAD_LIMIT: num({ default: 200 }),
DFM_USE_DB: bool(),
DFM_USE_DB_TESTING: bool({ default: undefined }),
DFM_DB_USER: str(
// Require this variable only when the database should be used.
this.isTrue(process.env['DFM_USE_DB'])
? {}
: { default: undefined }
),
DFM_DB_USER_TESTING: str({ default: undefined }),
DFM_DB_PASSWORD: str(
// Require this variable only when the database should be used.
this.isTrue(process.env['DFM_USE_DB'])
? {}
: { default: undefined }
),
DFM_DB_PASSWORD_TESTING: str({ default: undefined }),
DFM_DB_NAME: str(
// Require this variable only when the database should be used.
this.isTrue(process.env['DFM_USE_DB'])
? {}
: { default: undefined }
),
DFM_DB_NAME_TESTING: str({ default: undefined }),
DFM_DB_LOG: bool({ default: false }),
DFM_DB_LOG_TESTING: bool({ default: undefined }),
DFM_DB_HOST: str({ default: '127.0.0.1' }),
DFM_DB_HOST_TESTING: str({ default: '127.0.0.1' }),
DFM_DB_PORT: tcpPortValidator({ default: 5432 }),
DFM_DB_PORT_TESTING: tcpPortValidator({ default: 5432 }),
});
}
private static isInitialized = false;
private static throwIfNotInitialized() {
if (!this.isInitialized)
throw new Error('Config was used uninitialized');
}
public static initialize(
testing: boolean = false,
forceRefresh: boolean = false
) {
if (this.isInitialized && !forceRefresh) {
return;
}
dotenv.config();
const env = this.parseVariables();
this._websocketPort = testing
? env.DFM_WEBSOCKET_PORT_TESTING
: env.DFM_WEBSOCKET_PORT;
this._httpPort = testing
? env.DFM_HTTP_PORT_TESTING
: env.DFM_HTTP_PORT;
this._uploadLimit = env.DFM_UPLOAD_LIMIT;
this._useDb =
testing && env.DFM_USE_DB_TESTING
? env.DFM_USE_DB_TESTING
: env.DFM_USE_DB;
this._dbUser =
testing && env.DFM_DB_USER_TESTING
? env.DFM_DB_USER_TESTING
: env.DFM_DB_USER;
this._dbPassword =
testing && env.DFM_DB_PASSWORD_TESTING
? env.DFM_DB_PASSWORD_TESTING
: env.DFM_DB_PASSWORD;
this._dbName =
testing && env.DFM_DB_NAME_TESTING
? env.DFM_DB_NAME_TESTING
: env.DFM_DB_NAME;
this._dbLogging =
testing && env.DFM_DB_LOG_TESTING
? env.DFM_DB_LOG_TESTING
: env.DFM_DB_LOG;
this._dbHost = testing ? env.DFM_DB_HOST_TESTING : env.DFM_DB_HOST;
this._dbPort = testing ? env.DFM_DB_PORT_TESTING : env.DFM_DB_PORT;
this.isInitialized = true;
}
}