Setup logger, configloader, middleware & endpoints

This commit is contained in:
2021-01-03 18:16:01 +01:00
parent 444502a84c
commit 5b9d9aeca8
9 changed files with 303 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
const path = require('path');
const Field = require('./field.js');
let instance = null;
class Config {
constructor() {
this.location = Config.determineLocation();
this.fields = require(`${this.location}`);
}
static getInstance() {
if (instance == null) {
instance = new Config();
}
return instance;
}
static determineLocation() {
if (process.env.NODE_ENV === "production")
return path.join(__dirname, "../../config/env/production.json");
return path.join(__dirname, "../../config/env/development.json");
}
get(section, option) {
if (this.fields[section] === undefined || this.fields[section][option] === undefined) {
throw new Error(`Field "${section} => ${option}" does not exist.`);
}
const field = new Field(this.fields[section][option]);
if (field.value === '') {
const envField = process.env[[section.toUpperCase(), option.toUpperCase()].join('_')];
if (envField !== undefined && envField.length !== 0) { return envField; }
}
if (field.value === undefined) {
throw new Error(`${section} => ${option} is empty.`);
}
return field.value;
}
}
module.exports = Config;