Add caching middlware for static files

This commit is contained in:
2025-01-14 13:43:16 +01:00
parent 82aa5ef57d
commit 13c2693bdb
2 changed files with 10 additions and 1 deletions

View File

@ -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)
})
}

View File

@ -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{