From 7e0dd27b7808fbda495f19ee5d9480c1ed2b8bee Mon Sep 17 00:00:00 2001 From: velvettear Date: Sun, 20 Feb 2022 22:04:32 +0100 Subject: [PATCH] modified some config stuff --- config.json | 6 +++--- libs/info.js | 1 - libs/logger.js | 37 +++++++++++++++++++++++++++++++------ libs/util.js | 35 ++++++++++++++++++++++++++++++++++- pbc.js | 33 +++++++++++++++++++++++++++++---- 5 files changed, 97 insertions(+), 15 deletions(-) diff --git a/config.json b/config.json index 0d71d20..6d01576 100644 --- a/config.json +++ b/config.json @@ -1,15 +1,15 @@ { "server": { "listen": "0.0.0.0", - "port": 3000, - "timestamp": "DD.MM.YYYY HH:mm:ss:SS" + "port": 3000 }, "cache": { "active": false, "lifetime": 15 }, "log": { - "level": "debug" + "level": "debug", + "format": "DD.MM.YYYY HH:mm:ss:SS" }, "osc": { "host": "192.168.1.24", diff --git a/libs/info.js b/libs/info.js index 35d0e62..aead730 100644 --- a/libs/info.js +++ b/libs/info.js @@ -1,4 +1,3 @@ -const logger = require('./logger.js'); const cache = require('./cache.js'); const commands = require('./commands.js'); const os = require('os'); diff --git a/libs/logger.js b/libs/logger.js index d71ea7e..caba8ad 100644 --- a/libs/logger.js +++ b/libs/logger.js @@ -1,4 +1,3 @@ -const config = require('../config.json'); const moment = require('moment'); // constants @@ -11,9 +10,26 @@ const LOGLEVEL_INFO = 1; const LOGLEVEL_WARNING = 2; const LOGLEVEL_ERROR = 3; -// set loglevel on 'require' -const loglevel = function () { - switch (config.log.level) { +var loglevel = getLogLevel(); +var timestamp = getTimestamp(); + +function initialize() { + return new Promise((resolve, reject) => { + if (global.config == undefined) { + reject('could not initialize logger, config is undefined'); + } + loglevel = getLogLevel(); + timestamp = getTimestamp(); + resolve(); + }); +} + +// get the loglevel +function getLogLevel() { + if (global.config?.log?.level == undefined) { + return LOGLEVEL_INFO; + } + switch (global.config.log.level) { case LOG_PREFIX_DEBUG: case LOGLEVEL_DEBUG: return LOGLEVEL_DEBUG; @@ -29,7 +45,15 @@ const loglevel = function () { default: return LOGLEVEL_INFO; } -}(); +} + +// get the timestamp format +function getTimestamp() { + if (global.config?.log?.format != undefined) { + return global.config.log.format; + } + return "DD.MM.YYYY HH:mm:ss:SS"; +} // log a http request - response object function http(object) { @@ -109,12 +133,13 @@ function trace(message, prefix) { default: print = console.log; } - message = moment().format(config.server.timestamp) + ' | ' + prefix + ' > ' + message; + message = moment().format(timestamp) + ' | ' + prefix + ' > ' + message; print(message); } // exports module.exports = { + initialize, info, warn, debug, diff --git a/libs/util.js b/libs/util.js index ea02216..9b08a5a 100644 --- a/libs/util.js +++ b/libs/util.js @@ -1,4 +1,6 @@ const logger = require('./logger.js'); +const realpath = require('fs/promises').realpath; +const stat = require('fs/promises').stat; const http = require('http'); const { HTTP_GET, HTTP_POST } = require('./constants.js'); @@ -79,11 +81,42 @@ function sortById(array) { }); } +function fileExists(file) { + return new Promise((resolve, reject) => { + if (file == undefined) { + reject('can not check the existence of an undefined file'); + } + resolvePath(file) + .then((path) => { + stat(path) + .then((stats) => { + resolve({path, stats}); + }) + }) + .catch(reject); + }); +} + +function resolvePath(file) { + return new Promise((resolve, reject) => { + if (file == undefined) { + reject('can not resolve a path to an undefined file'); + } + realpath(file) + .then(resolve) + .catch((err) => { + reject('resolving path \'' + file + '\' encountered an error >>> ' + err); + }); + }); +} + module.exports = { timeDiff, clone, httpGET, httpPOST, toHex, - sortById + sortById, + fileExists, + resolvePath } \ No newline at end of file diff --git a/pbc.js b/pbc.js index bf22f61..fb88077 100644 --- a/pbc.js +++ b/pbc.js @@ -1,12 +1,37 @@ const logger = require('./libs/logger.js'); const api = require('./libs/api.js'); const server = require('./libs/server.js') +const util = require('./libs/util.js'); const packageJSON = require('./package.json'); -logger.info("launching " + packageJSON.name + " " + packageJSON.version); -api.setupEndpoints() +const INTERRUPTS = ['SIGINT', 'SIGTERM']; + +global.config = process.argv[2] || __dirname + '/config.json'; + +handleInterrupts(); + +util.fileExists(config) + .then((result) => { + global.config = require(result.path); + global.config.path = result.path; + }) + .then(logger.initialize) + .then(() => { + logger.info("launching " + packageJSON.name + " " + packageJSON.version); + }) + .then(api.setupEndpoints) .then(server.start) - .catch(function (err) { + .catch((err) => { logger.error(err); process.exit(1); - }); \ No newline at end of file + }); + +function handleInterrupts() { + for (var index = 0; index < INTERRUPTS.length; index++) { + process.once(INTERRUPTS[index], (code) => { + watchers.stop() + .then(exit(code)) + .catch(exit(code)); + }); + } +} \ No newline at end of file