Use mux.Match to find if route is already used

This commit is contained in:
Jan-Lukas Else 2020-09-21 16:10:00 +02:00
parent fb6b4f65ac
commit 2a9502defb
2 changed files with 2 additions and 20 deletions

11
http.go
View File

@ -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

View File

@ -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), "") {