From ae5ceb643f149987e460307db956ed47ead801b8 Mon Sep 17 00:00:00 2001 From: Filip Rojek Date: Thu, 4 Apr 2024 01:32:29 +0200 Subject: [PATCH] Added environment module --- package.json | 1 + progress-blog.md | 10 ++++++ src/app.ts | 4 ++- src/modules/Environment.ts | 73 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 src/modules/Environment.ts diff --git a/package.json b/package.json index d6b7233..b7fa714 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "license": "MIT", "dependencies": { "colors": "1.4.0", + "dotenv": "^16.4.5", "fs-extra": "^10.0.0", "inquirer": "^8.1.2", "pad": "^3.2.0" diff --git a/progress-blog.md b/progress-blog.md index c0fdc9b..f993785 100644 --- a/progress-blog.md +++ b/progress-blog.md @@ -37,3 +37,13 @@ - dropnul jsem support pro js - version update: 3.0.5 + +### 4-03-2024 + + - [ ] modules + - [ ] Log + - [ ] Auth + - [ ] Env + - [ ] Docs + - [ ] Databases +- [ ] CLI \ No newline at end of file diff --git a/src/app.ts b/src/app.ts index 01f27ad..1aa8ebe 100644 --- a/src/app.ts +++ b/src/app.ts @@ -5,4 +5,6 @@ //app.use(morgan('dev')) import Log from "./modules/LogService" -export { Log } +import Env from "./modules/Environment" + +export { Log, Env } diff --git a/src/modules/Environment.ts b/src/modules/Environment.ts new file mode 100644 index 0000000..9dcccd3 --- /dev/null +++ b/src/modules/Environment.ts @@ -0,0 +1,73 @@ +import path from 'path' +import fs from 'fs-extra' +import Log from '../modules/LogService' +import dotenv from 'dotenv' + +export default class Environment { + static get(env_path: string) { + console.log('ENV PATH:', env_path) + + if(!fs.existsSync(env_path)) { + console.log('env_path = ', env_path) + Log.error(500, `.env file for ${process.env.NODE_ENV ? process.env.NODE_ENV : ''} environment does not exists`) + process.exit() + } + + const norkcfg_path = path.join(path.dirname(env_path), '../norkconfig.json') + if(!fs.existsSync(norkcfg_path)) { + console.log('norkcfg_path = ', env_path) + Log.error(500, `norkcfg file does not exists`) + process.exit() + } + + dotenv.config({ path: env_path }) + + const norkcfg = fs.readJSONSync(norkcfg_path) + + if (norkcfg.database) { + if (norkcfg.database.db == 'postgresql') { + if (!process.env.DB_PORT) { + process.env.DB_PORT = '5432' + } + + if (!process.env.DB_HOST) { + process.env.DB_HOST = '127.0.0.1' + } + + if (!process.env.DB_USERNAME || !process.env.DB_PASSWORD || !process.env.DB_DATABASE) { + Log.error(500, 'missing DB parameters in .env file') + process.exit() + } + } + } + + if (process.env.JWT_SECRET === undefined || process.env.JWT_SECRET == '') { + Log.error(500, 'JWT_SECRET is not set!') + process.exit() + } + + return { + // General + APP_PORT: Number(process.env.APP_PORT), + APP_HOST: String(process.env.APP_HOST), + APP_HOSTNAME: process.env.APP_HOSTNAME !== undefined ? String(process.env.APP_HOSTNAME) : null, + CORS_WHITELIST: String(process.env.CORS_WHITELIST), + JWT_SECRET: String(process.env.JWT_SECRET), + // MongoDB + DB_URI: String(process.env.DB_URI), + // PostgreSQL + DB_PORT: Number(process.env.DB_PORT), + DB_HOST: String(process.env.DB_HOST), + DB_USERNAME: String(process.env.DB_USERNAME), + DB_PASSWORD: String(process.env.DB_PASSWORD), + DB_DATABASE: String(process.env.DB_DATABASE), + // Nork + NORK: norkcfg, + // SMTP + SMTP_HOST: String(process.env.SMTP_HOST), + SMTP_USER: String(process.env.SMTP_USER), + SMTP_PASS: String(process.env.SMTP_PASS), + SMTP_FROM: String(process.env.SMTP_FROM) + } + } +}