mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-27 10:52:01 +00:00
afc22dc5c2
This reverts commit ebefc2f28c6e88bbfa632f2b22cb8b99b75e95ec, reversing changes made to 223beab491d2f8dcf4c7285267fda355cfce3f05.
40 lines
1.3 KiB
CoffeeScript
40 lines
1.3 KiB
CoffeeScript
settings = require 'settings-sharelatex'
|
|
logger = require 'logger-sharelatex'
|
|
request = require 'request'
|
|
URL = require 'url'
|
|
|
|
module.exports = ProxyManager =
|
|
apply: (publicApiRouter) ->
|
|
for proxyUrl, target of settings.proxyUrls
|
|
do (target) ->
|
|
publicApiRouter.get proxyUrl, ProxyManager.createProxy(target)
|
|
|
|
createProxy: (target) ->
|
|
(req, res, next) ->
|
|
targetUrl = makeTargetUrl(target, req)
|
|
logger.log targetUrl: targetUrl, reqUrl: req.url, "proxying url"
|
|
|
|
upstream = request(targetUrl)
|
|
upstream.on "error", (error) ->
|
|
logger.error err: error, "error in ProxyManager"
|
|
|
|
# TODO: better handling of status code
|
|
# see https://github.com/overleaf/write_latex/wiki/Streams-and-pipes-in-Node.js
|
|
upstream.pipe(res)
|
|
|
|
# make a URL from a proxy target.
|
|
# if the query is specified, set/replace the target's query with the given query
|
|
makeTargetUrl = (target, req) ->
|
|
targetUrl = URL.parse(parseSettingUrl(target, req))
|
|
if req.query? and Object.keys(req.query).length > 0
|
|
targetUrl.query = req.query
|
|
targetUrl.search = null # clear `search` as it takes precedence over `query`
|
|
targetUrl.format()
|
|
|
|
parseSettingUrl = (target, { params }) ->
|
|
return target if typeof target is 'string'
|
|
if typeof target.path is 'function'
|
|
path = target.path(params)
|
|
else
|
|
path = target.path
|
|
"#{target.baseUrl}#{path or ''}"
|