ソースを参照

Add additional functionality to RedirectManager

James Allen 8 年 前
親
コミット
40f08d1592

+ 23 - 16
services/web/app/coffee/infrastructure/RedirectManager.coffee

@@ -1,20 +1,27 @@
 settings = require("settings-sharelatex")
 logger = require("logger-sharelatex")
 
-module.exports = (req, res, next)->
-	
-	requestedUrl = req.url
-
-	redirectUrl = settings.redirects[requestedUrl]
-
-	#remove starting slash
-	if !redirectUrl? and requestedUrl[requestedUrl.length-1] == "/"
-		requestedUrl = requestedUrl.substring(0, requestedUrl.length - 1)
-		redirectUrl = settings.redirects[requestedUrl]
-
-	if redirectUrl?
-		logger.log redirectUrl:redirectUrl, reqUrl:req.url, "redirecting to new path"
-		res.redirect 301, "#{redirectUrl}"
-	else
-		next()
+module.exports = RedirectManager =
+	apply: (webRouter) ->
+		for redirectUrl, target of settings.redirects
+			do (target) ->
+				method = target.method || 'get'
+				webRouter[method] redirectUrl, RedirectManager.createRedirect(target)
 
+	createRedirect: (target) ->
+		(req, res, next) ->
+			code = 302
+			if typeof target is 'string'
+				url = target
+			else
+				if target.method == "post"
+					code = 307
+				if typeof target.url == "function"
+					url = target.url(req.params)
+					if !url
+						return next()
+				else
+					url = target.url
+				if target.baseUrl?
+					url = "#{target.baseUrl}#{url}"
+			res.redirect code, url

+ 1 - 1
services/web/app/coffee/infrastructure/Server.coffee

@@ -73,7 +73,7 @@ app.use multer(dest: Settings.path.uploadFolder)
 app.use methodOverride()
 
 app.use metrics.http.monitor(logger)
-app.use RedirectManager
+RedirectManager.apply(webRouter)
 ProxyManager.apply(publicApiRouter)
 
 

+ 0 - 47
services/web/test/unit/coffee/infrastructure/RedirectManagerTests.coffee

@@ -1,47 +0,0 @@
-sinon = require('sinon')
-chai = require('chai')
-should = chai.should()
-expect = chai.expect
-path = require("path")
-modulePath = "../../../../app/js/infrastructure/RedirectManager.js"
-SandboxedModule = require('sandboxed-module')
-
-describe "redirectToNewTemplate", ->
-	beforeEach ->
-		@settings = 
-			redirects: 
-				"/path/here/" : "/path/elsewhere/"
-				"/no/trailing/slash":"/no/trailing/slash/elsewhere"
-				"/part/path": "/diff/part/path"
-			mountPointUrl:"/here"
-		@redirectManager = SandboxedModule.require modulePath, requires:
-			"settings-sharelatex":@settings
-			"logger-sharelatex":
-				log:->
-				err:->
-		@res = 
-			redirect: sinon.stub()
-		@req = {}
-
-	describe "redirect", ->
-
-		it "should perminant redirect if url matches redirect", ()->
-			@req.url = "/path/here/"
-			nextStub = sinon.stub()
-			@redirectManager @req, @res, nextStub
-			@res.redirect.calledWith(301, "/path/elsewhere/").should.equal true
-			nextStub.called.should.equal false
-				
-		it "should not redirect on non matching url", (done)->
-			@req.url = "non/matching/"
-			@redirectManager @req, @res, =>
-				@res.redirect.called.should.equal false
-				done()
-
-		it "should ignore slash at end of url", ->
-			@req.url = "/no/trailing/slash/"
-			nextStub = sinon.stub()
-			@redirectManager @req, @res, nextStub
-			@res.redirect.calledWith(301, "/no/trailing/slash/elsewhere").should.equal true
-			nextStub.called.should.equal false
-