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")
|
2014-07-23 11:16:49 +00:00
|
|
|
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
|
|
|
|
2014-07-23 11:16:49 +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)->
|
2014-07-23 11:16:49 +00:00
|
|
|
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
|