diff options
| author | Mitchell Riedstra <mitch@riedstra.dev> | 2023-01-07 13:31:23 -0500 |
|---|---|---|
| committer | Mitchell Riedstra <mitch@riedstra.dev> | 2023-01-07 13:31:23 -0500 |
| commit | ca33a035c779ae14fb6330c8801c75f49dd1bb79 (patch) | |
| tree | deaabaf15d6d91079a68f247e46070399e4343ee /cmd/server/handlers.go | |
| parent | 97dd660925434be537cd9a49a1d0c893b223e357 (diff) | |
| download | go-website-0.0.22.tar.gz go-website-0.0.22.tar.xz | |
Add an internal caching option. It performs quite well.v0.0.22
Also refactor and clean up most linter warnings.
Diffstat (limited to 'cmd/server/handlers.go')
| -rw-r--r-- | cmd/server/handlers.go | 59 |
1 files changed, 30 insertions, 29 deletions
diff --git a/cmd/server/handlers.go b/cmd/server/handlers.go index 3c767a4..bb174d8 100644 --- a/cmd/server/handlers.go +++ b/cmd/server/handlers.go @@ -6,6 +6,7 @@ import ( "path/filepath" "strings" + "riedstra.dev/mitch/go-website/mapcache" "riedstra.dev/mitch/go-website/page" "riedstra.dev/mitch/go-website/rediscache" ) @@ -28,17 +29,29 @@ func (a *App) addCacheableRoutes(r *http.ServeMux) *http.ServeMux { strings.TrimSuffix(strings.TrimPrefix(a.FeedPrefix, "/"), "/")) routes := map[string]http.Handler{ - "/_json/": a.PageJsonHandler(), + "/_json/": a.PageJSONHandler(), "/_md/": a.PageMarkdownHandler(), a.FeedPrefix: http.StripPrefix(a.FeedPrefix, a.FeedHandler()), "/": a.PageHandler(), } + if a.cache == nil { + a.cache = mapcache.New() + } + for route, handler := range routes { - if a.redisPool != nil { - r.Handle(route, rediscache.Handle(a.redisPool, a.RedisKey, - handler)) - } else { + switch { + case a.mapCache: + r.Handle(route, conditionalMiddleware( + a.IsLoggedIn, + handler, + a.cache.Handle(handler))) + case a.redisPool != nil: + r.Handle(route, conditionalMiddleware( + a.IsLoggedIn, + handler, + rediscache.Handle(a.redisPool, a.RedisKey, handler))) + default: r.Handle(route, handler) } } @@ -52,10 +65,7 @@ func (a *App) Handler() http.Handler { func (a *App) PageHandler() http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - u := r.URL.Path - if u == "/" { - u = "/index" - } + u := page.GetURLPath(r) loggedIn := a.IsLoggedIn(r) @@ -68,7 +78,7 @@ func (a *App) PageHandler() http.Handler { !loggedIn { page.Render4xx(w, r, map[string]interface{}{ "LoggedIn": loggedIn, - }, 404) + }, http.StatusNotFound) return } @@ -81,52 +91,43 @@ func (a *App) PageHandler() http.Handler { }) } -func (a *App) PageJsonHandler() http.Handler { +func (a *App) PageJSONHandler() http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - u := r.URL.Path - if u == "/" { - u = "/index" - } + u := page.GetURLPath(r) // Skip template directory if strings.HasPrefix(u[1:], filepath.Clean(page.TemplateDirectory)) { - page.Render4xx(w, r, map[string]interface{}{}, 404) + page.Render4xx(w, r, map[string]interface{}{}, http.StatusNotFound) + return } u = filepath.Join(".", u) - page.RenderJson(w, r, u, map[string]interface{}{}, 200) + page.RenderJSON(w, r, u, map[string]interface{}{}, http.StatusOK) }) } func (a *App) PageMarkdownHandler() http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - u := r.URL.Path - if u == "/" { - u = "/index" - } + u := page.GetURLPath(r) // Skip template directory if strings.HasPrefix(u[1:], filepath.Clean(page.TemplateDirectory)) { - page.Render4xx(w, r, map[string]interface{}{}, 404) + page.Render4xx(w, r, map[string]interface{}{}, http.StatusNotFound) + return } u = filepath.Join(".", u) - page.RenderMarkdown(w, r, u, map[string]interface{}{}, 200) + page.RenderMarkdown(w, r, u, map[string]interface{}{}, http.StatusOK) }) } func (a *App) RebuildIndexHandler() http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - u := r.URL.Path - if u == "/" { - u = "/index" - } - - u = filepath.Join(".", u) + u := page.GetPagePath(r) p := page.NewPage("index") err := p.RebuildIndex() |
