-
-
Save itrelease/f32cff9a8c9998b5e728 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var path = require('path'); | |
var express = require('express'); | |
var nunjucks = require('nunjucks'); | |
var logger = require('bunyan').createLogger({ name: 'mdz:web' }); | |
module.exports = function (redisClient, config, development) { | |
'use strict'; | |
var HOST = process.env.FRONT_HOST || 'meduza.io'; | |
var DEST_ROOT = development ? config.DEV_DEST_ROOT : config.DIST_DEST_ROOT; | |
var web = express.Router(); | |
nunjucks.configure(path.join(__dirname, '../..', DEST_ROOT), { | |
tags: { | |
blockStart: '<%', | |
blockEnd: '%>', | |
variableStart: '%%', | |
variableEnd: '%%', | |
commentStart: '<#', | |
commentEnd: '#>' | |
} | |
}); | |
web.get('*', function (req, res, next) { | |
res.redisURLKey = config.REDIS_KEY + req.url.slice(1); | |
logger.info(req.url); | |
next(); | |
}); | |
web.get(['/feed/*', '/rss/*'], function (req, res, next) { | |
res.set({ 'Content-Type': 'application/rss+xml; charset=ISO-8859-1' }); | |
res.onError = function (res) { | |
res.status(404).send(''); | |
}; | |
next(); | |
}); | |
web.get('/jsonfeed/mailru/all', function (req, res, next) { | |
res.set({ 'Content-Type': 'application/json' }); | |
res.onError = function (res) { | |
res.status(404).send(JSON.stringify({ error: 'Not found' })); | |
}; | |
next(); | |
}); | |
web.get('/sitemap/google/news.xml', function (req, res, next) { | |
res.set({ 'Content-Type': 'application/xml' }); | |
res.onError = function (res) { | |
res.status(404).send(''); | |
}; | |
next(); | |
}); | |
web.get('/readability/check/*', function (req, res) { | |
res.set({ 'Content-Type': 'application/json' }); | |
redisClient.get(res.redisURLKey, function (err, data) { | |
if (err || !data) { | |
data = { 'is_readable': false }; | |
} | |
res.send(data); | |
}); | |
}); | |
web.get('/readability/*', function (req, res, next) { | |
res.set({ 'Content-Type': 'application/json' }); | |
res.onError = function (res) { | |
res.status(404).send(JSON.stringify({ error: 'Not found' })); | |
}; | |
next(); | |
}); | |
web.get([ | |
'/feed/*', | |
'/rss/*', | |
'/jsonfeed/mailru/all', | |
'/sitemap/google/news.xml', | |
'/readability/check/*', | |
'/readability/*' | |
], function (req, res) { | |
redisClient.get(res.redisURLKey, function (err, data) { | |
if (err || !data) { | |
logger.warn({ | |
error: { | |
key: res.redisURLKey, | |
msg: err | |
} | |
}, req.url); | |
if (res.onError) { | |
res.onError(res); | |
} else { | |
res.status(404).send('404'); | |
} | |
return; | |
} | |
res.send(data); | |
}); | |
}); | |
// Check `wtf` param in query string | |
// if presence then fetch og tags | |
// and pass them for next middleware | |
web.get('/live/*', function (req, res, next) { | |
// 19061 | |
if (!req.param('wtf')) { | |
next(); | |
return; | |
} | |
var key = config.REDIS_KEY + 'wtf/og/' + req.param('wtf'); | |
redisClient.get(key, function (err, data) { | |
if (err || !data) { | |
logger.warn({ | |
error: { | |
key: key, msg: err | |
} | |
}, req.url); | |
next(); | |
return; | |
} | |
res.wtfOG = JSON.parse(data); | |
next(); | |
}); | |
}); | |
web.get([ | |
/\/news\/\w+/, | |
/\/feature\/\w+/, | |
/\/shapito\/\w+/, | |
/\/fun\/\w+/, | |
/\/galleries\/\w+/, | |
/\/cards\/\w+/, | |
/\/quiz\/\w+/, | |
/\/live\/\w+/ | |
], function (req, res) { | |
var key = config.REDIS_KEY + 'api/' + config.API_VERSION + req.url; | |
redisClient.get(key, function (err, data) { | |
if (err || !data) { | |
logger.warn({ | |
error: { | |
key: key, | |
msg: err | |
} | |
}, req.url); | |
res.status(404).send('404'); | |
return; | |
} | |
var json = JSON.parse(data); | |
var context = { | |
title: json.root.title | |
}; | |
nunjucks.render('index.html', context, function (err, data) { | |
if (err) { | |
logger.warn({ | |
error: { | |
url: req.url, | |
msg: err | |
} | |
}, 'Render view error'); | |
res.status(503).send('503'); | |
return; | |
} | |
res.send(data); | |
}); | |
}); | |
}); | |
web.get('/fun', function (req, res) { | |
res.set({ 'Location': 'https://' + HOST + '/shapito' }); | |
res.status(301).send(''); | |
}); | |
web.get([ | |
'/', | |
'/cards', | |
'/articles', | |
'/shapito' | |
], function (req, res) { | |
var context = { | |
title: 'Meduza' | |
}; | |
nunjucks.render('index.html', context, function (err, data) { | |
if (err) { | |
logger.warn({ | |
error: { | |
url: req.url, | |
msg: err | |
} | |
}, 'Render view error'); | |
res.status(503).send('503'); | |
return; | |
} | |
res.send(data); | |
}); | |
}); | |
web.get('*', function (req, res) { | |
logger.warn({ | |
error: { | |
url: req.url | |
} | |
}, 'Unknown route'); | |
res.status(404).send('404'); | |
}); | |
return web; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment