spee.ch/routes/show-routes.js
2017-06-29 22:26:29 -07:00

54 lines
2 KiB
JavaScript

const logger = require('winston');
const errorHandlers = require('../helpers/libraries/errorHandlers.js');
const { getAllClaims } = require('../controllers/showController.js');
const { getStatsSummary, postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js');
function sendAnalyticsAndLog (ip, originalUrl) {
// google analytics
sendGoogleAnalytics('show', ip, originalUrl);
// logging
logger.verbose(`POST request on ${originalUrl} from ${ip}`);
// get and serve the content
}
module.exports = (app) => {
// route to show the meme-fodder meme maker
app.get('/meme-fodder/play', ({ ip, originalUrl }, res) => {
sendAnalyticsAndLog(ip, originalUrl);
// get and render the content
getAllClaims('meme-fodder')
.then(orderedFreePublicClaims => {
postToStats('show', originalUrl, ip, 'success');
res.status(200).render('memeFodder', { claims: orderedFreePublicClaims });
})
.catch(error => {
errorHandlers.handleRequestError('show', originalUrl, ip, error, res);
});
});
// route to show statistics for spee.ch
app.get('/stats', ({ ip, originalUrl }, res) => {
sendAnalyticsAndLog(ip, originalUrl);
// get and render the content
getStatsSummary()
.then(result => {
postToStats('show', originalUrl, ip, 'success');
res.status(200).render('statistics', result);
})
.catch(error => {
errorHandlers.handleRequestError(error, res);
});
});
// route to display all free public claims at a given name
app.get('/:name/all', ({ ip, originalUrl, params }, res) => {
sendAnalyticsAndLog(ip, originalUrl);
// get and render the content
getAllClaims(params.name)
.then(orderedFreePublicClaims => {
postToStats('show', originalUrl, ip, 'success');
res.status(200).render('allClaims', { claims: orderedFreePublicClaims });
})
.catch(error => {
errorHandlers.handleRequestError('show', originalUrl, ip, error, res);
});
});
};