From a344e7713a8b2efadb01fd7a7df4731b225a7a9c Mon Sep 17 00:00:00 2001 From: Krishna Srinivas Date: Fri, 5 Feb 2016 03:27:56 +0530 Subject: [PATCH] browser-caching: enable browser caching for WebUI --- generic-handlers.go | 17 +++++++++++++++++ routers.go | 7 ++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/generic-handlers.go b/generic-handlers.go index cda47469c..13c01437c 100644 --- a/generic-handlers.go +++ b/generic-handlers.go @@ -93,6 +93,23 @@ func parseDate(req *http.Request) (time.Time, error) { return time.Time{}, errors.New("invalid request") } +// Adds Cache-Control header +type cacheControlHandler struct { + handler http.Handler +} + +func setCacheControlHandler(h http.Handler) http.Handler { + return cacheControlHandler{h} +} + +func (h cacheControlHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + if r.Method == "GET" { + // expire the cache in one week + w.Header().Set("Cache-Control", "public, max-age=604800") + } + h.handler.ServeHTTP(w, r) +} + // TimeValidityHandler to validate parsable time over http header func TimeValidityHandler(h http.Handler) http.Handler { return timeHandler{h} diff --git a/routers.go b/routers.go index d9f4f05c5..c368fcec2 100644 --- a/routers.go +++ b/routers.go @@ -55,9 +55,10 @@ type WebAPI struct { func getWebAPIHandler(web *WebAPI) http.Handler { var mwHandlers = []MiddlewareHandler{ - TimeValidityHandler, // Validate time. - AuthHandler, // Authentication handler for verifying tokens. - CorsHandler, // CORS added only for testing purposes. + setCacheControlHandler, // Adds Cache-Control header + TimeValidityHandler, // Validate time. + AuthHandler, // Authentication handler for verifying tokens. + CorsHandler, // CORS added only for testing purposes. } if web.AccessLog { mwHandlers = append(mwHandlers, AccessLogHandler)