mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
Merge pull request #666 from sharelatex/ta-smart-proxy
Replace OldAssetsProxy
This commit is contained in:
commit
c30cdf955b
4 changed files with 87 additions and 18 deletions
|
@ -1,16 +0,0 @@
|
|||
settings = require("settings-sharelatex")
|
||||
logger = require("logger-sharelatex")
|
||||
request = require("request")
|
||||
|
||||
module.exports = (req, res, next)->
|
||||
requestedUrl = req.url
|
||||
|
||||
redirectUrl = settings.proxyUrls[requestedUrl]
|
||||
if redirectUrl?
|
||||
logger.log redirectUrl:redirectUrl, reqUrl:req.url, "proxying url"
|
||||
upstream = request(redirectUrl)
|
||||
upstream.on "error", (error) ->
|
||||
logger.error err: error, "error in OldAssetProxy"
|
||||
upstream.pipe(res)
|
||||
else
|
||||
next()
|
25
services/web/app/coffee/infrastructure/ProxyManager.coffee
Normal file
25
services/web/app/coffee/infrastructure/ProxyManager.coffee
Normal file
|
@ -0,0 +1,25 @@
|
|||
settings = require("settings-sharelatex")
|
||||
logger = require("logger-sharelatex")
|
||||
httpProxy = require 'express-http-proxy'
|
||||
|
||||
module.exports =
|
||||
# add proxy for all paths listed in `settings.proxyUrls`and log errors
|
||||
apply: (app) ->
|
||||
for requestUrl, target of settings.proxyUrls
|
||||
targetUrl = @makeTargetUrl(target)
|
||||
if targetUrl?
|
||||
app.use requestUrl, httpProxy(targetUrl)
|
||||
else
|
||||
logger.error "Cannot make proxy target from #{target}"
|
||||
|
||||
# takes a 'target' and return an URL to proxy to.
|
||||
# 'target' can be:
|
||||
# - a String, representing the URL
|
||||
# - an Object with:
|
||||
# - a path attribute (String)
|
||||
# - a baseURL attribute (String)
|
||||
makeTargetUrl: (target) ->
|
||||
return target if typeof target is 'string'
|
||||
return target.path unless target.baseUrl?
|
||||
"#{target.baseUrl}#{target.path or ''}"
|
||||
|
|
@ -32,7 +32,7 @@ Mongoose = require("./Mongoose")
|
|||
oneDayInMilliseconds = 86400000
|
||||
ReferalConnect = require('../Features/Referal/ReferalConnect')
|
||||
RedirectManager = require("./RedirectManager")
|
||||
OldAssetProxy = require("./OldAssetProxy")
|
||||
ProxyManager = require("./ProxyManager")
|
||||
translations = require("translations-sharelatex").setup(Settings.i18n)
|
||||
Modules = require "./Modules"
|
||||
|
||||
|
@ -74,7 +74,7 @@ app.use methodOverride()
|
|||
|
||||
app.use metrics.http.monitor(logger)
|
||||
app.use RedirectManager
|
||||
app.use OldAssetProxy
|
||||
ProxyManager.apply(app)
|
||||
|
||||
|
||||
webRouter.use cookieParser(Settings.security.sessionSecret)
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
sinon = require('sinon')
|
||||
chai = require('chai')
|
||||
should = chai.should()
|
||||
expect = chai.expect
|
||||
modulePath = '../../../../app/js/infrastructure/ProxyManager'
|
||||
SandboxedModule = require('sandboxed-module')
|
||||
MockRequest = require "../helpers/MockRequest"
|
||||
MockResponse = require "../helpers/MockResponse"
|
||||
|
||||
describe "ProxyManager", ->
|
||||
before ->
|
||||
@settings = proxyUrls: {}
|
||||
@httpProxy = sinon.stub()
|
||||
@proxyManager = SandboxedModule.require modulePath, requires:
|
||||
'settings-sharelatex': @settings
|
||||
'logger-sharelatex':
|
||||
error: ->
|
||||
log: -> console.log(arguments)
|
||||
'httpProxy': @httpProxy
|
||||
|
||||
describe 'apply', ->
|
||||
before ->
|
||||
@sandbox = sinon.sandbox.create()
|
||||
@app = use: sinon.stub()
|
||||
@sandbox.stub(@proxyManager, 'makeTargetUrl')
|
||||
|
||||
after ->
|
||||
@sandbox.restore()
|
||||
|
||||
beforeEach ->
|
||||
@app.use.reset()
|
||||
@requestUrl = '/foo/bar'
|
||||
@settings.proxyUrls[@requestUrl] = 'http://whatever'
|
||||
|
||||
it 'sets routes', ->
|
||||
@proxyManager.makeTargetUrl.returns('http://whatever')
|
||||
@proxyManager.apply(@app)
|
||||
@app.use.calledWith(@requestUrl).should.equal true
|
||||
|
||||
it 'logs errors', ->
|
||||
@proxyManager.makeTargetUrl.returns(null)
|
||||
@proxyManager.apply(@app)
|
||||
@app.use.called.should.equal false
|
||||
|
||||
describe 'makeTargetUrl', ->
|
||||
it 'returns Strings', ->
|
||||
target = 'http://whatever'
|
||||
@proxyManager.makeTargetUrl(target).should.equal target
|
||||
|
||||
it 'makes with path', ->
|
||||
target = path: 'baz'
|
||||
@proxyManager.makeTargetUrl(target).should.equal 'baz'
|
||||
|
||||
it 'makes with baseUrl', ->
|
||||
target = baseUrl: 'foo.bar'
|
||||
@proxyManager.makeTargetUrl(target).should.equal 'foo.bar'
|
||||
|
||||
it 'makes with baseUrl and path', ->
|
||||
target = path: 'baz', baseUrl: 'foo.bar/'
|
||||
@proxyManager.makeTargetUrl(target).should.equal 'foo.bar/baz'
|
Loading…
Reference in a new issue