Added environment module

This commit is contained in:
Filip Rojek 2024-04-04 01:32:29 +02:00
parent 2e2352aa57
commit ae5ceb643f
4 changed files with 87 additions and 1 deletions

View File

@ -27,6 +27,7 @@
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"colors": "1.4.0", "colors": "1.4.0",
"dotenv": "^16.4.5",
"fs-extra": "^10.0.0", "fs-extra": "^10.0.0",
"inquirer": "^8.1.2", "inquirer": "^8.1.2",
"pad": "^3.2.0" "pad": "^3.2.0"

View File

@ -37,3 +37,13 @@
- dropnul jsem support pro js - dropnul jsem support pro js
- version update: 3.0.5 - version update: 3.0.5
### 4-03-2024
- [ ] modules
- [ ] Log
- [ ] Auth
- [ ] Env
- [ ] Docs
- [ ] Databases
- [ ] CLI

View File

@ -5,4 +5,6 @@
//app.use(morgan('dev')) //app.use(morgan('dev'))
import Log from "./modules/LogService" import Log from "./modules/LogService"
export { Log } import Env from "./modules/Environment"
export { Log, Env }

View File

@ -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)
}
}
}