jubilee/server.js
2018-10-17 11:50:57 +01:00

259 lines
7.1 KiB
JavaScript

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');
const rightbyme = require('./server/RightByMe');
const agenda = require('./server/agenda');
const directions = require('./server/directions');
logger.level = 'debug';
const app = express();
const port = process.env.PORT || 8110;
const sitePath = 'live';
apicache.options({ 'debug': true });
const cache = apicache.middleware;
app.use(express.static(path.join(__dirname, sitePath)));
// app.get('/weather', cache('15 minutes'), (req, res) => {
const asyncMiddleware = fn =>
(req, res, next) => {
Promise.resolve(fn(req, res, next))
.catch(next);
};
app.get('/weather', (req, res) => {
if (req.query.hasOwnProperty('ll'))
weather.doGetOpenWeather(req.query.ll)
.then((d) => {
res.set('Cache-Control', 'public, max-age=1800');
res.send(d);
}).catch((e) => {
logger.error(e);
res.status(500).send('There was an error!');
});
else {
// throw new Error('Weather: LL missing');
logger.warn('Weather: LL missing');
res.status(500).send('LL Missing');
}
});
// app.get('/forecast', cache('15 minutes'), (req, res) => {
app.get('/forecast', (req, res) => {
logger.info('/forecast');
if (req.query.hasOwnProperty('ll'))
weather.doGetFullForcast(req.query.ll)
.then((d) => {
res.set('Cache-Control', 'public, max-age=1800');
res.send(d);
}).catch((e) => {
logger.error(e);
res.status(500).send('There was an error!');
});
else {
// throw new Error('Weather: LL missing');
logger.warn('Weather: LL missing');
res.status(500).send('LL Missing');
}
});
app.get('/weatheralert', cache('15 minutes'), (req, res) => {
if (req.query.hasOwnProperty('ll'))
// weather.doGetOpenWeather(req.query.ll)
// doGetDarkSkyWeather
weather.doGetDarkSkyWeather(req.query.ll)
.then((d) => {
res.set('Cache-Control', 'public, max-age=1800');
res.send(d);
}).catch((e) => {
logger.error(e);
res.status(500).send('There was an error!');
});
else {
// throw new Error('Weather: LL missing');
logger.warn('Weather: LL missing');
res.status(500).send('LL Missing');
}
});
app.get('/fsexplore', cache('15 minutes'), (req, res) => {
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';
const query = req.query.hasOwnProperty('query') ? req.query.query : '';
console.log('req.query',req.query);
foursquare.doGetFourSquareExplore(ll, limit, section, query)
.then((d) => {
res.set('Cache-Control', 'public, max-age=900');
res.send(d);
}).catch((e) => {
logger.error(e);
res.status(500).send('There was an error!');
});
}
else {
// throw new Error('Weather: LL missing');
logger.warn('FS: LL missing');
res.status(500).send('LL Missing');
}
});
app.get('/rightbyme', cache('86400 seconds'), (req, res) => {
if (req.query.hasOwnProperty('ll'))
rightbyme.doGetRightByMe(req.query.ll)
.then((d) => {
res.set('Cache-Control', 'public, max-age=86400');
res.send(d);
}).catch((e) => {
logger.error(e);
res.status(500).send('There was an error!');
});
else {
// throw new Error('Weather: LL missing');
logger.warn('FS: LL missing');
res.status(500).send('LL Missing');
}
});
app.get('/nearbydetail', cache('15 minutes'), (req, res) => {
if (req.query.hasOwnProperty('id'))
rightbyme.doGetMoreDetail(req.query.id)
.then((d) => {
res.set('Cache-Control', 'public, max-age=900');
res.send(d);
}).catch((e) => {
logger.error(e);
res.status(500).send('There was an error!');
});
else {
// throw new Error('Weather: LL missing');
logger.warn('FS: LL missing');
res.status(500).send('LL Missing');
}
});
app.get('/news', cache('15 minutes'), (req, res) => {
euronews.getEuroNews().then((d) => {
res.set('Cache-Control', 'public, max-age=1800');
res.send(d);
}).catch((e) => {
if (e.message === '304:Not changed') {
logger.info('Euronews ', e.message);
res.status(304).send();
}
else {
logger.error(e);
res.status(500).send('There was an error!');
}
});
});
app.get('/article', cache('1 hour'), (req, res) => {
if (req.query.hasOwnProperty('guid')) {
logger.debug('Beofre', req.query.guid);
euronews.getArticle(req.query.guid)
.then((d) => {
res.set('Cache-Control', 'public, max-age=3600');
res.send(d);
}).catch((e) => {
logger.error(e);
res.status(500).send('There was an error!');
});
}
else {
// throw new Error('Weather: LL missing');
logger.warn('FS: GUID missing');
res.status(500).send('GUID Missing');
}
});
app.get('/agenda', cache('15 minutes'), (req, res) => {
agenda.doTodaysAgenda()
.then((d) => {
res.set('Cache-Control', 'public, max-age=900');
res.send(d);
}).catch((e) => {
logger.error(e);
res.status(500).send('There was an error!');
});
});
app.get('/oldtraffic', cache('5 minutes'), (req, res) => {
logger.debug(req.query);
if (req.query.hasOwnProperty('olat'))
directions.getTraffic(req.query.olat, req.query.olon, req.query.dlat, req.query.dlon)
.then((d) => {
res.set('Cache-Control', 'public, max-age=900');
res.send(d);
}).catch((e) => {
logger.error(e);
res.status(500).send('There was an error!');
});
else {
// throw new Error('Weather: LL missing');
logger.warn('FS: oLat missing');
res.status(500).send('oLat Missing');
}
});
app.get('/incidents', cache('5 minutes'), (req, res) => {
logger.debug(req.query);
directions.getIncidents()
.then((d) => {
logger.debug(d);
res.set('Cache-Control', 'public, max-age=300');
res.send(d);
}).catch((e) => {
logger.error(e);
res.status(500).send('directions.getIncidents: There was an error!');
});
});
app.get('/traffic', cache('5 minutes'), asyncMiddleware(async (req, res, next) => {
logger.debug(req.query);
if (req.query.hasOwnProperty('olat'))
await directions.doGetEstDirectionsWithIncidents(req.query.olat, req.query.olon, req.query.dlat, req.query.dlon).then((b) => {
//logger.debug('b', b);
res.set('Cache-Control', 'public, max-age=300');
res.send(b);
}).catch((e) => {
logger.error(e);
res.status(500).send('There was an error!');
});
else {
// throw new Error('Weather: LL missing');
logger.warn('FS: oLat missing');
res.status(500).send('oLat Missing');
}
}));
app.listen(port, (err) => {
if (err)
return logger.error('Server error:', err);
logger.info(`Jubilee Server is listening on ${port}`);
});