overleaf/services/web/app/coffee/Features/Blog/BlogController.coffee

40 lines
1.1 KiB
CoffeeScript
Raw Normal View History

2014-07-09 18:49:39 +00:00
request = require("request")
settings = require("settings-sharelatex")
logger = require("logger-sharelatex")
2014-07-10 13:53:53 +00:00
_ = require("underscore")
ErrorController = require "../Errors/ErrorController"
2014-07-09 18:49:39 +00:00
2014-07-10 13:53:53 +00:00
extensionsToProxy = [".png", ".xml", ".jpeg", ".json", ".zip", ".eps"]
module.exports = BlogController =
2014-07-09 18:49:39 +00:00
getPage: (req, res, next)->
2014-07-10 13:53:53 +00:00
url = req.url?.toLowerCase()
blogUrl = "#{settings.apis.blog.url}#{url}"
2014-10-08 15:07:44 +00:00
extensionsToProxy = [".png", ".xml", ".jpeg", ".json", ".zip", ".eps"]
2014-07-10 13:53:53 +00:00
shouldProxy = _.find extensionsToProxy, (extension)->
url.indexOf(extension) != -1
if shouldProxy
return BlogController._directProxy blogUrl, res
2014-07-09 18:49:39 +00:00
logger.log url:url, "proxying request to blog api"
2014-07-10 13:53:53 +00:00
request.get blogUrl, (err, r, data)->
if r?.statusCode == 404
return ErrorController.notFound(req, res, next)
2014-07-10 13:53:53 +00:00
data = data.trim()
2014-07-09 18:49:39 +00:00
try
data = JSON.parse(data)
catch err
logger.err err:err, data:data, "error parsing data from data"
2014-07-10 13:53:53 +00:00
res.render "blog/blog_holder", data
getIndexPage: (req, res)->
req.url = "/blog/index.html"
BlogController.getPage req, res
2014-07-09 18:49:39 +00:00
2014-07-10 13:53:53 +00:00
_directProxy: (originUrl, res)->
request.get(originUrl).pipe res