add cache control to static server

This commit is contained in:
Lizzy Hunt 2024-04-01 17:34:14 -06:00
parent ad30583265
commit 38110bb19d
Signed by untrusted user who does not match committer: simponic
GPG Key ID: 2909B9A7FF6213EE
1 changed files with 9 additions and 1 deletions

View File

@ -66,11 +66,19 @@ func IdContinuation(context *RequestContext, req *http.Request, resp http.Respon
} }
} }
func CacheControlMiddleware(next http.Handler, maxAge int) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
header := fmt.Sprintf("public, max-age=%d", maxAge)
w.Header().Set("Cache-Control", header)
next.ServeHTTP(w, r)
})
}
func MakeServer(argv *args.Arguments, dbConn *sql.DB) *http.Server { func MakeServer(argv *args.Arguments, dbConn *sql.DB) *http.Server {
mux := http.NewServeMux() mux := http.NewServeMux()
fileServer := http.FileServer(http.Dir(argv.StaticPath)) fileServer := http.FileServer(http.Dir(argv.StaticPath))
mux.Handle("GET /static/", http.StripPrefix("/static/", fileServer)) mux.Handle("/static/", http.StripPrefix("/static/", CacheControlMiddleware(fileServer, 3600)))
makeRequestContext := func() *RequestContext { makeRequestContext := func() *RequestContext {
return &RequestContext{ return &RequestContext{