From 47f32e1468e59010d03629b374efb63d34c059d4 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Wed, 4 Aug 2021 17:12:16 -0600 Subject: add response headers to prevent caching --- srv/cmd/mediocre-blog/main.go | 6 ++++++ srv/cmd/mediocre-blog/middleware.go | 9 +++++++++ 2 files changed, 15 insertions(+) (limited to 'srv') 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) { -- cgit v1.2.3