2021-01-26 14:03:42 +00:00
|
|
|
process.on('exit', () => {
|
|
|
|
/* eslint-disable no-console */
|
|
|
|
console.log('Shutting Shutdown');
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on('unhandledRejection', err => {
|
|
|
|
/* eslint-disable no-console */
|
|
|
|
console.error('Unhandled rejection in process occurred');
|
|
|
|
/* eslint-disable no-console */
|
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on('uncaughtException', err => {
|
|
|
|
/* eslint-disable no-console */
|
|
|
|
console.error('Uncaught exception in process occurred');
|
|
|
|
/* eslint-disable no-console */
|
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
|
2020-02-16 23:18:38 +00:00
|
|
|
const express = require('express');
|
|
|
|
const app = express();
|
|
|
|
const path = require('path');
|
|
|
|
const bodyParser = require('body-parser');
|
2020-10-13 17:28:55 +00:00
|
|
|
const http = require('http');
|
2020-01-19 14:03:27 +00:00
|
|
|
|
2020-02-16 23:18:38 +00:00
|
|
|
const { NODE_ENV } = process.env;
|
2020-01-19 14:03:27 +00:00
|
|
|
|
|
|
|
if (NODE_ENV === 'local' || NODE_ENV === 'development')
|
|
|
|
require('custom-env').env(process.env.NODE_ENV);
|
|
|
|
|
|
|
|
global.httpServerResponse = {
|
|
|
|
statusCode: 200,
|
2020-02-27 11:15:46 +00:00
|
|
|
responseType: { values: ['json', 'html'], currentType: 'json' },
|
2020-01-19 14:03:27 +00:00
|
|
|
responseTime: 0,
|
2020-07-10 22:10:50 +00:00
|
|
|
header: {},
|
2020-02-27 11:15:46 +00:00
|
|
|
body: { status: 'ok' },
|
2020-01-19 14:03:27 +00:00
|
|
|
};
|
|
|
|
|
2020-10-19 02:08:52 +00:00
|
|
|
app.use('*', function(req, res, next) {
|
2020-01-19 14:03:27 +00:00
|
|
|
if (process.env && process.env.PRODUCTION) {
|
|
|
|
res.set('Cache-Control', 'public, max-age=86400');
|
2020-02-27 11:15:46 +00:00
|
|
|
} else res.set('Cache-Control', 'no-cache');
|
2020-01-19 14:03:27 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
app.set('views', path.join(__dirname, 'views'));
|
|
|
|
app.set('view engine', 'ejs');
|
|
|
|
app.use(express.static('public'));
|
|
|
|
|
|
|
|
app.use(bodyParser.urlencoded({ extended: true }));
|
2020-02-29 22:56:00 +00:00
|
|
|
app.use(bodyParser.json());
|
2020-01-19 14:03:27 +00:00
|
|
|
|
|
|
|
app.use(require('./backend/api/settings'));
|
|
|
|
|
2020-10-19 02:08:52 +00:00
|
|
|
app.get('/', function(req, res) {
|
2020-10-13 17:28:55 +00:00
|
|
|
if (http.STATUS_CODES[global.httpServerResponse.statusCode]) {
|
|
|
|
res.status(global.httpServerResponse.statusCode);
|
|
|
|
} else {
|
|
|
|
res.status(422);
|
|
|
|
}
|
2020-10-19 02:08:52 +00:00
|
|
|
setTimeout(function() {
|
2020-01-19 14:03:27 +00:00
|
|
|
if (global.httpServerResponse.responseType.currentType === 'html') {
|
2020-01-21 17:00:35 +00:00
|
|
|
res.setHeader('Content-Type', 'text/html');
|
2020-07-10 22:10:50 +00:00
|
|
|
try {
|
|
|
|
const header = JSON.parse(global.httpServerResponse.header);
|
|
|
|
if (typeof header === 'object') {
|
|
|
|
for (const key in header) {
|
|
|
|
res.setHeader(String(key), String(header[key]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
//
|
|
|
|
}
|
2020-01-19 14:03:27 +00:00
|
|
|
return res.send(global.httpServerResponse.body);
|
|
|
|
} else {
|
|
|
|
res.setHeader('Content-Type', 'application/json');
|
2020-01-21 17:00:35 +00:00
|
|
|
return res.send(global.httpServerResponse.body);
|
2020-01-19 14:03:27 +00:00
|
|
|
}
|
|
|
|
}, global.httpServerResponse.responseTime);
|
|
|
|
});
|
|
|
|
|
2020-07-10 19:04:36 +00:00
|
|
|
const hook = {};
|
|
|
|
|
2020-10-19 02:08:52 +00:00
|
|
|
app.post('/api/webhooks/:id', function(req, res) {
|
2020-07-10 19:04:36 +00:00
|
|
|
const { id } = req.params;
|
|
|
|
hook[id] = req.body;
|
|
|
|
return res.status(200).json(req.body);
|
|
|
|
});
|
|
|
|
|
2020-10-19 02:08:52 +00:00
|
|
|
app.get('/api/webhooks/:id', function(req, res) {
|
2020-07-10 19:04:36 +00:00
|
|
|
const { id } = req.params;
|
|
|
|
if (hook[id] === undefined) return res.status(404).json({});
|
|
|
|
return res.status(200).json(hook[id]);
|
|
|
|
});
|
|
|
|
|
2020-10-19 02:08:52 +00:00
|
|
|
app.use('/*', function(req, res) {
|
2020-01-19 14:03:27 +00:00
|
|
|
res.status(404).render('notFound.ejs', {});
|
|
|
|
});
|
|
|
|
|
|
|
|
app.set('port', process.env.PORT || 3010);
|
|
|
|
|
2020-10-19 02:08:52 +00:00
|
|
|
app.listen(app.get('port'), function() {
|
2020-01-19 14:03:27 +00:00
|
|
|
//eslint-disable-next-line
|
2020-02-27 11:15:46 +00:00
|
|
|
console.log('Server running on port : ' + app.get('port'));
|
2020-01-19 14:03:27 +00:00
|
|
|
});
|