/
env-config.js
45 lines (36 loc) · 1.15 KB
/
env-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
const dotenv = require('dotenv');
const os = require('os');
function getLocalIP() {
// Network interfaces we want to ignore
const blacklist = [/VirtualBox/];
function inBlacklist(key) {
return blacklist.some((regex) => key.match(regex) !== null);
}
const interfaces = os.networkInterfaces();
let retval;
Object.keys(interfaces)
.filter((key) => !inBlacklist(key))
.some((key) => {
const ipv4 = interfaces[key].find((address) => address.family === 'IPv4');
if (ipv4 !== undefined) {
retval = ipv4.address;
return true;
}
return false;
});
return retval;
}
// In production, environment variables come from cloud
// (via now.json and secrets)
if (process.env.NODE_ENV !== 'production') {
dotenv.config();
}
process.env.PORT = parseInt(process.env.PORT, 10) || 3000;
process.env.BACKEND_URL = !process.env.BACKEND_URL
? `http://${getLocalIP()}:${process.env.PORT}`
: process.env.BACKEND_URL;
// Exported values are provided to frontend via transform-define
module.exports = {
'process.env.BACKEND_URL': process.env.BACKEND_URL,
'process.env.BUILD_TIMESTAMP': new Date().toString(),
};