From 00add1c31265adaddef7037c5053220fd64a12ff Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Wed, 29 Jul 2020 17:55:10 +0200 Subject: [PATCH] Fix --- http.go | 19 +++++++++---------- posts.go | 2 +- redirects.go | 7 +------ 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/http.go b/http.go index cc61b9f..48354ce 100644 --- a/http.go +++ b/http.go @@ -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 +} \ No newline at end of file diff --git a/posts.go b/posts.go index 1a8ae84..022f07a 100644 --- a/posts.go +++ b/posts.go @@ -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 diff --git a/redirects.go b/redirects.go index 266e524..144fb3d 100644 --- a/redirects.go +++ b/redirects.go @@ -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