Skip to content

Commit

Permalink
all: Redirect to slash
Browse files Browse the repository at this point in the history
  • Loading branch information
adriansmares committed Nov 29, 2021
1 parent a78cf47 commit f005f6d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/account/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (s *server) RegisterRoutes(server *web.Server) {
csrf.FieldName("_csrf"),
csrf.Path("/"),
)
router := server.Router().NewRoute().PathPrefix(s.config.Mount).Subrouter()
router := server.PrefixWithRedirect(s.config.Mount).Subrouter()
router.Use(
func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func generateConsoleCSPString(config *Config, nonce string) string {

// RegisterRoutes implements web.Registerer. It registers the Console to the web server.
func (console *Console) RegisterRoutes(server *web.Server) {
router := server.Router().NewRoute().PathPrefix(console.config.Mount).Subrouter()
router := server.PrefixWithRedirect(console.config.Mount).Subrouter()
router.Use(
mux.MiddlewareFunc(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/oauth/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func (s *server) output(w http.ResponseWriter, r *http.Request, resp *osin.Respo
}

func (s *server) RegisterRoutes(server *web.Server) {
router := server.Router().NewRoute().PathPrefix(s.config.Mount).Subrouter()
router := server.PrefixWithRedirect(s.config.Mount).Subrouter()
router.Use(
func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
4 changes: 1 addition & 3 deletions pkg/web/redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ type redirector struct {
}

func (red redirector) RegisterRoutes(s *Server) {
s.RootRouter().Path(red.Path).HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, red.Location, red.Code)
})
s.RootRouter().Path(red.Path).Handler(http.RedirectHandler(red.Location, red.Code))
}

// Redirect returns a Registerer that redirects requests to the given path to
Expand Down
13 changes: 13 additions & 0 deletions pkg/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,16 @@ func (s *Server) Static(prefix string, fs http.FileSystem) {
func (s *Server) Prefix(prefix string) *mux.Route {
return s.getRouter(prefix).PathPrefix(prefix)
}

// PrefixWithRedirect will create a route ending in slash.
// Paths which coincide with the route, but do not end with slash, will be
// redirect to the slash ending route.
func (s *Server) PrefixWithRedirect(prefix string) *mux.Route {
prefix = "/" + strings.Trim(prefix, "/")
prefixWithSlash := prefix
if prefix != "/" {
prefixWithSlash = prefix + "/"
s.getRouter(prefix).Path(prefix).Handler(http.RedirectHandler(prefixWithSlash, http.StatusPermanentRedirect))
}
return s.getRouter(prefixWithSlash).PathPrefix(prefixWithSlash)
}

0 comments on commit f005f6d

Please sign in to comment.