2020-08-16 19:05:28 +00:00
|
|
|
const http = require('http');
|
2017-12-15 18:34:59 +00:00
|
|
|
const express = require('express');
|
2018-06-14 08:20:19 +00:00
|
|
|
const cors = require('cors');
|
2018-01-14 18:53:29 +00:00
|
|
|
|
2017-12-15 18:34:59 +00:00
|
|
|
const path = require('path');
|
|
|
|
const apicache = require('apicache');
|
|
|
|
const logger = require('log4js').getLogger('Server');
|
|
|
|
|
|
|
|
const train = require('./server/lib/train');
|
2020-05-04 09:34:41 +00:00
|
|
|
const twitter = require('./server/lib/twitter');
|
2020-08-16 19:05:28 +00:00
|
|
|
|
|
|
|
const WebSocket = require('ws');
|
|
|
|
const SocketHandler = require('./server/lib/wshandlerv3');
|
|
|
|
|
|
|
|
const Events = require('events');
|
|
|
|
const busEmitter = new Events.EventEmitter();
|
|
|
|
|
2020-08-20 14:08:07 +00:00
|
|
|
logger.level = process.env.LOGGER_LEVEL || 'debug';
|
2017-12-15 18:34:59 +00:00
|
|
|
|
|
|
|
const app = express();
|
2017-12-15 19:12:48 +00:00
|
|
|
const port = process.env.PORT || 8100;
|
2017-12-15 18:34:59 +00:00
|
|
|
|
|
|
|
const sitePath = 'live';
|
|
|
|
|
|
|
|
apicache.options({ 'debug': true });
|
|
|
|
const cache = apicache.middleware;
|
2020-05-04 09:34:41 +00:00
|
|
|
app.use(express.json());
|
2017-12-15 18:34:59 +00:00
|
|
|
|
|
|
|
app.use(express.static(path.join(__dirname, sitePath)));
|
|
|
|
|
2018-01-14 18:53:29 +00:00
|
|
|
app.options('*', cors()); // include before other routes
|
2018-01-14 18:45:02 +00:00
|
|
|
|
2018-01-14 18:57:22 +00:00
|
|
|
app.use('/gettrains', cors(), cache('60 seconds'), train.getTrainTimes);
|
2018-01-14 18:56:20 +00:00
|
|
|
app.use('/getnexttraintimes', cors(), cache('60 seconds'), train.getNextTrainTimes);
|
2018-01-14 18:57:22 +00:00
|
|
|
app.use('/getroute', cors(), cache('60 seconds'), train.getRoute);
|
|
|
|
app.use('/getnews', cors(), cache('30 minutes'), train.getNews);
|
2020-05-04 09:34:41 +00:00
|
|
|
app.use('/getservice', cors(), cache('60 seconds'), train.getService);
|
|
|
|
|
|
|
|
app.use('/twitter', cors(), twitter.all);
|
2017-12-15 18:34:59 +00:00
|
|
|
|
2018-06-14 08:20:19 +00:00
|
|
|
process.on('uncaughtException', err => {
|
|
|
|
logger.error(err);
|
|
|
|
});
|
|
|
|
|
2020-08-16 19:05:28 +00:00
|
|
|
/* app.listen(port, (err) => {
|
2017-12-15 18:34:59 +00:00
|
|
|
if (err)
|
|
|
|
return logger.error('Server error:', err);
|
|
|
|
|
|
|
|
logger.info(`TrainTime Server is listening on ${port}`);
|
2020-08-16 19:05:28 +00:00
|
|
|
});*/
|
|
|
|
|
|
|
|
const server = http.createServer(app);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const wss = new WebSocket.Server({ server });
|
|
|
|
|
|
|
|
const webSocket = new SocketHandler(busEmitter, wss);
|
|
|
|
|
|
|
|
twitter.updates(busEmitter);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
logger.error(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
server.listen(port, (err) => {
|
|
|
|
if (err)
|
|
|
|
return logger.error('Server error:', err);
|
|
|
|
|
|
|
|
logger.info(`New Traintime server listening on ${ server.address().port}`);
|
2017-12-15 18:34:59 +00:00
|
|
|
});
|