aboutsummaryrefslogtreecommitdiff
path: root/cmd/server/handlers.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/server/handlers.go')
-rw-r--r--cmd/server/handlers.go59
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()