From 5fce2261ada616a4ed6ea7db5e3ec7899194d427 Mon Sep 17 00:00:00 2001 From: Nathan Faucett Date: Tue, 4 Oct 2022 09:26:24 -0400 Subject: [PATCH 1/2] add CacheControl to Static config --- app.go | 5 +++++ app_test.go | 19 +++++++++++++++++++ router.go | 5 +++++ 3 files changed, 29 insertions(+) diff --git a/app.go b/app.go index 5ef8d840b1..6e9729df78 100644 --- a/app.go +++ b/app.go @@ -421,6 +421,11 @@ type Static struct { // Optional. Default value 0. MaxAge int `json:"max_age"` + // SetHeaders defines a function that sets custom headers on response. + // + // Optional. Default: nil + SetHeaders func(c *Ctx) + // Next defines a function to skip this middleware when returned true. // // Optional. Default: nil diff --git a/app_test.go b/app_test.go index 7aff77476c..fdf8806823 100644 --- a/app_test.go +++ b/app_test.go @@ -767,6 +767,25 @@ func Test_App_Static_MaxAge(t *testing.T) { utils.AssertEqual(t, "public, max-age=100", resp.Header.Get(HeaderCacheControl), "CacheControl Control") } +// go test -run Test_App_Static_Custom_CacheControl +func Test_App_Static_Custom_CacheControl(t *testing.T) { + app := New() + + app.Static("/", "./.github", Static{SetHeaders: func(c *Ctx) { + if strings.Contains(string(c.GetRespHeader("Content-Type")), "text/html") { + c.Response().Header.Set("Cache-Control", "no-cache, no-store, must-revalidate") + } + }}) + + resp, err := app.Test(httptest.NewRequest("GET", "/index.html", nil)) + utils.AssertEqual(t, nil, err, "app.Test(req)") + utils.AssertEqual(t, "no-cache, no-store, must-revalidate", resp.Header.Get(HeaderCacheControl), "CacheControl Control") + + normal_resp, normal_err := app.Test(httptest.NewRequest("GET", "/config.yml", nil)) + utils.AssertEqual(t, nil, normal_err, "app.Test(req)") + utils.AssertEqual(t, "", normal_resp.Header.Get(HeaderCacheControl), "CacheControl Control") +} + // go test -run Test_App_Static_Download func Test_App_Static_Download(t *testing.T) { app := New() diff --git a/router.go b/router.go index 46fb1168b4..a54f189234 100644 --- a/router.go +++ b/router.go @@ -357,6 +357,7 @@ func (app *App) registerStatic(prefix, root string, config ...Static) Router { // Set config if provided var cacheControlValue string + var setHeaders func(c *Ctx) if len(config) > 0 { maxAge := config[0].MaxAge if maxAge > 0 { @@ -369,6 +370,7 @@ func (app *App) registerStatic(prefix, root string, config ...Static) Router { if config[0].Index != "" { fs.IndexNames = []string{config[0].Index} } + setHeaders = config[0].SetHeaders } fileHandler := fs.NewRequestHandler() handler := func(c *Ctx) error { @@ -388,6 +390,9 @@ func (app *App) registerStatic(prefix, root string, config ...Static) Router { if len(cacheControlValue) > 0 { c.fasthttp.Response.Header.Set(HeaderCacheControl, cacheControlValue) } + if setHeaders != nil { + setHeaders(c) + } return nil } // Reset response to default From d08b78d4a3986d8c6ef4f9576a5888e0ba68de91 Mon Sep 17 00:00:00 2001 From: Nathan Faucett Date: Wed, 5 Oct 2022 10:28:07 -0400 Subject: [PATCH 2/2] change config name to ModifyResponse --- app.go | 4 ++-- app_test.go | 3 ++- router.go | 8 ++++---- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/app.go b/app.go index 6e9729df78..0ca6e7dc66 100644 --- a/app.go +++ b/app.go @@ -421,10 +421,10 @@ type Static struct { // Optional. Default value 0. MaxAge int `json:"max_age"` - // SetHeaders defines a function that sets custom headers on response. + // ModifyResponse defines a function that allows you to alter the response. // // Optional. Default: nil - SetHeaders func(c *Ctx) + ModifyResponse Handler // Next defines a function to skip this middleware when returned true. // diff --git a/app_test.go b/app_test.go index fdf8806823..6bbd8fcba4 100644 --- a/app_test.go +++ b/app_test.go @@ -771,10 +771,11 @@ func Test_App_Static_MaxAge(t *testing.T) { func Test_App_Static_Custom_CacheControl(t *testing.T) { app := New() - app.Static("/", "./.github", Static{SetHeaders: func(c *Ctx) { + app.Static("/", "./.github", Static{ModifyResponse: func(c *Ctx) error { if strings.Contains(string(c.GetRespHeader("Content-Type")), "text/html") { c.Response().Header.Set("Cache-Control", "no-cache, no-store, must-revalidate") } + return nil }}) resp, err := app.Test(httptest.NewRequest("GET", "/index.html", nil)) diff --git a/router.go b/router.go index a54f189234..ff51bb0d52 100644 --- a/router.go +++ b/router.go @@ -357,7 +357,7 @@ func (app *App) registerStatic(prefix, root string, config ...Static) Router { // Set config if provided var cacheControlValue string - var setHeaders func(c *Ctx) + var modifyResponse Handler if len(config) > 0 { maxAge := config[0].MaxAge if maxAge > 0 { @@ -370,7 +370,7 @@ func (app *App) registerStatic(prefix, root string, config ...Static) Router { if config[0].Index != "" { fs.IndexNames = []string{config[0].Index} } - setHeaders = config[0].SetHeaders + modifyResponse = config[0].ModifyResponse } fileHandler := fs.NewRequestHandler() handler := func(c *Ctx) error { @@ -390,8 +390,8 @@ func (app *App) registerStatic(prefix, root string, config ...Static) Router { if len(cacheControlValue) > 0 { c.fasthttp.Response.Header.Set(HeaderCacheControl, cacheControlValue) } - if setHeaders != nil { - setHeaders(c) + if modifyResponse != nil { + return modifyResponse(c) } return nil }