-
Notifications
You must be signed in to change notification settings - Fork 14.1k
/
config.ts
147 lines (137 loc) · 4.5 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
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { merge as _merge } from 'lodash';
export interface RedisConfig {
port: number;
host: string;
password: string;
username: string;
db: number;
ssl: boolean;
validateHostname: boolean;
}
type ConfigType = {
port: number;
logLevel: string;
logToFile: boolean;
logFilename: string;
statsd: {
host: string;
port: number;
globalTags: Array<string>;
};
redis: RedisConfig;
redisStreamPrefix: string;
redisStreamReadCount: number;
redisStreamReadBlockMs: number;
jwtAlgorithms: string[];
jwtSecret: string;
jwtCookieName: string;
jwtChannelIdKey: string;
socketResponseTimeoutMs: number;
pingSocketsIntervalMs: number;
gcChannelsIntervalMs: number;
};
function defaultConfig(): ConfigType {
return {
port: 8080,
logLevel: 'info',
logToFile: false,
logFilename: 'app.log',
redisStreamPrefix: 'async-events-',
redisStreamReadCount: 100,
redisStreamReadBlockMs: 5000,
jwtAlgorithms: ['HS256'],
jwtSecret: '',
jwtCookieName: 'async-token',
jwtChannelIdKey: 'channel',
socketResponseTimeoutMs: 60 * 1000,
pingSocketsIntervalMs: 20 * 1000,
gcChannelsIntervalMs: 120 * 1000,
statsd: {
host: '127.0.0.1',
port: 8125,
globalTags: [],
},
redis: {
host: '127.0.0.1',
port: 6379,
password: '',
username: 'default',
db: 0,
ssl: false,
validateHostname: true,
},
};
}
function configFromFile(): Partial<ConfigType> {
const isTest = process.env.NODE_ENV === 'test';
const configFile = isTest ? '../config.test.json' : '../config.json';
try {
return require(configFile);
} catch {
console.warn('config.json file not found');
return {};
}
}
const isPresent = (s: string) => /\S+/.test(s);
const toNumber = Number;
const toBoolean = (s: string) => s.toLowerCase() === 'true';
const toStringArray = (s: string) => s.split(',');
function applyEnvOverrides(config: ConfigType): ConfigType {
const envVarConfigSetter: { [envVar: string]: (val: string) => void } = {
PORT: val => (config.port = toNumber(val)),
LOG_LEVEL: val => (config.logLevel = val),
LOG_TO_FILE: val => (config.logToFile = toBoolean(val)),
LOG_FILENAME: val => (config.logFilename = val),
REDIS_STREAM_PREFIX: val => (config.redisStreamPrefix = val),
REDIS_STREAM_READ_COUNT: val =>
(config.redisStreamReadCount = toNumber(val)),
REDIS_STREAM_READ_BLOCK_MS: val =>
(config.redisStreamReadBlockMs = toNumber(val)),
JWT_SECRET: val => (config.jwtSecret = val),
JWT_COOKIE_NAME: val => (config.jwtCookieName = val),
SOCKET_RESPONSE_TIMEOUT_MS: val =>
(config.socketResponseTimeoutMs = toNumber(val)),
PING_SOCKETS_INTERVAL_MS: val =>
(config.pingSocketsIntervalMs = toNumber(val)),
GC_CHANNELS_INTERVAL_MS: val =>
(config.gcChannelsIntervalMs = toNumber(val)),
REDIS_HOST: val => (config.redis.host = val),
REDIS_PORT: val => (config.redis.port = toNumber(val)),
REDIS_PASSWORD: val => (config.redis.password = val),
REDIS_USERNAME: val => (config.redis.username = val),
REDIS_DB: val => (config.redis.db = toNumber(val)),
REDIS_SSL: val => (config.redis.ssl = toBoolean(val)),
STATSD_HOST: val => (config.statsd.host = val),
STATSD_PORT: val => (config.statsd.port = toNumber(val)),
STATSD_GLOBAL_TAGS: val => (config.statsd.globalTags = toStringArray(val)),
};
Object.entries(envVarConfigSetter).forEach(([envVar, set]) => {
const envValue = process.env[envVar];
if (envValue && isPresent(envValue)) {
set(envValue);
}
});
return config;
}
export function buildConfig(): ConfigType {
const config = _merge(defaultConfig(), configFromFile());
return applyEnvOverrides(config);
}