pbc/libs/server.js

116 lines
3.8 KiB
JavaScript
Raw Normal View History

2022-02-07 15:41:27 +01:00
const config = require('../config.json');
const logger = require('./logger.js');
const api = require('./api.js')
const constants = require('./constants.js');
const modep = require('./modep.js');
const http = require('http');
var server;
function start() {
return new Promise(function (resolve, reject) {
if (!server) {
server = http.createServer();
}
server.listen(config.server.port, config.server.listen).on('listening', function () {
logger.debug('server listening on ' + config.server.listen + ':' + config.server.port + '...');
handleRequests();
});
});
}
function handleRequests() {
server.on('request', function (request, response) {
logger.request(request);
2022-02-07 23:50:16 +01:00
if (request.url.endsWith('/')) {
request.url = request.url.substring(0, request.url.length - 1);
}
2022-02-07 15:41:27 +01:00
var endpoint = api.getEndpoints().get(request.url);
if (!endpoint) {
var msg = 'endpoint \'' + request.url + '\' not defined';
response.writeHead(501);
response.end(msg);
logger.debug(msg);
return;
}
2022-02-08 11:11:49 +01:00
endpoint = endpoint[request.method];
if (endpoint == undefined || endpoint.method == undefined) {
var msg = 'error: endpoint \'' + request.url + '\' does not have any handlers for ' + request.method + ' requests';
2022-02-07 15:41:27 +01:00
response.writeHead(405);
response.end(msg);
logger.debug(msg);
return;
}
2022-02-08 13:46:02 +01:00
getRequestParams(request)
.then(function (params) {
return endpoint.method(endpoint.id, params);
})
2022-02-08 11:11:49 +01:00
.then(function (result) {
response.writeHead(200);
try {
response.end(JSON.stringify(result));
} catch (err) {
2022-02-07 23:50:16 +01:00
response.end(result);
2022-02-08 11:11:49 +01:00
}
})
.catch(function (err, code) {
2022-02-08 13:46:02 +01:00
logger.error(err);
2022-02-08 11:11:49 +01:00
if (code == undefined) {
code = 500;
}
response.writeHead(500);
response.end(err);
});
2022-02-07 15:41:27 +01:00
});
}
2022-02-08 13:46:02 +01:00
function getRequestParams(request) {
2022-02-07 15:41:27 +01:00
return new Promise(function (resolve, reject) {
var params = "";
request.on("data", function (data) {
params += data;
});
request.on("end", function () {
2022-02-08 13:46:02 +01:00
if (params == undefined || params.length == 0) {
return resolve();
}
2022-02-07 15:41:27 +01:00
return resolve(new URLSearchParams(params));
});
});
}
function handlePOSTPedals(request) {
return new Promise(function (resolve, reject) {
2022-02-08 13:46:02 +01:00
getRequestParams(request)
2022-02-07 15:41:27 +01:00
.then(function (params) {
var pedalId = request.url.substring(request.url.lastIndexOf('/') + 1);
var controlId = params.get('id');
if (controlId == undefined) {
reject('error: could not handle POST - missing parameter \'id\'', 400)
}
var value = params.get('value');
if (value == undefined) {
reject('error: could not handle POST - missing parameter \'value\'', 400)
}
modep.getPedalControlById(pedalId, controlId)
.then(function (control) {
modep.sendValueToControl(value, control);
})
.then(resolve)
.catch(reject);
})
.catch(reject);
});
}
function handlePOSTPedalboards(request) {
return new Promise(function (resolve, reject) {
modep.setPedalboardById(request.url.substring(request.url.lastIndexOf('/') + 1))
.then(resolve)
.catch(reject);
});
}
module.exports = {
start
}