From fca59e037e46680a11c992176f1476c8bb4dc30a Mon Sep 17 00:00:00 2001 From: Parnic Date: Mon, 26 Nov 2018 22:19:46 -0600 Subject: [PATCH] Added support for cache control as a gin-gonic middleware --- middleware.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 middleware.go diff --git a/middleware.go b/middleware.go new file mode 100644 index 0000000..3b7fae8 --- /dev/null +++ b/middleware.go @@ -0,0 +1,16 @@ +package precompiler + +import ( + "strings" + + "github.com/gin-gonic/gin" +) + +// GinMiddleware is used to set the cache-control header for all files under the specified asset path (including leading slash) +func GinMiddleware(assetPath string) gin.HandlerFunc { + return func(c *gin.Context) { + if strings.HasPrefix(c.Request.URL.Path, assetPath) { + c.Header("cache-control", "max-age=315360000; public") + } + } +}