2021-07-25 20:45:51 +00:00
|
|
|
/* eslint-disable no-console */
|
|
|
|
const { NODE_ENV } = process.env;
|
|
|
|
if (!NODE_ENV || NODE_ENV === 'development') {
|
|
|
|
// Load env vars from /data-ingestor/.env
|
|
|
|
require('custom-env').env();
|
|
|
|
}
|
|
|
|
|
|
|
|
process.on('exit', () => {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log('Server Shutting Shutdown');
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on('unhandledRejection', err => {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error('Unhandled rejection in server process occurred');
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on('uncaughtException', err => {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error('Uncaught exception in server process occurred');
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
const express = require('express');
|
|
|
|
const app = express();
|
|
|
|
const http = require('http').createServer(app);
|
|
|
|
const cors = require('cors');
|
|
|
|
const io = require('socket.io')(http, {
|
2021-07-28 11:12:17 +00:00
|
|
|
path: '/realtime/socket.io',
|
2021-07-25 20:45:51 +00:00
|
|
|
transports: ['websocket', 'polling'], // using websocket does not require sticky session
|
|
|
|
});
|
|
|
|
// attach socket to global object
|
|
|
|
global.io = io;
|
|
|
|
|
|
|
|
app.use(cors());
|
|
|
|
|
|
|
|
app.use(function(req, res, next) {
|
|
|
|
if (typeof req.body === 'string') {
|
|
|
|
req.body = JSON.parse(req.body);
|
|
|
|
}
|
|
|
|
res.header('Access-Control-Allow-Credentials', true);
|
|
|
|
res.header('Access-Control-Allow-Origin', req.headers.origin);
|
|
|
|
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
|
|
|
|
res.header(
|
|
|
|
'Access-Control-Allow-Headers',
|
|
|
|
'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept,Authorization'
|
|
|
|
);
|
|
|
|
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Add limit of 10 MB to avoid "Request Entity too large error"
|
|
|
|
// https://stackoverflow.com/questions/19917401/error-request-entity-too-large
|
|
|
|
app.use(express.urlencoded({ limit: '10mb', extended: true }));
|
|
|
|
app.use(express.json({ limit: '10mb' }));
|
|
|
|
|
2021-07-28 12:37:15 +00:00
|
|
|
app.get(['/stat', '/realtime/stat'], function(req, res) {
|
|
|
|
res.setHeader('Content-Type', 'application/json');
|
|
|
|
res.send(
|
|
|
|
JSON.stringify({
|
|
|
|
status: 200,
|
|
|
|
message: 'Service Status - OK',
|
|
|
|
serviceType: 'fyipe-realtime',
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-07-28 11:12:17 +00:00
|
|
|
app.use(['/', '/realtime'], require('./api/realtime'));
|
2021-07-25 20:45:51 +00:00
|
|
|
|
|
|
|
app.set('port', process.env.PORT || 3300);
|
|
|
|
|
|
|
|
http.listen(app.get('port'), function() {
|
|
|
|
// eslint-disable-next-line
|
|
|
|
console.log('realtime server started on port ' + app.get('port'));
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = app;
|