This commit is contained in:
Jan-Lukas Else 2020-07-29 17:55:10 +02:00
parent afab7686f8
commit 00add1c312
3 changed files with 11 additions and 17 deletions

19
http.go
View File

@ -63,7 +63,7 @@ func buildHandler() (http.Handler, error) {
} else {
for _, path := range allPostPaths {
if path != "" {
r.With(TrimSlash).Get(path, servePost)
r.Get(path, servePost)
}
}
}
@ -74,7 +74,7 @@ func buildHandler() (http.Handler, error) {
} else {
for _, path := range allRedirectPaths {
if path != "" {
r.With(TrimSlash).Get(path, serveRedirect)
r.Get(path, serveRedirect)
}
}
}
@ -103,11 +103,10 @@ func (d *dynamicHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
d.realHandler.ServeHTTP(w, r)
}
func TrimSlash(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if len(r.RequestURI) > 1 {
r.RequestURI = strings.TrimSuffix(r.RequestURI, "/")
}
next.ServeHTTP(w, r)
})
}
func SlashTrimmedPath(r *http.Request) string {
path := r.URL.Path
if len(path) > 1 {
path = strings.TrimSuffix(path, "/")
}
return path
}

View File

@ -17,7 +17,7 @@ type post struct {
}
func servePost(w http.ResponseWriter, r *http.Request) {
post, err := getPost(r.RequestURI, r.Context())
post, err := getPost(SlashTrimmedPath(r), r.Context())
if err == postNotFound {
http.NotFound(w, r)
return

View File

@ -9,13 +9,8 @@ import (
var redirectNotFound = errors.New("redirect not found")
type redirect struct {
fromPath string
toPath string
}
func serveRedirect(w http.ResponseWriter, r *http.Request) {
redirect, err := getRedirect(r.RequestURI, r.Context())
redirect, err := getRedirect(SlashTrimmedPath(r), r.Context())
if err == redirectNotFound {
http.NotFound(w, r)
return