jubilee/server.js

133 lines
3.3 KiB
JavaScript
Raw Normal View History

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-02-23 10:36:49 +00:00
logger.level = 'debug';
const app = express();
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;
app.use(express.static(path.join(__dirname, sitePath)));
app.get('/weather', cache('15 minutes'), (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) => {
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) => {
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) => {
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) => {
2018-02-23 10:36:49 +00:00
if (req.query.hasOwnProperty('ll'))
foursquare.doGetFourSquareExplore(req.query.ll)
.then((d) => {
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');
}
});
2018-02-26 16:56:14 +00:00
app.get('/rightbyme', cache('12 hour'), (req, res) => {
if (req.query.hasOwnProperty('ll'))
rightbyme.doGetRightByMe(req.query.ll)
.then((d) => {
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) => {
2018-02-23 10:36:49 +00:00
euronews.getEuroNews().then((d) => {
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!');
}
2018-02-23 10:36:49 +00:00
});
});
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.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');
}
});
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}`);
});