forked from openshift/library-go
/
cachecontrol.go
35 lines (31 loc) · 868 Bytes
/
cachecontrol.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package apiserverconfig
import (
"net/http"
"strings"
)
// cacheExcludedPaths is small and simple until the handlers include the cache headers they need
var cacheExcludedPathPrefixes = []string{
"/swagger-2.0.0.json",
"/swagger-2.0.0.pb-v1",
"/swagger-2.0.0.pb-v1.gz",
"/swagger.json",
"/swaggerapi",
"/openapi/",
}
// cacheControlFilter sets the Cache-Control header to the specified value.
func WithCacheControl(handler http.Handler, value string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if _, ok := w.Header()["Cache-Control"]; ok {
handler.ServeHTTP(w, req)
return
}
for _, prefix := range cacheExcludedPathPrefixes {
if strings.HasPrefix(req.URL.Path, prefix) {
handler.ServeHTTP(w, req)
return
}
}
w.Header().Set("Cache-Control", value)
handler.ServeHTTP(w, req)
})
}