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

View File

@ -17,7 +17,7 @@ type post struct {
} }
func servePost(w http.ResponseWriter, r *http.Request) { 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 { if err == postNotFound {
http.NotFound(w, r) http.NotFound(w, r)
return return

View File

@ -9,13 +9,8 @@ import (
var redirectNotFound = errors.New("redirect not found") var redirectNotFound = errors.New("redirect not found")
type redirect struct {
fromPath string
toPath string
}
func serveRedirect(w http.ResponseWriter, r *http.Request) { 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 { if err == redirectNotFound {
http.NotFound(w, r) http.NotFound(w, r)
return return