2018-02-23 10:36:49 +00:00
|
|
|
const express = require('express');
|
|
|
|
const path = require('path');
|
|
|
|
const apicache = require('apicache');
|
|
|
|
const logger = require('log4js').getLogger('Server');
|
|
|
|
const weather = require('./server/weather');
|
|
|
|
const euronews = require('./server/euronews');
|
|
|
|
const foursquare = require('./server/foursquare');
|
2018-02-26 16:56:14 +00:00
|
|
|
const rightbyme = require('./server/RightByMe');
|
2018-03-13 13:46:26 +00:00
|
|
|
const agenda = require('./server/agenda');
|
|
|
|
const directions = require('./server/directions');
|
2018-11-04 01:12:59 +00:00
|
|
|
const geocode = require('./server/geocode');
|
2018-02-23 10:36:49 +00:00
|
|
|
|
2020-05-10 10:59:35 +00:00
|
|
|
const cors = require('cors');
|
|
|
|
|
2018-02-23 10:36:49 +00:00
|
|
|
logger.level = 'debug';
|
|
|
|
|
|
|
|
const app = express();
|
2020-05-10 10:59:35 +00:00
|
|
|
|
|
|
|
app.use(cors());
|
|
|
|
|
2018-02-23 10:41:51 +00:00
|
|
|
const port = process.env.PORT || 8110;
|
2018-02-23 10:36:49 +00:00
|
|
|
|
|
|
|
const sitePath = 'live';
|
|
|
|
|
|
|
|
apicache.options({ 'debug': true });
|
|
|
|
const cache = apicache.middleware;
|
2020-01-20 23:15:01 +00:00
|
|
|
const standardError = {
|
|
|
|
'error':'There was an error'
|
|
|
|
};
|
2018-02-23 10:36:49 +00:00
|
|
|
|
|
|
|
app.use(express.static(path.join(__dirname, sitePath)));
|
|
|
|
|
2018-04-15 21:55:47 +00:00
|
|
|
// app.get('/weather', cache('15 minutes'), (req, res) => {
|
2018-05-22 14:38:49 +00:00
|
|
|
|
|
|
|
const asyncMiddleware = fn =>
|
|
|
|
(req, res, next) => {
|
|
|
|
Promise.resolve(fn(req, res, next))
|
|
|
|
.catch(next);
|
|
|
|
};
|
|
|
|
|
2018-04-15 21:55:47 +00:00
|
|
|
app.get('/weather', (req, res) => {
|
2018-02-23 10:36:49 +00:00
|
|
|
if (req.query.hasOwnProperty('ll'))
|
2018-03-01 23:45:04 +00:00
|
|
|
|
2018-02-23 10:36:49 +00:00
|
|
|
weather.doGetOpenWeather(req.query.ll)
|
|
|
|
.then((d) => {
|
2018-10-17 10:50:57 +00:00
|
|
|
res.set('Cache-Control', 'public, max-age=1800');
|
2018-02-23 10:36:49 +00:00
|
|
|
res.send(d);
|
|
|
|
}).catch((e) => {
|
|
|
|
logger.error(e);
|
2020-01-20 23:15:01 +00:00
|
|
|
res.status(500).send(Object.assign(standardError, { 'source':'weather', 'e':e }));
|
2018-02-23 10:36:49 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
else {
|
|
|
|
// throw new Error('Weather: LL missing');
|
|
|
|
logger.warn('Weather: LL missing');
|
|
|
|
res.status(500).send('LL Missing');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-04-15 21:55:47 +00:00
|
|
|
// app.get('/forecast', cache('15 minutes'), (req, res) => {
|
|
|
|
app.get('/forecast', (req, res) => {
|
|
|
|
logger.info('/forecast');
|
2018-03-07 00:02:22 +00:00
|
|
|
if (req.query.hasOwnProperty('ll'))
|
|
|
|
|
|
|
|
weather.doGetFullForcast(req.query.ll)
|
|
|
|
.then((d) => {
|
2018-10-17 10:50:57 +00:00
|
|
|
res.set('Cache-Control', 'public, max-age=1800');
|
2018-03-07 00:02:22 +00:00
|
|
|
res.send(d);
|
|
|
|
}).catch((e) => {
|
|
|
|
logger.error(e);
|
2020-01-20 23:15:01 +00:00
|
|
|
res.status(500).send(Object.assign(standardError, { 'source':'forecast', 'e':e }));
|
2018-03-07 00:02:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
else {
|
|
|
|
// throw new Error('Weather: LL missing');
|
|
|
|
logger.warn('Weather: LL missing');
|
|
|
|
res.status(500).send('LL Missing');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-03-05 19:14:37 +00:00
|
|
|
app.get('/weatheralert', cache('15 minutes'), (req, res) => {
|
2018-03-01 23:45:04 +00:00
|
|
|
if (req.query.hasOwnProperty('ll'))
|
|
|
|
// weather.doGetOpenWeather(req.query.ll)
|
|
|
|
// doGetDarkSkyWeather
|
|
|
|
weather.doGetDarkSkyWeather(req.query.ll)
|
|
|
|
.then((d) => {
|
2018-10-17 10:50:57 +00:00
|
|
|
res.set('Cache-Control', 'public, max-age=1800');
|
2018-03-01 23:45:04 +00:00
|
|
|
res.send(d);
|
|
|
|
}).catch((e) => {
|
|
|
|
logger.error(e);
|
2020-01-20 23:15:01 +00:00
|
|
|
res.status(500).send(Object.assign(standardError, { 'source':'weatheralert', 'e':e }));
|
2018-03-01 23:45:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
else {
|
|
|
|
// throw new Error('Weather: LL missing');
|
|
|
|
logger.warn('Weather: LL missing');
|
|
|
|
res.status(500).send('LL Missing');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-03-05 19:14:37 +00:00
|
|
|
app.get('/fsexplore', cache('15 minutes'), (req, res) => {
|
2018-03-25 20:43:44 +00:00
|
|
|
if (req.query.hasOwnProperty('ll')) {
|
|
|
|
const ll = req.query.ll;
|
|
|
|
const limit = req.query.hasOwnProperty('ll') ? req.query.limit : 3;
|
|
|
|
const section = req.query.hasOwnProperty('section') ? req.query.section : 'topPicks';
|
2018-09-03 21:23:04 +00:00
|
|
|
const query = req.query.hasOwnProperty('query') ? req.query.query : '';
|
2020-01-20 23:15:01 +00:00
|
|
|
console.log('req.query', req.query);
|
2018-09-03 21:23:04 +00:00
|
|
|
foursquare.doGetFourSquareExplore(ll, limit, section, query)
|
2018-02-23 10:36:49 +00:00
|
|
|
.then((d) => {
|
2018-10-17 10:50:57 +00:00
|
|
|
res.set('Cache-Control', 'public, max-age=900');
|
2018-02-23 10:36:49 +00:00
|
|
|
res.send(d);
|
|
|
|
}).catch((e) => {
|
|
|
|
logger.error(e);
|
2020-01-20 23:15:01 +00:00
|
|
|
res.status(500).send(Object.assign(standardError, { 'source':'fsexplore', 'e':e }));
|
2018-02-23 10:36:49 +00:00
|
|
|
});
|
2018-03-25 20:43:44 +00:00
|
|
|
}
|
2018-02-23 10:36:49 +00:00
|
|
|
|
|
|
|
else {
|
|
|
|
// throw new Error('Weather: LL missing');
|
|
|
|
logger.warn('FS: LL missing');
|
|
|
|
res.status(500).send('LL Missing');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-01-20 23:15:01 +00:00
|
|
|
app.get('/geocode', /* cache('15 minutes'),*/ (req, res) => {
|
2018-11-04 01:12:59 +00:00
|
|
|
if (req.query.hasOwnProperty('ll')) {
|
|
|
|
const ll = req.query.ll;
|
2020-01-20 23:15:01 +00:00
|
|
|
console.log('ll', ll);
|
2018-11-04 01:12:59 +00:00
|
|
|
geocode.doGetGeocode(ll)
|
|
|
|
.then((d) => {
|
|
|
|
res.set('Cache-Control', 'public, max-age=900');
|
|
|
|
res.send(d);
|
|
|
|
}).catch((e) => {
|
|
|
|
logger.error(e);
|
2020-01-20 23:15:01 +00:00
|
|
|
res.status(500).send(Object.assign(standardError, { 'source':'geocode', 'e':e }));
|
2018-11-04 01:12:59 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
// throw new Error('Weather: LL missing');
|
|
|
|
logger.warn('FS: LL missing');
|
|
|
|
res.status(500).send('LL Missing');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-10-17 10:50:57 +00:00
|
|
|
app.get('/rightbyme', cache('86400 seconds'), (req, res) => {
|
2018-02-26 16:56:14 +00:00
|
|
|
if (req.query.hasOwnProperty('ll'))
|
|
|
|
rightbyme.doGetRightByMe(req.query.ll)
|
|
|
|
.then((d) => {
|
2018-10-17 10:50:57 +00:00
|
|
|
res.set('Cache-Control', 'public, max-age=86400');
|
2018-02-26 16:56:14 +00:00
|
|
|
res.send(d);
|
|
|
|
}).catch((e) => {
|
|
|
|
logger.error(e);
|
2020-01-20 23:15:01 +00:00
|
|
|
res.status(500).send(Object.assign(standardError, { 'source':'rightbyme', 'e':e }));
|
2018-02-26 16:56:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
else {
|
|
|
|
// throw new Error('Weather: LL missing');
|
|
|
|
logger.warn('FS: LL missing');
|
|
|
|
res.status(500).send('LL Missing');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-03-25 20:43:44 +00:00
|
|
|
app.get('/nearbydetail', cache('15 minutes'), (req, res) => {
|
2018-03-23 17:13:53 +00:00
|
|
|
if (req.query.hasOwnProperty('id'))
|
|
|
|
rightbyme.doGetMoreDetail(req.query.id)
|
|
|
|
.then((d) => {
|
2018-11-12 00:10:14 +00:00
|
|
|
res.set('Cache-Control', 'public, max-age=1800');
|
2018-03-23 17:13:53 +00:00
|
|
|
res.send(d);
|
|
|
|
}).catch((e) => {
|
|
|
|
logger.error(e);
|
2020-01-20 23:15:01 +00:00
|
|
|
res.status(500).send(Object.assign(standardError, { 'source':'nearbydetail', 'e':e }));
|
2018-03-23 17:13:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
else {
|
|
|
|
// throw new Error('Weather: LL missing');
|
|
|
|
logger.warn('FS: LL missing');
|
|
|
|
res.status(500).send('LL Missing');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-03-05 19:14:37 +00:00
|
|
|
app.get('/news', cache('15 minutes'), (req, res) => {
|
2018-02-23 10:36:49 +00:00
|
|
|
euronews.getEuroNews().then((d) => {
|
2018-10-17 10:50:57 +00:00
|
|
|
res.set('Cache-Control', 'public, max-age=1800');
|
2018-02-23 10:36:49 +00:00
|
|
|
res.send(d);
|
|
|
|
}).catch((e) => {
|
2018-03-05 19:14:37 +00:00
|
|
|
if (e.message === '304:Not changed') {
|
|
|
|
logger.info('Euronews ', e.message);
|
|
|
|
res.status(304).send();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
logger.error(e);
|
2020-01-20 23:15:01 +00:00
|
|
|
res.status(500).send(Object.assign(standardError, { 'source':'news', 'e':e }));
|
2018-03-05 19:14:37 +00:00
|
|
|
}
|
2018-02-23 10:36:49 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-03-06 12:03:36 +00:00
|
|
|
app.get('/article', cache('1 hour'), (req, res) => {
|
2018-03-05 19:14:37 +00:00
|
|
|
if (req.query.hasOwnProperty('guid')) {
|
|
|
|
logger.debug('Beofre', req.query.guid);
|
|
|
|
|
|
|
|
euronews.getArticle(req.query.guid)
|
|
|
|
.then((d) => {
|
2018-10-17 10:50:57 +00:00
|
|
|
res.set('Cache-Control', 'public, max-age=3600');
|
2018-03-05 19:14:37 +00:00
|
|
|
res.send(d);
|
|
|
|
}).catch((e) => {
|
2018-03-13 13:46:26 +00:00
|
|
|
logger.error(e);
|
2020-01-20 23:15:01 +00:00
|
|
|
res.status(500).send(Object.assign(standardError, { 'source':'article', 'e':e }));
|
2018-03-13 13:46:26 +00:00
|
|
|
});
|
2018-03-05 19:14:37 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// throw new Error('Weather: LL missing');
|
|
|
|
logger.warn('FS: GUID missing');
|
|
|
|
res.status(500).send('GUID Missing');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-03-13 13:46:26 +00:00
|
|
|
app.get('/agenda', cache('15 minutes'), (req, res) => {
|
|
|
|
agenda.doTodaysAgenda()
|
|
|
|
.then((d) => {
|
2018-10-17 10:50:57 +00:00
|
|
|
res.set('Cache-Control', 'public, max-age=900');
|
2018-03-13 13:46:26 +00:00
|
|
|
res.send(d);
|
|
|
|
}).catch((e) => {
|
|
|
|
logger.error(e);
|
2020-01-20 23:15:01 +00:00
|
|
|
res.status(500).send(Object.assign(standardError, { 'source':'agenda', 'e':e }));
|
2018-03-13 13:46:26 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-05-22 16:10:38 +00:00
|
|
|
app.get('/oldtraffic', cache('5 minutes'), (req, res) => {
|
2018-03-13 13:46:26 +00:00
|
|
|
logger.debug(req.query);
|
2018-03-25 20:43:44 +00:00
|
|
|
if (req.query.hasOwnProperty('olat'))
|
2018-03-13 13:46:26 +00:00
|
|
|
|
|
|
|
directions.getTraffic(req.query.olat, req.query.olon, req.query.dlat, req.query.dlon)
|
|
|
|
.then((d) => {
|
2018-10-17 10:50:57 +00:00
|
|
|
res.set('Cache-Control', 'public, max-age=900');
|
2018-03-13 13:46:26 +00:00
|
|
|
res.send(d);
|
|
|
|
}).catch((e) => {
|
|
|
|
logger.error(e);
|
2020-01-20 23:15:01 +00:00
|
|
|
res.status(500).send(Object.assign(standardError, { 'source':'oldtraffic', 'e':e }));
|
2018-03-13 13:46:26 +00:00
|
|
|
});
|
2018-03-25 20:43:44 +00:00
|
|
|
|
2018-03-13 13:46:26 +00:00
|
|
|
else {
|
|
|
|
// throw new Error('Weather: LL missing');
|
|
|
|
logger.warn('FS: oLat missing');
|
|
|
|
res.status(500).send('oLat Missing');
|
|
|
|
}
|
|
|
|
});
|
2018-05-22 14:38:49 +00:00
|
|
|
|
|
|
|
app.get('/incidents', cache('5 minutes'), (req, res) => {
|
|
|
|
logger.debug(req.query);
|
|
|
|
directions.getIncidents()
|
|
|
|
.then((d) => {
|
|
|
|
logger.debug(d);
|
2018-10-17 10:50:57 +00:00
|
|
|
res.set('Cache-Control', 'public, max-age=300');
|
2018-05-22 14:38:49 +00:00
|
|
|
res.send(d);
|
|
|
|
}).catch((e) => {
|
|
|
|
logger.error(e);
|
2020-01-20 23:15:01 +00:00
|
|
|
res.status(500).send(Object.assign(standardError, { 'source':'incidents', 'e':e }));
|
2018-05-22 14:38:49 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-05-22 16:10:38 +00:00
|
|
|
app.get('/traffic', cache('5 minutes'), asyncMiddleware(async (req, res, next) => {
|
2018-05-22 15:16:23 +00:00
|
|
|
logger.debug(req.query);
|
|
|
|
|
2018-05-22 16:10:38 +00:00
|
|
|
if (req.query.hasOwnProperty('olat'))
|
|
|
|
|
|
|
|
await directions.doGetEstDirectionsWithIncidents(req.query.olat, req.query.olon, req.query.dlat, req.query.dlon).then((b) => {
|
2020-01-20 23:15:01 +00:00
|
|
|
// logger.debug('b', b);
|
2018-10-17 10:50:57 +00:00
|
|
|
res.set('Cache-Control', 'public, max-age=300');
|
2018-05-22 16:10:38 +00:00
|
|
|
res.send(b);
|
|
|
|
}).catch((e) => {
|
|
|
|
logger.error(e);
|
2020-01-20 23:15:01 +00:00
|
|
|
res.status(500).send(Object.assign(standardError, { 'source':'traffic', 'e':e }));
|
2018-05-22 16:10:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
else {
|
|
|
|
// throw new Error('Weather: LL missing');
|
|
|
|
logger.warn('FS: oLat missing');
|
|
|
|
res.status(500).send('oLat Missing');
|
|
|
|
}
|
2018-05-22 15:16:23 +00:00
|
|
|
}));
|
|
|
|
|
2018-02-23 10:36:49 +00:00
|
|
|
app.listen(port, (err) => {
|
|
|
|
if (err)
|
|
|
|
return logger.error('Server error:', err);
|
|
|
|
|
|
|
|
logger.info(`Jubilee Server is listening on ${port}`);
|
|
|
|
});
|