-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.js
177 lines (150 loc) · 4.23 KB
/
config.js
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
import { identity, isInteger } from 'lodash-es';
import log4js from 'log4js';
import { dirname, resolve as resolvePath } from 'node:path';
import { fileURLToPath } from 'node:url';
const logLevels = ['FATAL', 'ERROR', 'WARN', 'INFO', 'DEBUG', 'TRACE'];
export const root = dirname(fileURLToPath(import.meta.url));
// eslint-disable-next-line max-lines-per-function
export async function loadConfig() {
await loadDotenv();
const environment = process.env.NODE_ENV ?? 'development';
const dbFile = resolvePath(
root,
parseEnvironmentString('REVPROD_DB_FILE', 'db.json')
);
const host = parseEnvironmentString('REVPROD_LISTEN_HOST', '0.0.0.0');
const port = parseEnvironmentPort('REVPROD_LISTEN_PORT', 3000);
const logLevel = parseEnvironmentEnum(
'REVPROD_LOG_LEVEL',
logLevels,
environment === 'production' ? 'DEBUG' : 'TRACE',
value => value.toUpperCase()
);
const cors = parseEnvironmentBoolean('REVPROD_CORS', false);
const corsOrigins = parseEnvironmentString(
'REVPROD_CORS_ORIGINS',
[],
origins => origins.split(',')
);
const title = parseEnvironmentString(
'REVPROD_TITLE',
'The Revolutionary Product'
);
const landingPageBaseUrl = parseEnvironmentString(
'REVPROD_LANDING_PAGE_BASE_URL',
''
);
function createLogger(category) {
const newLogger = log4js.getLogger(category);
newLogger.level = logLevel;
return newLogger;
}
const logger = createLogger('config');
logger.info(`Environment: ${environment}`);
logger.info(`Log level: ${logLevel}`);
logger.info(`CORS enabled: ${cors}`);
logger.info(
`CORS allowed origins: ${
corsOrigins.length === 0 ? 'all' : corsOrigins.join(', ')
}`
);
return {
// Paths
dbFile,
// Environment,
env: environment,
// Server
host,
port,
// Security
cors,
corsOrigins,
// Application
title,
landingPageBaseUrl,
// Functions
createLogger
};
}
async function loadDotenv() {
let dotenv;
try {
dotenv = await import('dotenv');
} catch {
// Ignore
}
if (dotenv) {
dotenv.config();
}
}
function getEnvironmentString(variableName, required = true) {
const value = process.env[variableName];
if (required && value === undefined) {
throw new Error(`$${variableName} is required`);
}
return value;
}
function parseEnvironmentBoolean(variableName, defaultValue) {
const value = getEnvironmentString(variableName, defaultValue === undefined);
if (value === undefined) {
return defaultValue;
} else if (/^(?:1|y|yes|t|true)$/u.test(value)) {
return true;
} else if (/^(?:0|n|no|f|false)$/u.test(value)) {
return false;
}
throw new Error(
`$${variableName} must be a boolean, but its value is ${JSON.stringify(
value
)}`
);
}
function parseEnvironmentEnum(
variableName,
allowedValues,
defaultValue,
coerce = identity
) {
const value = getEnvironmentString(variableName, defaultValue === undefined);
if (value === undefined) {
return defaultValue;
}
const coerced = coerce(value);
if (!allowedValues.includes(coerced)) {
throw new Error(
`$${variableName} must be one of ${allowedValues
.map(allowed => JSON.stringify(allowed))
.join(', ')}, but its value is ${JSON.stringify(coerced)}`
);
}
return coerced;
}
function parseEnvironmentInt(variableName, defaultValue, min, max) {
const value = getEnvironmentString(variableName, defaultValue === undefined);
if (value === undefined) {
return defaultValue;
}
const parsed = Number.parseInt(value, 10);
if (
!isInteger(parsed) ||
(min !== undefined && value < min) ||
(max !== undefined && value > max)
) {
throw new Error(
`$${variableName} must be an integer between ${min ?? '-Infinity'} and ${
max ?? 'Infinity'
}, but its value is ${JSON.stringify(value)}`
);
}
return parsed;
}
function parseEnvironmentPort(variableName, defaultValue) {
return parseEnvironmentInt(variableName, defaultValue, 1, 65_535);
}
function parseEnvironmentString(variableName, defaultValue, coerce = identity) {
const value = getEnvironmentString(variableName, defaultValue === undefined);
if (value === undefined) {
return defaultValue;
}
return coerce(value);
}