diff --git a/cmd/web/middleware.go b/cmd/web/middleware.go index aed0826..220d110 100644 --- a/cmd/web/middleware.go +++ b/cmd/web/middleware.go @@ -29,3 +29,10 @@ func (app *application) logRequest(next http.Handler) http.Handler { next.ServeHTTP(w, r) }) } + +func (app *application) cacheStatic(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Cache-Control", "public, max-age=86400") + next.ServeHTTP(w, r) + }) +} diff --git a/cmd/web/routes.go b/cmd/web/routes.go index 365771f..62acedd 100644 --- a/cmd/web/routes.go +++ b/cmd/web/routes.go @@ -19,7 +19,9 @@ type application struct { func (app *application) routes() http.Handler { mux := http.NewServeMux() - mux.Handle("GET /static/", http.FileServerFS(ui.Files)) + cached := alice.New(app.cacheStatic) + mux.Handle("GET /static/", cached.Then(http.FileServerFS(ui.Files))) + mux.HandleFunc("GET /election/create", app.createElectionPage) api.HandlerWithOptions(app, api.StdHTTPServerOptions{