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
|
// Blog
|
||||||
rootPath := "/"
|
rootPath := "/"
|
||||||
blogPath := "/blog"
|
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, serveHome("", NONE))
|
||||||
r.With(cacheMiddleware, minifier.Middleware).Get(rootPath+rssPath, serveHome("", RSS))
|
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+jsonPath, serveHome("", JSON))
|
||||||
r.With(cacheMiddleware, minifier.Middleware).Get(rootPath+atomPath, serveHome("", ATOM))
|
r.With(cacheMiddleware, minifier.Middleware).Get(rootPath+atomPath, serveHome("", ATOM))
|
||||||
r.With(cacheMiddleware, minifier.Middleware).Get(paginationPath, serveHome("", NONE))
|
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, serveHome(blogPath, NONE))
|
||||||
r.With(cacheMiddleware, minifier.Middleware).Get(blogPath+rssPath, serveHome(blogPath, RSS))
|
r.With(cacheMiddleware, minifier.Middleware).Get(blogPath+rssPath, serveHome(blogPath, RSS))
|
||||||
r.With(cacheMiddleware, minifier.Middleware).Get(blogPath+jsonPath, serveHome(blogPath, JSON))
|
r.With(cacheMiddleware, minifier.Middleware).Get(blogPath+jsonPath, serveHome(blogPath, JSON))
|
||||||
|
@ -155,13 +155,6 @@ func buildHandler() (http.Handler, error) {
|
||||||
return r, nil
|
return r, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func routesToStringSlice(routes []chi.Route) (ss stringSlice) {
|
|
||||||
for _, r := range routes {
|
|
||||||
ss = append(ss, r.Pattern)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
type dynamicHandler struct {
|
type dynamicHandler struct {
|
||||||
realHandler http.Handler
|
realHandler http.Handler
|
||||||
changeMutex *sync.Mutex
|
changeMutex *sync.Mutex
|
||||||
|
|
11
utils.go
11
utils.go
|
@ -5,17 +5,6 @@ import (
|
||||||
"strings"
|
"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 {
|
func urlize(str string) string {
|
||||||
newStr := ""
|
newStr := ""
|
||||||
for _, c := range strings.Split(strings.ToLower(str), "") {
|
for _, c := range strings.Split(strings.ToLower(str), "") {
|
||||||
|
|
Loading…
Reference in New Issue