|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +type ConfigType = { |
| 20 | + port: number; |
| 21 | + logLevel: string; |
| 22 | + logToFile: boolean; |
| 23 | + logFilename: string; |
| 24 | + statsd: { |
| 25 | + host: string; |
| 26 | + port: number; |
| 27 | + globalTags: Array<string>; |
| 28 | + }; |
| 29 | + redis: { |
| 30 | + port: number; |
| 31 | + host: string; |
| 32 | + password: string; |
| 33 | + db: number; |
| 34 | + ssl: boolean; |
| 35 | + }; |
| 36 | + redisStreamPrefix: string; |
| 37 | + redisStreamReadCount: number; |
| 38 | + redisStreamReadBlockMs: number; |
| 39 | + jwtSecret: string; |
| 40 | + jwtCookieName: string; |
| 41 | + socketResponseTimeoutMs: number; |
| 42 | + pingSocketsIntervalMs: number; |
| 43 | + gcChannelsIntervalMs: number; |
| 44 | +}; |
| 45 | + |
| 46 | +function defaultConfig(): ConfigType { |
| 47 | + return { |
| 48 | + port: 8080, |
| 49 | + logLevel: 'info', |
| 50 | + logToFile: false, |
| 51 | + logFilename: 'app.log', |
| 52 | + redisStreamPrefix: 'async-events-', |
| 53 | + redisStreamReadCount: 100, |
| 54 | + redisStreamReadBlockMs: 5000, |
| 55 | + jwtSecret: '', |
| 56 | + jwtCookieName: 'async-token', |
| 57 | + socketResponseTimeoutMs: 60 * 1000, |
| 58 | + pingSocketsIntervalMs: 20 * 1000, |
| 59 | + gcChannelsIntervalMs: 120 * 1000, |
| 60 | + statsd: { |
| 61 | + host: '127.0.0.1', |
| 62 | + port: 8125, |
| 63 | + globalTags: [], |
| 64 | + }, |
| 65 | + redis: { |
| 66 | + host: '127.0.0.1', |
| 67 | + port: 6379, |
| 68 | + password: '', |
| 69 | + db: 0, |
| 70 | + ssl: false, |
| 71 | + }, |
| 72 | + }; |
| 73 | +} |
| 74 | + |
| 75 | +function configFromFile(): Partial<ConfigType> { |
| 76 | + const isTest = process.env.NODE_ENV === 'test'; |
| 77 | + const configFile = isTest ? '../config.test.json' : '../config.json'; |
| 78 | + try { |
| 79 | + return require(configFile); |
| 80 | + } catch (err) { |
| 81 | + console.warn('config.json file not found'); |
| 82 | + return {}; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +const isPresent = (s: string) => /\S+/.test(s); |
| 87 | +const toNumber = Number; |
| 88 | +const toBoolean = (s: string) => s.toLowerCase() === 'true'; |
| 89 | +const toStringArray = (s: string) => s.split(','); |
| 90 | + |
| 91 | +function applyEnvOverrides(config: ConfigType): ConfigType { |
| 92 | + const envVarConfigSetter: { [envVar: string]: (val: string) => void } = { |
| 93 | + PORT: val => (config.port = toNumber(val)), |
| 94 | + LOG_LEVEL: val => (config.logLevel = val), |
| 95 | + LOG_TO_FILE: val => (config.logToFile = toBoolean(val)), |
| 96 | + LOG_FILENAME: val => (config.logFilename = val), |
| 97 | + REDIS_STREAM_PREFIX: val => (config.redisStreamPrefix = val), |
| 98 | + REDIS_STREAM_READ_COUNT: val => |
| 99 | + (config.redisStreamReadCount = toNumber(val)), |
| 100 | + REDIS_STREAM_READ_BLOCK_MS: val => |
| 101 | + (config.redisStreamReadBlockMs = toNumber(val)), |
| 102 | + JWT_SECRET: val => (config.jwtSecret = val), |
| 103 | + JWT_COOKIE_NAME: val => (config.jwtCookieName = val), |
| 104 | + SOCKET_RESPONSE_TIMEOUT_MS: val => |
| 105 | + (config.socketResponseTimeoutMs = toNumber(val)), |
| 106 | + PING_SOCKETS_INTERVAL_MS: val => |
| 107 | + (config.pingSocketsIntervalMs = toNumber(val)), |
| 108 | + GC_CHANNELS_INTERVAL_MS: val => |
| 109 | + (config.gcChannelsIntervalMs = toNumber(val)), |
| 110 | + REDIS_HOST: val => (config.redis.host = val), |
| 111 | + REDIS_PORT: val => (config.redis.port = toNumber(val)), |
| 112 | + REDIS_PASSWORD: val => (config.redis.password = val), |
| 113 | + REDIS_DB: val => (config.redis.db = toNumber(val)), |
| 114 | + REDIS_SSL: val => (config.redis.ssl = toBoolean(val)), |
| 115 | + STATSD_HOST: val => (config.statsd.host = val), |
| 116 | + STATSD_PORT: val => (config.statsd.port = toNumber(val)), |
| 117 | + STATSD_GLOBAL_TAGS: val => (config.statsd.globalTags = toStringArray(val)), |
| 118 | + }; |
| 119 | + |
| 120 | + for (const [envVar, set] of Object.entries(envVarConfigSetter)) { |
| 121 | + const envValue = process.env[envVar]; |
| 122 | + if (envValue && isPresent(envValue)) { |
| 123 | + set(envValue); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return config; |
| 128 | +} |
| 129 | + |
| 130 | +export function buildConfig(): ConfigType { |
| 131 | + const config = Object.assign(defaultConfig(), configFromFile()); |
| 132 | + return applyEnvOverrides(config); |
| 133 | +} |
0 commit comments