blinky/libs/server.js

113 lines
No EOL
3.7 KiB
JavaScript

const config = require('../config.json');
const logger = require('./logger.js');
const controller = require('./controller.js');
const parser = require('./parser.js');
const constants = require('./constants.js');
const path = require('path');
const express = require('express');
const favicon = require('serve-favicon');
const bodyparser = require('body-parser');
const app = express();
app.use(favicon(path.join(path.dirname(__dirname), 'public', 'favicon.ico')));
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({ extended: true }));
const html = require('./index.js').get();
// run the express http server and handle gets/posts
async function start() {
return await new Promise(function (resolve, reject) {
app.listen(config.server.port, config.server.listen)
.on('listening', function () {
resolve('server listening on ' + config.server.listen + ':' + config.server.port + '...')
})
.on('error', function (err) {
reject('error starting server (' + err + ')');
});
});
}
function handleRequests() {
// GET html page
app.get('*', (request, response) => {
logger.http(request);
response.send(html);
response.end();
});
// POST '/set'
app.post('/' + constants.MODE_SET, (request, response) => {
logger.http(request);
handleSimpleAnimation(parser.parseRequest(request.body, constants.MODE_SET), response);
});
// POST '/morph'
app.post('/' + constants.MODE_MORPH, (request, response) => {
logger.http(request);
handleSimpleAnimation(parser.parseRequest(request.body, constants.MODE_MORPH), response);
});
// POST '/blink'
app.post('/' + constants.MODE_BLINK, (request, response) => {
logger.http(request);
handleComplexAnimation(parser.parseRequest(request.body, constants.MODE_BLINK), response);
});
// POST '/pulse'
app.post('/' + constants.MODE_PULSE, (request, response) => {
logger.http(request);
handleComplexAnimation(parser.parseRequest(request.body, constants.MODE_PULSE), response);
});
// POST '/poweroff'
app.post('/' + constants.MODE_POWEROFF, (request, response) => {
logger.http(request);
handlePowerOff(parser.parseRequest(request.body, constants.MODE_POWEROFF), response);
});
}
async function handleSimpleAnimation(config, response) {
try {
response.end(JSON.stringify(await controller.simple(config)));
} catch (err) {
if (err.message.includes('feature report')) {
await controller.resetBlinksticks();
}
logger.error(err);
response.status(500);
response.end(JSON.stringify({ status: 'error', error: err.message }));
}
}
async function handleComplexAnimation(config, response) {
if (controller.isInfiniteAnimation(config)) {
response.end(JSON.stringify({ status: 'ok', time: 'infinite' }));
response = undefined;
}
try {
const result = await controller.complex(config);
if (response === undefined) {
return;
}
response.end(JSON.stringify(result));
} catch (err) {
if (response === undefined) {
return;
}
logger.error(err);
response.status(500);
response.end(JSON.stringify({ status: 'error', error: err.message }));
}
}
async function handlePowerOff(config, response) {
try {
response.end(JSON.stringify(await controller.powerOff(config)));
} catch (err) {
response.status(500);
response.end(JSON.stringify({ status: 'error', error: err.message }));
}
}
// exports
module.exports = {
start,
handleRequests
};