mirror of
https://github.com/filiprojek/nork.git
synced 2024-11-23 01:01:04 +01:00
Added environment module
This commit is contained in:
parent
2e2352aa57
commit
ae5ceb643f
@ -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"
|
||||
|
@ -37,3 +37,13 @@
|
||||
- dropnul jsem support pro js
|
||||
|
||||
- version update: 3.0.5
|
||||
|
||||
### 4-03-2024
|
||||
|
||||
- [ ] modules
|
||||
- [ ] Log
|
||||
- [ ] Auth
|
||||
- [ ] Env
|
||||
- [ ] Docs
|
||||
- [ ] Databases
|
||||
- [ ] CLI
|
@ -5,4 +5,6 @@
|
||||
//app.use(morgan('dev'))
|
||||
|
||||
import Log from "./modules/LogService"
|
||||
export { Log }
|
||||
import Env from "./modules/Environment"
|
||||
|
||||
export { Log, Env }
|
||||
|
73
src/modules/Environment.ts
Normal file
73
src/modules/Environment.ts
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user