-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.ts
56 lines (50 loc) · 1.82 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
import dotenv from 'dotenv';
import path from 'path';
function getEnvironmentFilePath(): string {
switch (process.env.NODE_ENV) {
case 'production':
case 'development':
return '.env';
case 'local_development':
return '.env.local_development';
case 'local_production':
return '.env.local_production';
case 'local':
return '.env.local';
default:
return '.env';
}
}
dotenv.config({
path: path.resolve(getEnvironmentFilePath()),
});
function required<T>(key: string, defaultValue?: string): T {
if (!IS_TEST && (typeof process.env[key] === 'undefined' && typeof defaultValue === 'undefined' || process.env[key] === '')) {
throw new Error('Missing required environment variable: ' + key);
}
return process.env[key] as T || defaultValue as T;
}
export const IS_PRODUCTION = process.env.NODE_ENV === 'production';
export const IS_TEST = process.env.NODE_ENV === 'test';
export const IS_LOCAL = process.env.NODE_ENV ? process.env.NODE_ENV.toString().startsWith('local') : false;
export const config = {
NODE_ENV: required<string>('NODE_ENV'),
MYSQL: {
HOST: required<string>('MYSQL_HOST'),
PORT: required<number>('MYSQL_PORT'),
USER: required<string>('MYSQL_USER'),
PASSWORD: required<string>('MYSQL_PASSWORD'),
DATABASE: required<string>('MYSQL_DATABASE'),
},
JWT_SECRET: required<string>('JWT_SECRET'),
AES_SECRET: required<string>('AES_SECRET'),
AWS: {
CLOUDWATCH: {
ACCESS_KEY_ID: required<string>('AWS_CLOUDWATCH_ACCESS_KEY_ID'),
SECRET_ACCESS_KEY: required<string>('AWS_CLOUDWATCH_SECRET_ACCESS_KEY'),
},
},
};
console.log(`[CONFIGURATION] Initialized from ${getEnvironmentFilePath()}`);
console.log(`[CONFIGURATION] RUNNING NODE ENV: ${config.NODE_ENV}`);
console.log(`[CONFIGURATION] RUNNING MYSQL DB HOST: ${config.MYSQL.HOST}`);