From 7cb5298ae3976f0c7494cadd7a83afe8e67fe3db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Thu, 27 Dec 2018 21:19:58 +0100 Subject: [PATCH 1/2] feat(backend): set caching headers for all static files Those should improve browser caching so we avoid fetching all css/js files on every page load --- assets.go | 11 +++++++++++ main.go | 3 ++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/assets.go b/assets.go index b50da0e36..95c4f8be5 100644 --- a/assets.go +++ b/assets.go @@ -7,6 +7,7 @@ import ( "net/http" "os" "strings" + "time" assetfs "github.com/elazarl/go-bindata-assetfs" "github.com/gin-gonic/gin" @@ -87,3 +88,13 @@ func serverFileOrEmpty(path string, contentType string, c *gin.Context) { } c.File(path) } + +func staticHeaders(prefix string) gin.HandlerFunc { + return func(c *gin.Context) { + if strings.HasPrefix(c.Request.URL.Path, prefix) { + c.Header("Cache-Control", "public, max-age=2592000") + expiresTime := time.Now().AddDate(0, 0, 30).Format(http.TimeFormat) + c.Header("Expires", expiresTime) + } + } +} diff --git a/main.go b/main.go index 3e9c980e5..503d53bfe 100644 --- a/main.go +++ b/main.go @@ -19,11 +19,11 @@ import ( "github.com/gin-contrib/static" "github.com/gin-gonic/contrib/sentry" "github.com/gin-gonic/gin" - "github.com/patrickmn/go-cache" "github.com/spf13/pflag" raven "github.com/getsentry/raven-go" ginprometheus "github.com/mcuadros/go-gin-prometheus" + cache "github.com/patrickmn/go-cache" log "github.com/sirupsen/logrus" ) @@ -55,6 +55,7 @@ func getViewURL(sub string) string { func setupRouter(router *gin.Engine) { router.Use(gzip.Gzip(gzip.DefaultCompression)) + router.Use(staticHeaders(getViewURL("/static/"))) router.Use(static.Serve(getViewURL("/"), staticBuildFileSystem)) // next 2 lines are to allow service raw sources so sentry can fetch source maps router.Use(static.Serve(getViewURL("/static/js/"), staticSrcFileSystem)) From db9f28cdf4fe6e8657aca3944e80562489a4275b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Thu, 27 Dec 2018 21:20:44 +0100 Subject: [PATCH 2/2] chore(backend): rename function for clarity there's a typo and we now raise 404 on missing file --- assets.go | 2 +- main.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/assets.go b/assets.go index 95c4f8be5..e5d5ebe8a 100644 --- a/assets.go +++ b/assets.go @@ -77,7 +77,7 @@ func loadTemplate(t *template.Template, path string) *template.Template { return t } -func serverFileOrEmpty(path string, contentType string, c *gin.Context) { +func serveFileOr404(path string, contentType string, c *gin.Context) { if path == "" { c.Data(200, contentType, nil) return diff --git a/main.go b/main.go index 503d53bfe..4d0fca3e9 100644 --- a/main.go +++ b/main.go @@ -88,10 +88,10 @@ func setupRouter(router *gin.Engine) { router.GET(getViewURL("/labelValues.json"), knownLabelValues) router.GET(getViewURL("/custom.css"), func(c *gin.Context) { - serverFileOrEmpty(config.Config.Custom.CSS, "text/css", c) + serveFileOr404(config.Config.Custom.CSS, "text/css", c) }) router.GET(getViewURL("/custom.js"), func(c *gin.Context) { - serverFileOrEmpty(config.Config.Custom.JS, "application/javascript", c) + serveFileOr404(config.Config.Custom.JS, "application/javascript", c) }) router.NoRoute(notFound)