generated from ComicScrip/klassmate-api
-
Notifications
You must be signed in to change notification settings - Fork 3
/
env.js
53 lines (45 loc) · 1.65 KB
/
env.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
require('dotenv').config();
function getEnv(variable) {
const value = process.env[variable];
if (typeof value === 'undefined') {
console.warn(`Seems like the variable "${variable}" is not set in the environment.
Did you forget to execute "cp .env.sample .env" and adjust variables in the .env file to match your own environment ?`);
}
return value;
}
const inProdEnv = getEnv('NODE_ENV') === 'production';
const inDevEnv = getEnv('NODE_ENV') === 'development';
const inTestEnv = getEnv('NODE_ENV') === 'test';
const PORT = getEnv(`PORT${inTestEnv ? '_TEST' : ''}`);
const DATABASE_URL = getEnv(`DATABASE_URL`);
const dbUrlregex =
// eslint-disable-next-line no-useless-escape
/^(?:([^:\/?#\s]+):\/{2})?(?:([^@\/?#\s]+)@)?([^\/?#\s]+)?(?:\/([^?#\s]*))?(?:[?]([^#\s]+))?\S*$/;
const DB_USER = DATABASE_URL.match(dbUrlregex)[2].split(':')[0];
const DB_PASSWORD = DATABASE_URL.match(dbUrlregex)[2].split(':')[1];
const DB_HOST = DATABASE_URL.match(dbUrlregex)[3].split(':')[0];
const DB_PORT = DATABASE_URL.match(dbUrlregex)[3].split(':')[1];
const DB_NAME = DATABASE_URL.match(dbUrlregex)[4].split('/')[0];
const CORS_ALLOWED_ORIGINS = getEnv(`CORS_ALLOWED_ORIGINS`);
const SESSION_COOKIE_DOMAIN = getEnv(`SESSION_COOKIE_DOMAIN`);
const SESSION_COOKIE_NAME = getEnv(`SESSION_COOKIE_NAME`);
const SESSION_COOKIE_SECRET = getEnv(`SESSION_COOKIE_SECRET`);
const API_BASE_URL = getEnv(`API_BASE_URL`);
module.exports = {
getEnv,
inTestEnv,
inProdEnv,
inDevEnv,
PORT,
CORS_ALLOWED_ORIGINS,
SESSION_COOKIE_DOMAIN,
SESSION_COOKIE_NAME,
SESSION_COOKIE_SECRET,
DATABASE_URL,
DB_HOST,
DB_USER,
DB_PASSWORD,
DB_NAME,
DB_PORT,
API_BASE_URL,
};