Skip to content

Commit 8310194

Browse files
feat(pkg): add cache control middleware
1 parent 7ce25db commit 8310194

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

pkg/chiext/chiext.go

+16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package chiext
22

33
import (
4+
"fmt"
45
"io/fs"
56
"net/http"
67

@@ -24,3 +25,18 @@ func MountFS(r chi.Router, f fs.FS) {
2425
panic(err)
2526
}
2627
}
28+
29+
func CacheControl(maxAge int) func(h http.Handler) http.Handler {
30+
var value string
31+
if maxAge == 0 {
32+
value = "max-age=31536000,immutable"
33+
} else {
34+
value = fmt.Sprintf("max-age=%d", maxAge)
35+
}
36+
return func(h http.Handler) http.Handler {
37+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
38+
w.Header().Set("Cache-Control", value)
39+
h.ServeHTTP(w, r)
40+
})
41+
}
42+
}

0 commit comments

Comments
 (0)