mirror of https://github.com/jlelse/GoBlog
Fix
This commit is contained in:
parent
afab7686f8
commit
00add1c312
19
http.go
19
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
|
||||
}
|
2
posts.go
2
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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue