Selaa lähdekoodia

added redirects from config file in, used for old template paths

Henry Oswald 12 vuotta sitten
vanhempi
sitoutus
978e3262b5

+ 3 - 1
services/web/app/coffee/Features/Templates/TemplatesMiddlewear.coffee

@@ -1,4 +1,6 @@
 settings = require("settings-sharelatex")
+logger = require("logger-sharelatex")
+
 
 module.exports =
 	saveTemplateDataInSession: (req, res, next)->
@@ -20,4 +22,4 @@ module.exports =
 
 	insert_templates_user_id: (req, res, next)->
 		req.params.user_id = settings.apis.templates_api.user_id
-		next()
+		next()

+ 20 - 0
services/web/app/coffee/infrastructure/RedirectManager.coffee

@@ -0,0 +1,20 @@
+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()
+

+ 2 - 0
services/web/app/coffee/infrastructure/Server.coffee

@@ -16,6 +16,7 @@ sessionStore = new RedisStore(host:Settings.redis.web.host, port:Settings.redis.
 cookieParser = express.cookieParser(Settings.security.sessionSecret)
 oneDayInMilliseconds = 86400000
 ReferalConnect = require('../Features/Referal/ReferalConnect')
+RedirectManager = require("./RedirectManager")
 
 metrics.mongodb.monitor(Path.resolve(__dirname + "/../../../node_modules/mongojs/node_modules/mongodb"), logger)
 metrics.mongodb.monitor(Path.resolve(__dirname + "/../../../node_modules/mongoose/node_modules/mongodb"), logger)
@@ -74,6 +75,7 @@ app.configure 'production', ->
 	app.enable('view cache')
 
 app.use metrics.http.monitor(logger)
+app.use RedirectManager
 
 app.use (req, res, next)->
 	metrics.inc "http-request"

+ 2 - 5
services/web/config/settings.defaults.coffee

@@ -264,11 +264,8 @@ module.exports =
 				url: "/logout"
 			}]
 		}]
-
-
-	algolia:
-		appId:"SK53GL4JLY"
-		secret:"75dc5e65794cd47eb7f725e6bb5075be"
 	
 
+	redirects:
+		"/templates/index": "/templates/"
 

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

@@ -0,0 +1,47 @@
+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
+