-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathconfig.ts
More file actions
286 lines (246 loc) · 9.71 KB
/
config.ts
File metadata and controls
286 lines (246 loc) · 9.71 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import './utils/dotenv-config.js';
import dotenv from 'dotenv';
import { bool, cleanEnv, makeValidator, num, str, url } from 'envalid';
export class Config {
private static _websocketPort?: number;
private static _httpPort?: number;
private static _httpBackendUrl?: string;
private static _httpFrontendUrl?: string;
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;
private static _authUrl?: string;
private static _authClientId?: string;
private static _authClientSecret?: string;
private static _authUserRegistrationAdapter?: string;
private static _authSelfServiceUrl?: string;
public static get websocketPort(): number {
this.throwIfNotInitialized();
return this._websocketPort!;
}
public static get httpPort(): number {
this.throwIfNotInitialized();
return this._httpPort!;
}
public static get httpBackendUrl(): string {
this.throwIfNotInitialized();
return this._httpBackendUrl!;
}
public static get httpFrontendUrl(): string {
this.throwIfNotInitialized();
return this._httpFrontendUrl!;
}
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!;
}
public static get authUrl(): string {
this.throwIfNotInitialized();
return this._authUrl!;
}
public static get authClientId(): string {
this.throwIfNotInitialized();
return this._authClientId!;
}
public static get authClientSecret(): string {
this.throwIfNotInitialized();
return this._authClientSecret!;
}
public static get authUserRegistrationAdapter(): string {
this.throwIfNotInitialized();
return this._authUserRegistrationAdapter!;
}
public static get authSelfServiceUrl(): string {
this.throwIfNotInitialized();
return this._authSelfServiceUrl!;
}
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_HTTP_BACKEND_URL: url({ default: 'http://localhost:3201' }),
DFM_HTTP_BACKEND_URL_TESTING: url({
default: 'http://localhost:13201',
}),
DFM_HTTP_FRONTEND_URL: url({ default: 'http://localhost:4200' }),
DFM_HTTP_FRONTEND_URL_TESTING: url({
default: 'http://localhost:14200',
}),
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: undefined }),
DFM_DB_PORT: tcpPortValidator({ default: 5432 }),
DFM_DB_PORT_TESTING: tcpPortValidator({ default: undefined }),
DFM_AUTH_URL: url({ default: 'http://127.0.0.1:9091/' }),
DFM_AUTH_URL_TESTING: url({ default: 'http://127.0.0.1:9091/' }),
DFM_AUTH_CLIENT_ID: str({ default: 'dfm-backend' }),
DFM_AUTH_CLIENT_ID_TESTING: str({ default: 'dfm-backend' }),
DFM_AUTH_CLIENT_SECRET: str({ default: '' }),
DFM_AUTH_CLIENT_SECRET_TESTING: str({ default: '' }),
DFM_AUTH_USER_REGISTRATION_ADAPTER: str({
default: '',
choices: ['', 'keycloak'],
}),
DFM_AUTH_USER_REGISTRATION_ADAPTER_TESTING: str({
default: '',
choices: ['', 'keycloak'],
}),
DFM_AUTH_SELF_SERVICE_URL: url({ default: '' }),
DFM_AUTH_SELF_SERVICE_URL_TESTING: url({ default: '' }),
});
}
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._httpBackendUrl =
testing && env.DFM_HTTP_BACKEND_URL_TESTING
? env.DFM_HTTP_BACKEND_URL_TESTING
: env.DFM_HTTP_BACKEND_URL;
this._httpFrontendUrl =
testing && env.DFM_HTTP_FRONTEND_URL_TESTING
? env.DFM_HTTP_FRONTEND_URL_TESTING
: env.DFM_HTTP_FRONTEND_URL;
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_TESTING
: env.DFM_DB_HOST;
this._dbPort =
testing && env.DFM_DB_PORT_TESTING
? env.DFM_DB_PORT_TESTING
: env.DFM_DB_PORT;
this._authUrl =
testing && env.DFM_AUTH_URL_TESTING
? env.DFM_AUTH_URL_TESTING
: env.DFM_AUTH_URL;
this._authClientId =
testing && env.DFM_AUTH_CLIENT_ID_TESTING
? env.DFM_AUTH_CLIENT_ID_TESTING
: env.DFM_AUTH_CLIENT_ID;
this._authClientSecret =
testing && env.DFM_AUTH_CLIENT_SECRET_TESTING
? env.DFM_AUTH_CLIENT_SECRET_TESTING
: env.DFM_AUTH_CLIENT_SECRET;
this._authUserRegistrationAdapter =
testing && env.DFM_AUTH_USER_REGISTRATION_ADAPTER_TESTING
? env.DFM_AUTH_USER_REGISTRATION_ADAPTER_TESTING
: env.DFM_AUTH_USER_REGISTRATION_ADAPTER;
this._authSelfServiceUrl =
testing && env.DFM_AUTH_SELF_SERVICE_URL_TESTING
? env.DFM_AUTH_SELF_SERVICE_URL_TESTING
: env.DFM_AUTH_SELF_SERVICE_URL;
this.isInitialized = true;
}
}