optimized api setup and handling

This commit is contained in:
Daniel Sommer 2022-03-18 13:26:51 +01:00
parent 797c32d5c6
commit 739c52fc84
2 changed files with 45 additions and 40 deletions

View file

@ -10,7 +10,7 @@
"api": [
{
"url": "/tail",
"type": "get",
"method": "get",
"command": "tail",
"args": [
"-f", "/tmp/test"
@ -19,7 +19,7 @@
},
{
"url": "/uptime",
"type": "get",
"method": "get",
"command": "uptime",
"args": [
]

View file

@ -1,6 +1,7 @@
const logger = require('./logger.js');
const commands = require('./commands.js');
const http = require('http');
const { config } = require('process');
let server;
let api;
@ -15,7 +16,7 @@ async function start() {
return new Promise((resolve, reject) => {
server.listen(port, listen)
.on('listening', function () {
logger.debug('server listening on ' + config.server.listen + ':' + config.server.port + '...');
logger.info('server listening on ' + global.config.server.listen + ':' + global.config.server.port + '...');
handleRequests();
resolve();
});
@ -25,46 +26,42 @@ async function start() {
function handleRequests() {
server.on('request', (request, response) => {
request.timestamp = new Date().getTime();
respond(request, response, api.get(request.url));
respond(request, response);
});
}
async function respond(request, response, endpoint) {
async function respond(request, response) {
if (request === undefined || response === undefined) {
return;
}
let endpoint = api[request.method]?.[request.url];
if (endpoint === undefined) {
return finishRequest(request, response, { error: 'endpoint not defined' }, 501);
}
try {
endpoint.result = await commands.execute(endpoint);
} catch (err) {
endpoint.error = err.toString();
return finishRequest(request, response, endpoint, 501);
}
return finishRequest(request, response, endpoint);
}
function finishRequest(request, response, data, code) {
if (response === undefined) {
return;
}
let data = {
status: 'ok',
command: endpoint.command,
args: endpoint.args
};
if (endpoint.detached === true) {
data.detached = true;
if (code === undefined) {
code = 200;
}
let code = 200;
if (endpoint === undefined) {
code = 501;
data.status = 'error';
data.msg = 'endpoint not defined';
} else if (request.method.toLowerCase() !== (endpoint.type.toLowerCase())) {
code = 405;
data.status = 'error';
data.msg = 'endpoint does not support ' + request.method + ' requests';
if (code === 200) {
data.status = 'ok';
} else {
try {
const result = await commands.execute(endpoint);
if (result !== undefined) {
data.result = result;
}
} catch (err) {
code = 501;
data.status = 'error';
data.msg = err.toString();
}
data.status = 'error';
}
data.time = new Date().getTime() - request.timestamp + 'ms';
const json = JSON.stringify(data);
data.time = (new Date().getTime() - request.timestamp) + 'ms';
response.writeHead(code);
const json = JSON.stringify(data);
response.end(json);
logger.http({ request: request, code: code, data: json });
}
@ -74,13 +71,21 @@ function setupAPI() {
if (apiConfig === undefined || apiConfig.length === 0) {
throw new Error('no api endpoints configured - aborting');
}
api = new Map();
config.api.forEach(function (endpoint) {
let url = endpoint.url;
let tmp = endpoint;
delete tmp.url;
api.set(url, tmp);
});
const now = new Date().getTime();
api = {};
logger.debug('setting up api...');
let index;
for (index = 0; index < global.config.api.length; index++) {
let endpoint = global.config.api[index];
let key = endpoint.url;
let method = endpoint.method.toUpperCase();
delete endpoint.method;
delete endpoint.url;
let methodApi = api[method] || {};
methodApi[key] = endpoint;
api[method] = methodApi;
}
logger.debug('api setup with ' + index + ' endpoints after ' + (new Date().getTime() - now) + 'ms');
}
module.exports = {