/
config.js
123 lines (114 loc) · 3.25 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
const _ = require('lodash');
const assert = require('assert');
const omit = require('omit-deep');
const environment = process.env.NODE_ENV;
const configs = {
production: function() {
return {
baseURL: assertEnvVar(
'BASE_URL',
'The URL of the website including the protocol and the port if needed'
),
port: assertEnvVar(
'PORT',
'The port that the REST api should be listening on'
),
vgoURL: assertEnvVar(
'VGO_URL',
'The VGO URL used for getting cases information and sending opening offers'
),
vgoAPIKey: assertEnvVar('VGO_API_KEY', 'The VGO website API Key'),
affiliateAddress: assertEnvVar(
'AFFILIATE_ADDRESS',
'The Ethereum address of the account for collecting fees'
),
sessionKeys: [
assertEnvVar('SESSION_KEY', 'Key used for session managment'),
],
steamApiKey: assertEnvVar(
'STEAM_API_KEY',
'Key used for steam login integration'
),
log: {
level: optionalEnvVar('LOG_LEVEL', 'Log level', 'info'),
},
};
},
development: function() {
return {
baseURL: 'http://localhost:3000',
port: 3001,
vgoURL: 'http://localhost:3002',
vgoAPIKey: 'somekey',
affiliateAddress: '0x0000000000000000',
sessionKeys: ['this is not secure'],
steamApiKey: assertEnvVar(
'STEAM_API_KEY',
'Key used for steam login integration'
),
log: {
level: 'debug',
},
};
},
qa: function() {
return {
baseURL: 'http://localhost:3000',
port: 3001,
vgoURL: 'http://localhost:3002',
vgoAPIKey: 'somekey',
affiliateAddress: '0x0000000000000000',
sessionKeys: ['this is not secure'],
steamApiKey: assertEnvVar(
'STEAM_API_KEY',
'Key used for steam login integration'
),
log: {
level: 'debug',
},
};
},
};
let config = configs[environment];
assert(
config,
`Configuration ${environment} does not exist, NODE_ENV must be one of ${Object.keys(
configs
)}`
);
config = config();
// Convenience helper to display the config with sensitive fields stripped
config.toWire = function() {
return omit(_.cloneDeep(config), ['password', 'toWire']);
};
console.log(
`Running in ${environment} mode. Config is ${JSON.stringify(
config.toWire(),
null,
2
)}\n--\n'password' keys omitted.`
);
module.exports = config;
function assertEnvVar(envVar, description) {
const value = process.env[envVar];
assert(
value !== null && value !== undefined,
`Environment variable ${envVar} (currently is ${value}) must be present. Description: ${description}`
);
return value;
}
function optionalEnvVar(envVar, description, defaultValue) {
const value = process.env[envVar];
if (value === null || value === undefined) {
if (defaultValue === undefined) {
console.warn(
`Optional environment variable ${envVar} (currently is ${value}) not present. Description: ${description}`
);
} else {
console.warn(
`Optional environment variable ${envVar} (currently is ${value}) not present. Description: ${description}. Using default value ${defaultValue}`
);
}
}
return value || defaultValue;
}