modified some config stuff
This commit is contained in:
parent
505400d1f4
commit
7e0dd27b78
5 changed files with 97 additions and 15 deletions
|
@ -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",
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
const logger = require('./logger.js');
|
||||
const cache = require('./cache.js');
|
||||
const commands = require('./commands.js');
|
||||
const os = require('os');
|
||||
|
|
|
@ -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,
|
||||
|
|
35
libs/util.js
35
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
|
||||
}
|
33
pbc.js
33
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);
|
||||
});
|
||||
});
|
||||
|
||||
function handleInterrupts() {
|
||||
for (var index = 0; index < INTERRUPTS.length; index++) {
|
||||
process.once(INTERRUPTS[index], (code) => {
|
||||
watchers.stop()
|
||||
.then(exit(code))
|
||||
.catch(exit(code));
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue