mirror of https://github.com/jlelse/GoBlog
Use mux.Match to find if route is already used
This commit is contained in:
parent
fb6b4f65ac
commit
2a9502defb
11
http.go
11
http.go
|
@ -136,13 +136,13 @@ func buildHandler() (http.Handler, error) {
|
|||
// Blog
|
||||
rootPath := "/"
|
||||
blogPath := "/blog"
|
||||
if routePatterns := routesToStringSlice(r.Routes()); !routePatterns.has(rootPath) {
|
||||
if !r.Match(chi.NewRouteContext(), http.MethodGet, rootPath) {
|
||||
r.With(cacheMiddleware, minifier.Middleware).Get(rootPath, serveHome("", NONE))
|
||||
r.With(cacheMiddleware, minifier.Middleware).Get(rootPath+rssPath, serveHome("", RSS))
|
||||
r.With(cacheMiddleware, minifier.Middleware).Get(rootPath+jsonPath, serveHome("", JSON))
|
||||
r.With(cacheMiddleware, minifier.Middleware).Get(rootPath+atomPath, serveHome("", ATOM))
|
||||
r.With(cacheMiddleware, minifier.Middleware).Get(paginationPath, serveHome("", NONE))
|
||||
} else if !routePatterns.has(blogPath) {
|
||||
} else if !r.Match(chi.NewRouteContext(), http.MethodGet, blogPath) {
|
||||
r.With(cacheMiddleware, minifier.Middleware).Get(blogPath, serveHome(blogPath, NONE))
|
||||
r.With(cacheMiddleware, minifier.Middleware).Get(blogPath+rssPath, serveHome(blogPath, RSS))
|
||||
r.With(cacheMiddleware, minifier.Middleware).Get(blogPath+jsonPath, serveHome(blogPath, JSON))
|
||||
|
@ -155,13 +155,6 @@ func buildHandler() (http.Handler, error) {
|
|||
return r, nil
|
||||
}
|
||||
|
||||
func routesToStringSlice(routes []chi.Route) (ss stringSlice) {
|
||||
for _, r := range routes {
|
||||
ss = append(ss, r.Pattern)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
type dynamicHandler struct {
|
||||
realHandler http.Handler
|
||||
changeMutex *sync.Mutex
|
||||
|
|
11
utils.go
11
utils.go
|
@ -5,17 +5,6 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
type stringSlice []string
|
||||
|
||||
func (s stringSlice) has(value string) bool {
|
||||
for _, v := range s {
|
||||
if v == value {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func urlize(str string) string {
|
||||
newStr := ""
|
||||
for _, c := range strings.Split(strings.ToLower(str), "") {
|
||||
|
|
Loading…
Reference in New Issue