From a975e1ae4d5f62ee34a7549e21645ec7872f714c Mon Sep 17 00:00:00 2001 From: Davide bonomini Date: Mon, 18 Dec 2023 16:35:21 +0100 Subject: [PATCH] Added SetCacheControl to Outcome. Removed unused code from SetCacheControl in evo.context. --- evo.context.go | 4 ---- lib/outcome/outcome.go | 21 ++++++++++++++++++++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/evo.context.go b/evo.context.go index a11a40b4..af5dfbeb 100644 --- a/evo.context.go +++ b/evo.context.go @@ -258,10 +258,6 @@ func (r *Request) RestartRouting() error { } func (r *Request) SetCacheControl(t time.Duration, headers ...string) { - if len(headers) == 0 { - return - } - var ccHeader string = fmt.Sprintf("max-age=%.0f", t.Seconds()) var options string diff --git a/lib/outcome/outcome.go b/lib/outcome/outcome.go index d191c685..5695703e 100644 --- a/lib/outcome/outcome.go +++ b/lib/outcome/outcome.go @@ -4,9 +4,10 @@ import ( "encoding/base64" "encoding/json" "fmt" - "github.com/gofiber/fiber/v2" "reflect" "time" + + "github.com/gofiber/fiber/v2" ) type Response struct { @@ -185,3 +186,21 @@ func (response *Response) Filename(filename string) *Response { func (response *Response) ResponseSerializer() *Response { return response } + +func (response *Response) SetCacheControl(t time.Duration, headers ...string) *Response { + var ccHeader string = fmt.Sprintf("max-age=%.0f", t.Seconds()) + var options string + + for _, header := range headers { + options = options + fmt.Sprintf(", %s", header) + } + + ccHeader = ccHeader + options + + if response.Headers == nil { + response.Headers = map[string]string{} + } + response.Headers["Cache-Control"] = ccHeader + + return response +}