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 {
|
} 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
|
||||||
})
|
}
|
||||||
}
|
|
2
posts.go
2
posts.go
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue