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 e39e618
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
9 changes: 8 additions & 1 deletion pkg/account/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package account
import (
"context"
"net/http"
"strings"

"github.com/gorilla/csrf"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -94,7 +95,13 @@ func (s *server) RegisterRoutes(server *web.Server) {
csrf.FieldName("_csrf"),
csrf.Path("/"),
)
router := server.Router().NewRoute().PathPrefix(s.config.Mount).Subrouter()
prefix := "/" + strings.Trim(s.config.Mount, "/")
prefixWithSlash := prefix
if prefix != "/" {
prefixWithSlash = prefix + "/"
server.Router().Path(prefix).Handler(http.RedirectHandler(prefixWithSlash, http.StatusPermanentRedirect))
}
router := server.Prefix(prefixWithSlash).Subrouter()
router.Use(
func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
9 changes: 8 additions & 1 deletion pkg/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"net/http"
"net/url"
"strings"

"github.com/gorilla/csrf"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -130,7 +131,13 @@ 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()
prefix := "/" + strings.Trim(console.config.Mount, "/")
prefixWithSlash := prefix
if prefix != "/" {
prefixWithSlash = prefix + "/"
server.Router().Path(prefix).Handler(http.RedirectHandler(prefixWithSlash, http.StatusPermanentRedirect))
}
router := server.Prefix(prefixWithSlash).Subrouter()
router.Use(
mux.MiddlewareFunc(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
8 changes: 7 additions & 1 deletion pkg/oauth/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,13 @@ 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()
prefix := "/" + strings.Trim(s.config.Mount, "/")
prefixWithSlash := prefix
if prefix != "/" {
prefixWithSlash = prefix + "/"
server.Router().Path(prefix).Handler(http.RedirectHandler(prefixWithSlash, http.StatusPermanentRedirect))
}
router := server.Prefix(prefixWithSlash).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

0 comments on commit e39e618

Please sign in to comment.