From 3eaa6c38414ac19aa8bf2f60dda30a3507cbb146 Mon Sep 17 00:00:00 2001 From: Francesco Ghezzo Date: Sat, 25 Jul 2020 12:01:43 +0200 Subject: [PATCH 1/3] Add serve cache and related test --- static.go | 19 +++++++++++++++++-- static_test.go | 21 +++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/static.go b/static.go index ffa00ee..7cb4077 100644 --- a/static.go +++ b/static.go @@ -1,6 +1,7 @@ package static import ( + "fmt" "net/http" "os" "path" @@ -55,16 +56,30 @@ func ServeRoot(urlPrefix, root string) gin.HandlerFunc { return Serve(urlPrefix, LocalFile(root, false)) } -// Static returns a middleware handler that serves static files in the given directory. -func Serve(urlPrefix string, fs ServeFileSystem) gin.HandlerFunc { +// GenericServe returns a middleware handler that serves static files in the given directory. +func GenericServe(urlPrefix string, fs ServeFileSystem, cacheAge uint) gin.HandlerFunc { fileserver := http.FileServer(fs) if urlPrefix != "" { fileserver = http.StripPrefix(urlPrefix, fileserver) } return func(c *gin.Context) { if fs.Exists(urlPrefix, c.Request.URL.Path) { + if cacheAge != 0 { + c.Writer.Header().Add("Cache-Control", fmt.Sprintf("max-age=%d", cacheAge)) + } fileserver.ServeHTTP(c.Writer, c.Request) c.Abort() } } } + +// Static returns a middleware handler that serves static files in the given directory. +func Serve(urlPrefix string, fs ServeFileSystem) gin.HandlerFunc { + return GenericServe(urlPrefix, fs, 0) +} + +// ServeCached returns a middleware handler that similar as Serve but with the Cache-Control Header set as passed in the cacheAge parameter +func ServeCached(urlPrefix string, fs ServeFileSystem, cacheAge uint) gin.HandlerFunc { + return GenericServe(urlPrefix, fs, cacheAge) + +} diff --git a/static_test.go b/static_test.go index 6925005..1e446f5 100644 --- a/static_test.go +++ b/static_test.go @@ -132,3 +132,24 @@ func TestListIndex(t *testing.T) { w = performRequest(router, "GET", "/") assert.Contains(t, w.Body.String(), ` Date: Thu, 30 Jul 2020 16:20:51 +0200 Subject: [PATCH 2/3] Update go mod --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 1fb38e3..80b8141 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/gin-contrib/static +module github.com/ghezzofr/static require ( github.com/elazarl/go-bindata-assetfs v1.0.0 From 36aa06af61c09369f69cffe80fef4007c6b6597b Mon Sep 17 00:00:00 2001 From: Francesco Ghezzo Date: Thu, 30 Jul 2020 16:36:29 +0200 Subject: [PATCH 3/3] Add cache control specifications --- static.go | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/static.go b/static.go index 7cb4077..3895080 100644 --- a/static.go +++ b/static.go @@ -23,6 +23,12 @@ type localFileSystem struct { indexes bool } +type CacheConfigs struct { + Public bool + MaxAge uint + Immutable bool +} + func LocalFile(root string, indexes bool) *localFileSystem { return &localFileSystem{ FileSystem: gin.Dir(root, indexes), @@ -57,16 +63,26 @@ func ServeRoot(urlPrefix, root string) gin.HandlerFunc { } // GenericServe returns a middleware handler that serves static files in the given directory. -func GenericServe(urlPrefix string, fs ServeFileSystem, cacheAge uint) gin.HandlerFunc { +func GenericServe(urlPrefix string, fs ServeFileSystem, cc CacheConfigs) gin.HandlerFunc { fileserver := http.FileServer(fs) if urlPrefix != "" { fileserver = http.StripPrefix(urlPrefix, fileserver) } return func(c *gin.Context) { if fs.Exists(urlPrefix, c.Request.URL.Path) { - if cacheAge != 0 { - c.Writer.Header().Add("Cache-Control", fmt.Sprintf("max-age=%d", cacheAge)) + var cacheControl []string + if cc.Public { + cacheControl = append(cacheControl, "public") + } + if cc.MaxAge != 0 { + cacheControl = append(cacheControl, fmt.Sprintf("max-age=%d", cc.MaxAge)) + } else { + cacheControl = append(cacheControl, "no-store") + } + if cc.Immutable { + cacheControl = append(cacheControl, "immutable") } + c.Writer.Header().Add("Cache-Control", strings.Join(cacheControl, ", ")) fileserver.ServeHTTP(c.Writer, c.Request) c.Abort() } @@ -75,11 +91,11 @@ func GenericServe(urlPrefix string, fs ServeFileSystem, cacheAge uint) gin.Handl // Static returns a middleware handler that serves static files in the given directory. func Serve(urlPrefix string, fs ServeFileSystem) gin.HandlerFunc { - return GenericServe(urlPrefix, fs, 0) + return GenericServe(urlPrefix, fs, CacheConfigs{MaxAge: 0}) } // ServeCached returns a middleware handler that similar as Serve but with the Cache-Control Header set as passed in the cacheAge parameter -func ServeCached(urlPrefix string, fs ServeFileSystem, cacheAge uint) gin.HandlerFunc { - return GenericServe(urlPrefix, fs, cacheAge) +func ServeCached(urlPrefix string, fs ServeFileSystem, cc CacheConfigs) gin.HandlerFunc { + return GenericServe(urlPrefix, fs, cc) }