diff options
| author | Mitchell Riedstra <mitch@riedstra.dev> | 2021-10-24 12:52:38 -0400 |
|---|---|---|
| committer | Mitchell Riedstra <mitch@riedstra.dev> | 2021-10-24 12:52:38 -0400 |
| commit | 268fcf7e6b671d4959a12111d5abf553bf0a201b (patch) | |
| tree | 6f5c46c9a765478eaae78d66f4b9aefc953bbd1a /cmd/server/handlers.go | |
| parent | d36b6a55c8fa4400cd39de717443e110e990a8a3 (diff) | |
| download | go-website-268fcf7e6b671d4959a12111d5abf553bf0a201b.tar.gz go-website-268fcf7e6b671d4959a12111d5abf553bf0a201b.tar.xz | |
Redis caching. Linter config and cleanup.
Diffstat (limited to 'cmd/server/handlers.go')
| -rw-r--r-- | cmd/server/handlers.go | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/cmd/server/handlers.go b/cmd/server/handlers.go index cb63774..869e5ba 100644 --- a/cmd/server/handlers.go +++ b/cmd/server/handlers.go @@ -7,14 +7,23 @@ import ( "github.com/gorilla/mux" "riedstra.dev/mitch/go-website/page" + "riedstra.dev/mitch/go-website/rediscache" ) func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request) { rtr := mux.NewRouter() rtr.HandleFunc(a.ReIndexPath, a.RebuildIndexHandler) rtr.PathPrefix("/static/").Handler(a.StaticHandler()) - rtr.PathPrefix(fmt.Sprintf("/%s/{tag}", a.FeedPrefix)).HandlerFunc(a.FeedHandler) - rtr.PathPrefix("/").HandlerFunc(a.PageHandler) + rtr.PathPrefix(fmt.Sprintf("/%s/{tag}", a.FeedPrefix)).HandlerFunc( + a.FeedHandler) + + if a.redisPool != nil { + rtr.PathPrefix("/").Handler(rediscache.Handle( + a.redisPool, http.HandlerFunc(a.PageHandler))) + } else { + rtr.PathPrefix("/").Handler(http.HandlerFunc(a.PageHandler)) + } + rtr.ServeHTTP(w, r) } @@ -23,6 +32,7 @@ func (a *App) PageHandler(w http.ResponseWriter, r *http.Request) { if u == "/" { u = "/index" } + u = filepath.Join(".", u) page.RenderForPath(w, r, u) @@ -33,6 +43,7 @@ func (a *App) RebuildIndexHandler(w http.ResponseWriter, r *http.Request) { if u == "/" { u = "/index" } + u = filepath.Join(".", u) p := page.NewPage("index") @@ -44,7 +55,7 @@ func (a *App) RebuildIndexHandler(w http.ResponseWriter, r *http.Request) { } // StaticHandler simply returns a HTTP handler that looks at the current -// directory and exposes `static` via HTTP `/static` +// directory and exposes `static` via HTTP `/static`. func (a *App) StaticHandler() http.Handler { return http.StripPrefix("/static/", http.FileServer(http.Dir(a.StaticDirectory))) } |
