summaryrefslogtreecommitdiff
path: root/api/serve.go
diff options
context:
space:
mode:
authorLizzy Hunt <elizabeth.hunt@simponic.xyz>2024-04-01 17:34:14 -0600
committerLizzy Hunt <elizabeth.hunt@simponic.xyz>2024-04-01 17:34:14 -0600
commit38110bb19dcd4ed5d83a5165da13e02c17ea0e76 (patch)
treed1b72c855c9fb7b520040306e8ca0a5a700cc55d /api/serve.go
parentad30583265f838edd60f3ce2026db60de0f9df96 (diff)
downloadhatecomputers.club-38110bb19dcd4ed5d83a5165da13e02c17ea0e76.tar.gz
hatecomputers.club-38110bb19dcd4ed5d83a5165da13e02c17ea0e76.zip
add cache control to static server
Diffstat (limited to 'api/serve.go')
-rw-r--r--api/serve.go10
1 files changed, 9 insertions, 1 deletions
diff --git a/api/serve.go b/api/serve.go
index 7cef1c9..2cddcfd 100644
--- a/api/serve.go
+++ b/api/serve.go
@@ -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 {
mux := http.NewServeMux()
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 {
return &RequestContext{