1 | import dotenv from "dotenv";
|
2 |
|
3 | /**
|
4 | * init dotenv
|
5 | * priority: environment -> .env
|
6 | *
|
7 | * Available settings
|
8 | *
|
9 | * APP_PORT=9527
|
10 | * APP_BASE_PATH=/v1
|
11 | * APP_MONGODB_CONNECTION="mongodb://localhost/petstore"
|
12 | */
|
13 |
|
14 | dotenv.config();
|
15 |
|
16 | /**
|
17 | *
|
18 | * @param {string} name envrionment name
|
19 | * @param {object} opt option with { required, default }
|
20 | * @returns {*} value
|
21 | */
|
22 |
|
23 | export function env(name, init) {
|
24 | const value = process.env[name.toUpperCase()] || process.env[name] || init;
|
25 |
|
26 | if (value === undefined) {
|
27 | throw new Error(`environment ${name} is missing`);
|
28 | }
|
29 |
|
30 | return value;
|
31 | }
|
32 |
|
33 | /**
|
34 | * basic
|
35 | */
|
36 | export const NODE_ENV = env("NODE_ENV", "development");
|
37 | export const PORT = env("PORT", 9527);
|
38 | export const LOG_LEVEL = env("LOG_LEVEL", "info");
|