-
Notifications
You must be signed in to change notification settings - Fork 18
/
config.ts
223 lines (204 loc) · 6.58 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
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
import { ConfigScope } from '@lokalise/node-core'
import type { RedisConfig } from '@lokalise/node-core'
import type { AwsConfig } from './aws/awsConfig.js'
import { getAwsConfig } from './aws/awsConfig.js'
const configScope: ConfigScope = new ConfigScope()
export const SERVICE_NAME = 'node-service-template'
export type IntervalJobConfig = {
periodInSeconds: number
}
export type CronJobConfig = {
cronExpression: string
}
export type Config = {
app: AppConfig
db: DbConfig
aws: AwsConfig
redis: RedisConfig
scheduler: RedisConfig
amqp: AmqpConfig
integrations: {
fakeStore: {
baseUrl: string
}
}
jobs: JobConfig
vendors: {
newrelic: {
isEnabled: boolean
appName: string
}
bugsnag: {
isEnabled: boolean
apiKey?: string
appType?: string
}
amplitude: {
isEnabled: boolean
apiKey?: string
serverZone: string
flushIntervalMillis?: number
flushMaxRetries?: number
flushQueueSize?: number
}
}
}
export type JobConfig = {
processLogFilesJob: IntervalJobConfig
deleteOldUsersJob: IntervalJobConfig
sendEmailsJob: CronJobConfig
}
export type DbConfig = {
databaseUrl: string
}
export type AmqpConfig = {
hostname: string
port: number
username: string
password: string
vhost: string
useTls: boolean
}
export type AppConfig = {
port: number
bindAddress: string
jwtPublicKey: string
logLevel: 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace' | 'silent'
nodeEnv: 'production' | 'development' | 'test'
appEnv: 'production' | 'development' | 'staging'
appVersion: string
gitCommitSha: string
baseUrl: string
metrics: {
isEnabled: boolean
}
}
export function getConfig(): Config {
return {
app: getAppConfig(),
db: getDbConfig(),
aws: getAwsConfig(configScope),
redis: getRedisConfig(),
amqp: getAmqpConfig(),
scheduler: getSchedulerConfig(),
integrations: {
fakeStore: {
baseUrl: configScope.getMandatory('SAMPLE_FAKE_STORE_BASE_URL'),
},
},
jobs: {
deleteOldUsersJob: {
periodInSeconds: configScope.getMandatoryInteger('DELETE_OLD_USERS_JOB_PERIOD_IN_SECS'),
},
processLogFilesJob: {
periodInSeconds: configScope.getMandatoryInteger('PROCESS_LOGS_FILES_JOB_PERIOD_IN_SECS'),
},
sendEmailsJob: {
cronExpression: configScope.getMandatory('SEND_EMAILS_JOB_CRON'),
},
},
vendors: {
newrelic: {
isEnabled: configScope.getOptionalBoolean('NEW_RELIC_ENABLED', true),
appName: configScope.getOptionalNullable('NEW_RELIC_APP_NAME', ''),
},
bugsnag: {
isEnabled: configScope.getOptionalBoolean('BUGSNAG_ENABLED', true),
apiKey: configScope.getOptionalNullable('BUGSNAG_KEY', undefined),
appType: configScope.getOptionalNullable('BUGSNAG_APP_TYPE', undefined),
},
amplitude: {
isEnabled: configScope.getOptionalBoolean('AMPLITUDE_ENABLED', false),
apiKey: configScope.getOptionalNullable('AMPLITUDE_KEY', undefined),
serverZone: configScope.getOptionalOneOf('AMPLITUDE_SERVER_ZONE', 'EU', ['EU', 'US']),
flushIntervalMillis: configScope.getOptionalInteger(
'AMPLITUDE_FLUSH_INTERVAL_MILLIS',
10_000,
),
flushQueueSize: configScope.getOptionalInteger('AMPLITUDE_FLUSH_QUEUE_SIZE', 300),
flushMaxRetries: configScope.getOptionalInteger('AMPLITUDE_FLUSH_MAX_RETRIES', 12),
},
},
}
}
export function getAmqpConfig(): AmqpConfig {
return {
hostname: configScope.getMandatory('AMQP_HOSTNAME'),
port: configScope.getMandatoryInteger('AMQP_PORT'),
username: configScope.getMandatory('AMQP_USERNAME'),
password: configScope.getMandatory('AMQP_PASSWORD'),
vhost: configScope.getOptional('AMQP_VHOST', ''),
useTls: configScope.getOptionalBoolean('AMQP_USE_TLS', true),
}
}
export function getDbConfig(): DbConfig {
return {
databaseUrl: configScope.getMandatory('DATABASE_URL'),
}
}
export function getRedisConfig(): RedisConfig {
return {
host: configScope.getMandatory('REDIS_HOST'),
keyPrefix: configScope.getMandatory('REDIS_KEY_PREFIX'),
port: configScope.getMandatoryInteger('REDIS_PORT'),
username: configScope.getOptionalNullable('REDIS_USERNAME', undefined),
password: configScope.getOptionalNullable('REDIS_PASSWORD', undefined),
useTls: configScope.getOptionalBoolean('REDIS_USE_TLS', true),
commandTimeout: configScope.getOptionalNullableInteger('REDIS_COMMAND_TIMEOUT', undefined),
connectTimeout: configScope.getOptionalNullableInteger('REDIS_CONNECT_TIMEOUT', undefined),
}
}
export function getSchedulerConfig(): RedisConfig {
return {
host: configScope.getMandatory('SCHEDULER_REDIS_HOST'),
keyPrefix: configScope.getMandatory('SCHEDULER_REDIS_KEY_PREFIX'),
port: configScope.getMandatoryInteger('SCHEDULER_REDIS_PORT'),
username: configScope.getOptionalNullable('SCHEDULER_REDIS_USERNAME', undefined),
password: configScope.getOptionalNullable('SCHEDULER_REDIS_PASSWORD', undefined),
useTls: configScope.getOptionalBoolean('REDIS_USE_TLS', true),
commandTimeout: configScope.getOptionalNullableInteger(
'SCHEDULER_REDIS_COMMAND_TIMEOUT',
undefined,
),
connectTimeout: configScope.getOptionalNullableInteger(
'SCHEDULER_REDIS_CONNECT_TIMEOUT',
undefined,
),
}
}
export function getAppConfig(): AppConfig {
return {
port: configScope.getOptionalInteger('APP_PORT', 3000),
bindAddress: configScope.getMandatory('APP_BIND_ADDRESS'),
jwtPublicKey: decodeJwtConfig(configScope.getMandatory('JWT_PUBLIC_KEY')),
logLevel: configScope.getMandatoryOneOf('LOG_LEVEL', [
'fatal',
'error',
'warn',
'info',
'debug',
'trace',
'silent',
]),
nodeEnv: configScope.getMandatoryOneOf('NODE_ENV', ['production', 'development', 'test']),
appEnv: configScope.getMandatoryOneOf('APP_ENV', ['production', 'development', 'staging']),
appVersion: configScope.getOptional('APP_VERSION', 'VERSION_NOT_SET'),
baseUrl: configScope.getOptional('BASE_URL', ``),
gitCommitSha: configScope.getOptional('GIT_COMMIT_SHA', 'COMMIT_SHA_NOT_SET'),
metrics: {
isEnabled: configScope.getOptionalBoolean('METRICS_ENABLED', !configScope.isDevelopment()),
},
}
}
export function isDevelopment() {
return configScope.isDevelopment()
}
export function isTest() {
return configScope.isTest()
}
export function isProduction() {
return configScope.isProduction()
}
export function decodeJwtConfig(jwtPublicKey: string) {
return jwtPublicKey.replaceAll('||', '\n')
}