diff --git a/.gitignore b/.gitignore index c423139..637b488 100644 --- a/.gitignore +++ b/.gitignore @@ -88,4 +88,4 @@ /BackEnd/configs/OS.js /BackEnd/src/configs/OS.ts /BackEnd/dist - +/BackEnd/.env* diff --git a/BackEnd/nodemon.json b/BackEnd/nodemon.json index f8be398..c200acd 100644 --- a/BackEnd/nodemon.json +++ b/BackEnd/nodemon.json @@ -2,7 +2,10 @@ "events": { "start": "clear" }, + "execMap": { + "ts": "ts-node -r dotenv/config" + }, "ext": "ts", - "ignore": [".git", "node_modules", "dist"], + "ignore": [".git", "node_modules", "dist", "uploads"], "watch": ["./src"] } diff --git a/BackEnd/package-lock.json b/BackEnd/package-lock.json index 748049b..34b4efc 100644 --- a/BackEnd/package-lock.json +++ b/BackEnd/package-lock.json @@ -14,7 +14,9 @@ "body-parser": "^1.19.0", "child_process": "^1.0.2", "cors": "2.8.5", + "dotenv": "^10.0.0", "ejs": "3.1.5", + "envalid": "^7.1.1", "errorhandler": "1.5.1", "express": "^4.17.1", "express-generator": "^4.16.1", @@ -2969,6 +2971,14 @@ "node": ">=4" } }, + "node_modules/dotenv": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-10.0.0.tgz", + "integrity": "sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==", + "engines": { + "node": ">=10" + } + }, "node_modules/duplexer2": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/duplexer2/-/duplexer2-0.1.4.tgz", @@ -3137,6 +3147,14 @@ "integrity": "sha512-f2LZMYl1Fzu7YSBKg+RoROelpOaNrcGmE9AZubeDfrCEia483oW4MI4VyFd5VNHIgQ/7qm1I0wUHK1eJnn2y2w==", "dev": true }, + "node_modules/envalid": { + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/envalid/-/envalid-7.1.1.tgz", + "integrity": "sha512-2MIPjXAWcAVsO2jTVbgMFRc66lQCy2bQDP+zPT3NucrbWMD3pij5iGZMwQiv3zdveyVfl0qAGpI1JRQGfkmu1w==", + "engines": { + "node": ">=8.12" + } + }, "node_modules/error-ex": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/error-ex/-/error-ex-1.3.2.tgz", @@ -14646,6 +14664,11 @@ "is-obj": "^1.0.0" } }, + "dotenv": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-10.0.0.tgz", + "integrity": "sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==" + }, "duplexer2": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/duplexer2/-/duplexer2-0.1.4.tgz", @@ -14805,6 +14828,11 @@ "integrity": "sha512-f2LZMYl1Fzu7YSBKg+RoROelpOaNrcGmE9AZubeDfrCEia483oW4MI4VyFd5VNHIgQ/7qm1I0wUHK1eJnn2y2w==", "dev": true }, + "envalid": { + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/envalid/-/envalid-7.1.1.tgz", + "integrity": "sha512-2MIPjXAWcAVsO2jTVbgMFRc66lQCy2bQDP+zPT3NucrbWMD3pij5iGZMwQiv3zdveyVfl0qAGpI1JRQGfkmu1w==" + }, "error-ex": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/error-ex/-/error-ex-1.3.2.tgz", diff --git a/BackEnd/package.json b/BackEnd/package.json index a482297..517518b 100644 --- a/BackEnd/package.json +++ b/BackEnd/package.json @@ -20,7 +20,9 @@ "body-parser": "^1.19.0", "child_process": "^1.0.2", "cors": "2.8.5", + "dotenv": "^10.0.0", "ejs": "3.1.5", + "envalid": "^7.1.1", "errorhandler": "1.5.1", "express": "^4.17.1", "express-generator": "^4.16.1", diff --git a/BackEnd/src/env/db.ts b/BackEnd/src/env/db.ts new file mode 100644 index 0000000..efba7fa --- /dev/null +++ b/BackEnd/src/env/db.ts @@ -0,0 +1,2 @@ +export default { +} diff --git a/BackEnd/src/env/index.ts b/BackEnd/src/env/index.ts new file mode 100644 index 0000000..adaec72 --- /dev/null +++ b/BackEnd/src/env/index.ts @@ -0,0 +1,15 @@ +import { cleanEnv, str } from 'envalid' + +import serverEnv from './server' +import dbEnv from './db' + +const env = cleanEnv(process.env, { + NODE_ENV: str({ + choices: ['development', 'test', 'production'] + }), + + ...serverEnv, + ...dbEnv +}) + +export default env diff --git a/BackEnd/src/env/server.ts b/BackEnd/src/env/server.ts new file mode 100644 index 0000000..f12f76f --- /dev/null +++ b/BackEnd/src/env/server.ts @@ -0,0 +1,19 @@ +import { bool, host, port, str } from 'envalid' +import { ownDefault } from './utils' + +export default { + SERVER_HOSTNAME: host({ devDefault: 'localhost' }), + SERVER_PORT: port({ devDefault: 3000 }), + + SERVER_DEBUG: bool({ + desc: 'Allow to see more debug from like error handlers', + default: false, + devDefault: ownDefault({ dev: true, test: false, all: false }) + }), + SERVER_MORGAN_FORMAT: str({ + choices: ['combined', 'common', 'dev', 'short', 'tiny'], + default: 'combined', + devDefault: 'dev', + docs: 'https://www.npmjs.com/package/morgan#predefined-formats' + }) +} diff --git a/BackEnd/src/env/utils.ts b/BackEnd/src/env/utils.ts new file mode 100644 index 0000000..41a5bcd --- /dev/null +++ b/BackEnd/src/env/utils.ts @@ -0,0 +1,11 @@ +export function ownDefault ({ dev, test, all }: + {dev: T, test: T, all: T}) { + switch (process.env.NODE_ENV) { + case 'test': + return test + case 'development': + return dev + default: + return all + } +}