diff options
author | Brian Picciano <mediocregopher@gmail.com> | 2021-08-04 17:12:16 -0600 |
---|---|---|
committer | Brian Picciano <mediocregopher@gmail.com> | 2021-08-04 17:13:02 -0600 |
commit | 47f32e1468e59010d03629b374efb63d34c059d4 (patch) | |
tree | 77ab61d2ed3fbb86d683d8748707c3060840014f | |
parent | 5ca7dadd02fb49dd62ad448d12021359e41beec1 (diff) |
add response headers to prevent caching
-rw-r--r-- | srv/cmd/mediocre-blog/main.go | 6 | ||||
-rw-r--r-- | srv/cmd/mediocre-blog/middleware.go | 9 |
2 files changed, 15 insertions, 0 deletions
diff --git a/srv/cmd/mediocre-blog/main.go b/srv/cmd/mediocre-blog/main.go index 5233a2c..66c17ee 100644 --- a/srv/cmd/mediocre-blog/main.go +++ b/srv/cmd/mediocre-blog/main.go @@ -172,6 +172,12 @@ func main() { apiHandler := logMiddleware(logger.WithNamespace("api"), apiMux) apiHandler = annotateMiddleware(apiHandler) + apiHandler = addResponseHeaders(map[string]string{ + "Cache-Control": "no-store, max-age=0", + "Pragma": "no-cache", + "Expires": "0", + }, apiHandler) + mux.Handle("/api/", http.StripPrefix("/api", apiHandler)) // run diff --git a/srv/cmd/mediocre-blog/middleware.go b/srv/cmd/mediocre-blog/middleware.go index 4ffba2c..165f82f 100644 --- a/srv/cmd/mediocre-blog/middleware.go +++ b/srv/cmd/mediocre-blog/middleware.go @@ -9,6 +9,15 @@ import ( "github.com/mediocregopher/mediocre-go-lib/v2/mlog" ) +func addResponseHeaders(headers map[string]string, h http.Handler) http.Handler { + return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + for k, v := range headers { + rw.Header().Set(k, v) + } + h.ServeHTTP(rw, r) + }) +} + func annotateMiddleware(h http.Handler) http.Handler { return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { |