Add caching middlware for static files
This commit is contained in:
@ -29,3 +29,10 @@ func (app *application) logRequest(next http.Handler) http.Handler {
|
|||||||
next.ServeHTTP(w, r)
|
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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
@ -19,7 +19,9 @@ type application struct {
|
|||||||
func (app *application) routes() http.Handler {
|
func (app *application) routes() http.Handler {
|
||||||
mux := http.NewServeMux()
|
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)
|
mux.HandleFunc("GET /election/create", app.createElectionPage)
|
||||||
|
|
||||||
api.HandlerWithOptions(app, api.StdHTTPServerOptions{
|
api.HandlerWithOptions(app, api.StdHTTPServerOptions{
|
||||||
|
Reference in New Issue
Block a user