From a213cfdb4a0cc39e2dabfa98094f24f59b9f21c7 Mon Sep 17 00:00:00 2001 From: darkweak Date: Mon, 12 Aug 2024 22:32:35 +0200 Subject: [PATCH] bump storages to v0.0.6 --- configurationtypes/types.go | 6 +- context/cache.go | 2 +- context/cache_test.go | 9 +- context/graphql_test.go | 2 +- context/method.go | 2 +- context/method_test.go | 4 +- context/mode.go | 2 +- context/mode_test.go | 2 +- context/timeout.go | 4 +- context/timeout_test.go | 4 +- context/types_test.go | 6 +- docs/website/content/docs/storages/add-own.md | 13 +- go.mod | 2 +- go.sum | 4 +- pkg/middleware/configuration.go | 8 +- pkg/middleware/middleware.go | 32 +- pkg/storage/defaultProvider.go | 7 +- pkg/surrogate/providers/common.go | 15 +- pkg/surrogate/providers/common_test.go | 2 +- pkg/surrogate/providers/factory_test.go | 8 +- plugins/beego/go.mod | 2 +- plugins/beego/go.sum | 4 +- plugins/caddy/cleaner.go | 4 +- plugins/caddy/configuration.go | 8 +- plugins/caddy/dispatch.go | 14 +- plugins/caddy/go.mod | 2 +- plugins/caddy/go.sum | 4 +- plugins/caddy/httpcache.go | 5 +- plugins/chi/go.mod | 2 +- plugins/chi/go.sum | 4 +- plugins/dotweb/go.mod | 2 +- plugins/dotweb/go.sum | 4 +- plugins/echo/go.mod | 2 +- plugins/echo/go.sum | 4 +- plugins/fiber/go.mod | 2 +- plugins/fiber/go.sum | 4 +- plugins/gin/go.mod | 2 +- plugins/gin/go.sum | 4 +- plugins/go-zero/go.mod | 4 +- plugins/go-zero/go.sum | 8 +- plugins/goa/go.mod | 2 +- plugins/goa/go.sum | 4 +- plugins/goyave/go.mod | 2 +- plugins/goyave/go.sum | 4 +- plugins/hertz/go.mod | 2 +- plugins/hertz/go.sum | 4 +- plugins/kratos/go.mod | 2 +- plugins/kratos/go.sum | 4 +- plugins/roadrunner/go.mod | 2 +- plugins/roadrunner/go.sum | 4 +- plugins/roadrunner/httpcache.go | 2 +- plugins/skipper/go.mod | 2 +- plugins/skipper/go.sum | 4 +- plugins/souin/configuration/configuration.go | 8 +- plugins/souin/storages/storages.go | 6 +- plugins/traefik/go.mod | 2 +- plugins/traefik/go.sum | 4 +- .../github.com/darkweak/souin/api/main.go | 50 ++ .../souin/api/prometheus/prometheus.go | 5 + .../github.com/darkweak/souin/api/souin.go | 233 ++++++++ .../github.com/darkweak/souin/api/types.go | 12 + .../github.com/darkweak/souin/cache/ykey.go | 136 +++++ .../souin/configurationtypes/types.go | 11 +- .../darkweak/souin/context/cache.go | 5 - .../darkweak/souin/context/graphql.go | 27 - .../github.com/darkweak/souin/context/key.go | 117 ++-- .../darkweak/souin/context/method.go | 5 - .../github.com/darkweak/souin/context/mode.go | 7 +- .../github.com/darkweak/souin/context/now.go | 4 - .../darkweak/souin/context/timeout.go | 8 +- .../darkweak/souin/context/types.go | 5 +- .../darkweak/souin/pkg/api/debug/debug.go | 87 --- .../github.com/darkweak/souin/pkg/api/main.go | 9 +- .../souin/pkg/api/prometheus/prometheus.go | 106 +--- .../darkweak/souin/pkg/api/souin.go | 97 +--- .../souin/pkg/middleware/configuration.go | 12 - .../souin/pkg/middleware/middleware.go | 530 +++++------------- .../darkweak/souin/pkg/middleware/writer.go | 31 +- .../darkweak/souin/pkg/rfc/revalidation.go | 55 +- .../github.com/darkweak/souin/pkg/rfc/vary.go | 7 +- .../souin/pkg/storage/abstractProvider.go | 57 ++ .../pkg/storage/abstractProvider_test.go | 16 + .../souin/pkg/storage/cacheProvider.go | 142 +++++ .../souin/pkg/storage/defaultProvider.go | 205 ------- .../darkweak/souin/pkg/storage/types/types.go | 11 +- .../souin/pkg/surrogate/providers/common.go | 93 ++- .../github.com/darkweak/souin/tests/mock.go | 9 +- .../github.com/darkweak/storages/core/core.go | 21 +- .../darkweak/storages/core/core_wasm.go | 167 ++++++ .../darkweak/storages/core/logger.go | 18 + plugins/traefik/vendor/modules.txt | 2 +- plugins/tyk/go.mod | 2 +- plugins/tyk/go.sum | 4 +- .../cache/surrogate/providers/common.go | 6 +- plugins/tyk/override/middleware/middleware.go | 2 +- .../tyk/override/storage/abstractProvider.go | 14 +- plugins/tyk/override/storage/cacheProvider.go | 2 +- plugins/webgo/go.mod | 2 +- plugins/webgo/go.sum | 4 +- tests/mock.go | 9 +- 100 files changed, 1342 insertions(+), 1280 deletions(-) create mode 100644 plugins/traefik/vendor/github.com/darkweak/souin/api/main.go create mode 100644 plugins/traefik/vendor/github.com/darkweak/souin/api/prometheus/prometheus.go create mode 100644 plugins/traefik/vendor/github.com/darkweak/souin/api/souin.go create mode 100644 plugins/traefik/vendor/github.com/darkweak/souin/api/types.go create mode 100644 plugins/traefik/vendor/github.com/darkweak/souin/cache/ykey.go delete mode 100644 plugins/traefik/vendor/github.com/darkweak/souin/pkg/api/debug/debug.go create mode 100644 plugins/traefik/vendor/github.com/darkweak/souin/pkg/storage/abstractProvider.go create mode 100644 plugins/traefik/vendor/github.com/darkweak/souin/pkg/storage/abstractProvider_test.go create mode 100644 plugins/traefik/vendor/github.com/darkweak/souin/pkg/storage/cacheProvider.go delete mode 100644 plugins/traefik/vendor/github.com/darkweak/souin/pkg/storage/defaultProvider.go create mode 100644 plugins/traefik/vendor/github.com/darkweak/storages/core/core_wasm.go create mode 100644 plugins/traefik/vendor/github.com/darkweak/storages/core/logger.go diff --git a/configurationtypes/types.go b/configurationtypes/types.go index f0ffb9535..016aae3a5 100644 --- a/configurationtypes/types.go +++ b/configurationtypes/types.go @@ -9,7 +9,7 @@ import ( "strings" "time" - "go.uber.org/zap" + "github.com/darkweak/storages/core" yaml "gopkg.in/yaml.v3" ) @@ -439,8 +439,8 @@ type AbstractConfigurationInterface interface { GetDefaultCache() DefaultCacheInterface GetAPI() API GetLogLevel() string - GetLogger() *zap.Logger - SetLogger(*zap.Logger) + GetLogger() core.Logger + SetLogger(core.Logger) GetYkeys() map[string]SurrogateKeys GetSurrogateKeys() map[string]SurrogateKeys GetCacheKeys() CacheKeys diff --git a/context/cache.go b/context/cache.go index a2ff6ff33..0e24066b4 100644 --- a/context/cache.go +++ b/context/cache.go @@ -28,7 +28,7 @@ func (cc *cacheContext) SetupContext(c configurationtypes.AbstractConfigurationI if c.GetDefaultCache().GetCacheName() != "" { cc.cacheName = c.GetDefaultCache().GetCacheName() } - c.GetLogger().Sugar().Debugf("Set %s as Cache-Status name", cc.cacheName) + c.GetLogger().Debugf("Set %s as Cache-Status name", cc.cacheName) } func (cc *cacheContext) SetContext(req *http.Request) *http.Request { diff --git a/context/cache_test.go b/context/cache_test.go index f31f30831..e6b98c5d0 100644 --- a/context/cache_test.go +++ b/context/cache_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/darkweak/souin/configurationtypes" + "github.com/darkweak/storages/core" "go.uber.org/zap" ) @@ -29,10 +30,10 @@ func (*testConfiguration) GetAPI() configurationtypes.API { func (*testConfiguration) GetLogLevel() string { return "" } -func (*testConfiguration) GetLogger() *zap.Logger { - return zap.NewNop() +func (*testConfiguration) GetLogger() core.Logger { + return zap.NewNop().Sugar() } -func (*testConfiguration) SetLogger(*zap.Logger) { +func (*testConfiguration) SetLogger(core.Logger) { } func (*testConfiguration) GetYkeys() map[string]configurationtypes.SurrogateKeys { return nil @@ -49,7 +50,7 @@ func Test_CacheContext_SetupContext(t *testing.T) { c := testConfiguration{ defaultCache: &dc, } - c.SetLogger(zap.NewNop()) + c.SetLogger(zap.NewNop().Sugar()) ctx := cacheContext{} ctx.SetupContext(&c) diff --git a/context/graphql_test.go b/context/graphql_test.go index 2ce432c63..f8392b336 100644 --- a/context/graphql_test.go +++ b/context/graphql_test.go @@ -34,7 +34,7 @@ func Test_GraphQLContext_SetContext(t *testing.T) { c := testConfiguration{ defaultCache: &dc, } - c.SetLogger(zap.NewNop()) + c.SetLogger(zap.NewNop().Sugar()) ctx := graphQLContext{custom: true} req := httptest.NewRequest(http.MethodGet, "http://domain.com", nil) diff --git a/context/method.go b/context/method.go index 74becc87b..502e4f2cd 100644 --- a/context/method.go +++ b/context/method.go @@ -26,7 +26,7 @@ func (m *methodContext) SetupContext(c configurationtypes.AbstractConfigurationI m.allowedVerbs = c.GetDefaultCache().GetAllowedHTTPVerbs() m.custom = true } - c.GetLogger().Sugar().Debugf("Allow %d method(s). %v.", len(m.allowedVerbs), m.allowedVerbs) + c.GetLogger().Debugf("Allow %d method(s). %v.", len(m.allowedVerbs), m.allowedVerbs) } func (m *methodContext) SetContext(req *http.Request) *http.Request { diff --git a/context/method_test.go b/context/method_test.go index 6e5082749..9dc8a6e45 100644 --- a/context/method_test.go +++ b/context/method_test.go @@ -14,7 +14,7 @@ func Test_MethodContext_SetupContext(t *testing.T) { c := testConfiguration{ defaultCache: &dc, } - c.SetLogger(zap.NewNop()) + c.SetLogger(zap.NewNop().Sugar()) ctx := methodContext{} ctx.SetupContext(&c) @@ -34,7 +34,7 @@ func Test_MethodContext_SetContext(t *testing.T) { c := testConfiguration{ defaultCache: &dc, } - c.SetLogger(zap.NewNop()) + c.SetLogger(zap.NewNop().Sugar()) ctx := methodContext{} c.defaultCache.AllowedHTTPVerbs = []string{http.MethodGet, http.MethodHead} ctx.SetupContext(&c) diff --git a/context/mode.go b/context/mode.go index dbe682d1c..d436152b5 100644 --- a/context/mode.go +++ b/context/mode.go @@ -22,7 +22,7 @@ func (mc *ModeContext) SetupContext(c configurationtypes.AbstractConfigurationIn mc.Bypass_request = mode == "bypass" || mode == "bypass_request" mc.Bypass_response = mode == "bypass" || mode == "bypass_response" mc.Strict = !mc.Bypass_request && !mc.Bypass_response - c.GetLogger().Sugar().Debugf("The cache logic will run as %s: %+v", mode, mc) + c.GetLogger().Debugf("The cache logic will run as %s: %+v", mode, mc) } func (mc *ModeContext) SetContext(req *http.Request) *http.Request { diff --git a/context/mode_test.go b/context/mode_test.go index 4477c7993..6e9faf29f 100644 --- a/context/mode_test.go +++ b/context/mode_test.go @@ -12,7 +12,7 @@ func Test_ModeContext_SetupContext(t *testing.T) { c := testConfiguration{ defaultCache: &dc, } - c.SetLogger(zap.NewNop()) + c.SetLogger(zap.NewNop().Sugar()) ctx := ModeContext{} ctx.SetupContext(&c) diff --git a/context/timeout.go b/context/timeout.go index e2260bdf0..f5a816a6f 100644 --- a/context/timeout.go +++ b/context/timeout.go @@ -35,8 +35,8 @@ func (t *timeoutContext) SetupContext(c configurationtypes.AbstractConfiguration if c.GetDefaultCache().GetTimeout().Backend.Duration != 0 { t.timeoutBackend = c.GetDefaultCache().GetTimeout().Backend.Duration } - c.GetLogger().Sugar().Infof("Set backend timeout to %v", t.timeoutBackend) - c.GetLogger().Sugar().Infof("Set cache timeout to %v", t.timeoutBackend) + c.GetLogger().Infof("Set backend timeout to %v", t.timeoutBackend) + c.GetLogger().Infof("Set cache timeout to %v", t.timeoutBackend) } func (t *timeoutContext) SetContext(req *http.Request) *http.Request { diff --git a/context/timeout_test.go b/context/timeout_test.go index 710c0fe5e..dae521b36 100644 --- a/context/timeout_test.go +++ b/context/timeout_test.go @@ -16,7 +16,7 @@ func Test_TimeoutContext_SetupContext(t *testing.T) { c := testConfiguration{ defaultCache: &dc, } - c.SetLogger(zap.NewNop()) + c.SetLogger(zap.NewNop().Sugar()) ctx := timeoutContext{} ctx.SetupContext(&c) @@ -61,7 +61,7 @@ func Test_TimeoutContext_SetContext(t *testing.T) { c := testConfiguration{ defaultCache: &dc, } - c.SetLogger(zap.NewNop()) + c.SetLogger(zap.NewNop().Sugar()) ctx := timeoutContext{} ctx.SetupContext(&c) diff --git a/context/types_test.go b/context/types_test.go index a15358b9f..5e2b9724d 100644 --- a/context/types_test.go +++ b/context/types_test.go @@ -20,7 +20,7 @@ func Test_Context_Init(t *testing.T) { c := testConfiguration{ defaultCache: &dc, } - c.SetLogger(zap.NewNop()) + c.SetLogger(zap.NewNop().Sugar()) co := GetContext() co.Init(&c) @@ -31,7 +31,7 @@ func Test_Context_SetContext(t *testing.T) { c := testConfiguration{ defaultCache: &dc, } - c.SetLogger(zap.NewNop()) + c.SetLogger(zap.NewNop().Sugar()) co := GetContext() co.Init(&c) @@ -58,7 +58,7 @@ func Test_Context_SetBaseContext(t *testing.T) { c := testConfiguration{ defaultCache: &dc, } - c.SetLogger(zap.NewNop()) + c.SetLogger(zap.NewNop().Sugar()) co := GetContext() co.Init(&c) diff --git a/docs/website/content/docs/storages/add-own.md b/docs/website/content/docs/storages/add-own.md index d43fe3bfe..323bcabee 100644 --- a/docs/website/content/docs/storages/add-own.md +++ b/docs/website/content/docs/storages/add-own.md @@ -12,7 +12,7 @@ Your `struct` must implement it to be a valid storer and be registered in the st By convention we declare a `Factory` function that respects this signature: ```go -func(providerConfiguration core.CacheProvider, logger *zap.Logger, stale time.Duration) (core.Storer, error) +func(providerConfiguration core.CacheProvider, logger core.Logger, stale time.Duration) (core.Storer, error) ``` And the `Storer` interface is the following: @@ -47,18 +47,17 @@ import ( "github.com/darkweak/souin/pkg/storage/types" "github.com/darkweak/storages/core" - "go.uber.org/zap" ) // custom storage provider type type customStorage struct { m *sync.Map stale time.Duration - logger *zap.Logger + logger core.Logger } // Factory function create new custom storage instance -func Factory(_ core.CacheProvider, logger *zap.Logger, stale time.Duration) (types.Storer, error) { +func Factory(_ core.CacheProvider, logger core.Logger, stale time.Duration) (types.Storer, error) { return &customStorage{m: &sync.Map{}, logger: logger, stale: stale}, nil } ``` @@ -164,7 +163,7 @@ func (provider *customStorage) SetMultiLevel(baseKey, variedKey string, value [] var e error compressed := new(bytes.Buffer) if _, e = lz4.NewWriter(compressed).ReadFrom(bytes.NewReader(value)); e != nil { - provider.logger.Sugar().Errorf("Impossible to compress the key %s into Badger, %v", variedKey, e) + provider.logger.Errorf("Impossible to compress the key %s into Badger, %v", variedKey, e) return e } @@ -182,7 +181,7 @@ func (provider *customStorage) SetMultiLevel(baseKey, variedKey string, value [] return e } - provider.logger.Sugar().Debugf("Store the new mapping for the key %s in customStorage", variedKey) + provider.logger.Debugf("Store the new mapping for the key %s in customStorage", variedKey) provider.m.Store(mappingKey, val) return nil } @@ -233,7 +232,7 @@ After that you'll be able to register your storage using // anywhere.go ... logger, _ := zap.NewProduction() -customStorage, _ := your_package.Factory(core.CacheProvider{}, logger, time.Hour) +customStorage, _ := your_package.Factory(core.CacheProvider{}, logger.Sugar(), time.Hour) // It will register as `YOUR_CUSTOM_STORAGE-THE_UUID`. core.RegisterStorage(customStorage) diff --git a/go.mod b/go.mod index b50a674b9..e0c308765 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.22.1 require ( github.com/caddyserver/caddy/v2 v2.8.4 github.com/cespare/xxhash/v2 v2.2.0 - github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 + github.com/darkweak/storages/core v0.0.6 github.com/google/uuid v1.6.0 github.com/pierrec/lz4/v4 v4.1.21 github.com/pquerna/cachecontrol v0.2.0 diff --git a/go.sum b/go.sum index 44d44c079..840de0080 100644 --- a/go.sum +++ b/go.sum @@ -98,8 +98,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= github.com/darkweak/go-esi v0.0.5 h1:b9LHI8Tz46R+i6p8avKPHAIBRQUCZDebNmKm5w/Zrns= github.com/darkweak/go-esi v0.0.5/go.mod h1:koCJqwum1u6mslyZuq/Phm6hfG1K3ZK5Y7jrUBTH654= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 h1:WsLHVD+WDxYh5SuZzpZXnhnDruZVrrvnYsHw08izs1s= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881/go.mod h1:mWN/TO5aLYt2jK6EweZ4JFXXPYmYwYFUQ4YBuIlBkpc= +github.com/darkweak/storages/core v0.0.6 h1:Yj0Z98HsC9Kd9cmjAXLagx6aU7AtXuL++RV2W+FSjc0= +github.com/darkweak/storages/core v0.0.6/go.mod h1:ajTpB9IFLRIRY0EEFLjM5vtsrcNTh+TJK9yRxgG5/wY= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/pkg/middleware/configuration.go b/pkg/middleware/configuration.go index ad137138c..8abd9917b 100644 --- a/pkg/middleware/configuration.go +++ b/pkg/middleware/configuration.go @@ -2,7 +2,7 @@ package middleware import ( "github.com/darkweak/souin/configurationtypes" - "go.uber.org/zap" + "github.com/darkweak/storages/core" ) // BaseConfiguration holder @@ -12,7 +12,7 @@ type BaseConfiguration struct { CacheKeys configurationtypes.CacheKeys `json:"cache_keys" yaml:"cache_keys"` URLs map[string]configurationtypes.URL `json:"urls" yaml:"urls"` LogLevel string `json:"log_level" yaml:"log_level"` - Logger *zap.Logger + Logger core.Logger PluginName string Ykeys map[string]configurationtypes.SurrogateKeys `json:"ykeys" yaml:"ykeys"` SurrogateKeys map[string]configurationtypes.SurrogateKeys `json:"surrogate_keys" yaml:"surrogate_keys"` @@ -44,12 +44,12 @@ func (c *BaseConfiguration) GetLogLevel() string { } // GetLogger get the logger -func (c *BaseConfiguration) GetLogger() *zap.Logger { +func (c *BaseConfiguration) GetLogger() core.Logger { return c.Logger } // SetLogger set the logger -func (c *BaseConfiguration) SetLogger(l *zap.Logger) { +func (c *BaseConfiguration) SetLogger(l core.Logger) { c.Logger = l } diff --git a/pkg/middleware/middleware.go b/pkg/middleware/middleware.go index 2aa0b0b98..840830448 100644 --- a/pkg/middleware/middleware.go +++ b/pkg/middleware/middleware.go @@ -60,7 +60,7 @@ func NewHTTPCacheHandler(c configurationtypes.AbstractConfigurationInterface) *S }, } logger, _ := cfg.Build() - c.SetLogger(logger) + c.SetLogger(logger.Sugar()) } storedStorers := core.GetRegisteredStorers() @@ -80,7 +80,7 @@ func NewHTTPCacheHandler(c configurationtypes.AbstractConfigurationInterface) *S for _, storer := range storers { names = append(names, storer.Name()) } - c.GetLogger().Sugar().Debugf("You're running Souin with the following storages %s", strings.Join(names, ", ")) + c.GetLogger().Debugf("You're running Souin with the following storages %s", strings.Join(names, ", ")) } } if len(storers) == 0 { @@ -95,7 +95,7 @@ func NewHTTPCacheHandler(c configurationtypes.AbstractConfigurationInterface) *S storers = append(storers, memoryStorer) } - c.GetLogger().Sugar().Debugf("Storer initialized: %#v.", storers) + c.GetLogger().Debugf("Storer initialized: %#v.", storers) regexpUrls := helpers.InitializeRegexp(c) surrogateStorage := surrogate.InitializeSurrogate(c, fmt.Sprintf("%s-%s", storers[0].Name(), storers[0].Uuid())) c.GetLogger().Debug("Surrogate storage initialized.") @@ -214,7 +214,7 @@ func (s *SouinBaseHandler) Store( } responseCc, _ := cacheobject.ParseResponseCacheControl(rfc.HeaderAllCommaSepValuesString(customWriter.Header(), headerName)) - s.Configuration.GetLogger().Sugar().Debugf("Response cache-control %+v", responseCc) + s.Configuration.GetLogger().Debugf("Response cache-control %+v", responseCc) if responseCc == nil { customWriter.Header().Set("Cache-Status", fmt.Sprintf("%s; fwd=uri-miss; key=%s; detail=INVALID-RESPONSE-CACHE-CONTROL", rq.Context().Value(context.CacheName), rfc.GetCacheKeyFromCtx(rq.Context()))) return nil @@ -302,7 +302,7 @@ func (s *SouinBaseHandler) Store( cachedKey = fmt.Sprint(xxhash.Sum64String(cachedKey)) variedKey = fmt.Sprint(xxhash.Sum64String(variedKey)) } - s.Configuration.GetLogger().Sugar().Debugf("Store the response for %s with duration %v", variedKey, ma) + s.Configuration.GetLogger().Debugf("Store the response for %s with duration %v", variedKey, ma) var wg sync.WaitGroup mu := sync.Mutex{} @@ -328,7 +328,7 @@ func (s *SouinBaseHandler) Store( res.Header.Get("Etag"), ma, variedKey, ) == nil { - s.Configuration.GetLogger().Sugar().Debugf("Stored the key %s in the %s provider", variedKey, currentStorer.Name()) + s.Configuration.GetLogger().Debugf("Stored the key %s in the %s provider", variedKey, currentStorer.Name()) } else { mu.Lock() fails = append(fails, fmt.Sprintf("; detail=%s-INSERTION-ERROR", currentStorer.Name())) @@ -376,7 +376,7 @@ func (s *SouinBaseHandler) Upstream( requestCc *cacheobject.RequestCacheDirectives, cachedKey string, ) error { - s.Configuration.GetLogger().Sugar().Debug("Request the upstream server") + s.Configuration.GetLogger().Debug("Request the upstream server") prometheus.Increment(prometheus.RequestCounter) var recoveredFromErr error = nil @@ -400,7 +400,7 @@ func (s *SouinBaseHandler) Upstream( } sfValue, err, shared := s.singleflightPool.Do(singleflightCacheKey, func() (interface{}, error) { if e := next(customWriter, rq); e != nil { - s.Configuration.GetLogger().Sugar().Warnf("%#v", e) + s.Configuration.GetLogger().Warnf("%#v", e) customWriter.Header().Set("Cache-Status", fmt.Sprintf("%s; fwd=uri-miss; key=%s; detail=SERVE-HTTP-ERROR", rq.Context().Value(context.CacheName), rfc.GetCacheKeyFromCtx(rq.Context()))) return nil, e } @@ -452,7 +452,7 @@ func (s *SouinBaseHandler) Upstream( } } if shared { - s.Configuration.GetLogger().Sugar().Infof("Reused response from concurrent request with the key %s", cachedKey) + s.Configuration.GetLogger().Infof("Reused response from concurrent request with the key %s", cachedKey) } _, _ = customWriter.Write(sfWriter.body) maps.Copy(customWriter.Header(), sfWriter.headers) @@ -463,7 +463,7 @@ func (s *SouinBaseHandler) Upstream( } func (s *SouinBaseHandler) Revalidate(validator *core.Revalidator, next handlerFunc, customWriter *CustomWriter, rq *http.Request, requestCc *cacheobject.RequestCacheDirectives, cachedKey string) error { - s.Configuration.GetLogger().Sugar().Debug("Revalidate the request with the upstream server") + s.Configuration.GetLogger().Debug("Revalidate the request with the upstream server") prometheus.Increment(prometheus.RequestRevalidationCounter) singleflightCacheKey := cachedKey @@ -508,7 +508,7 @@ func (s *SouinBaseHandler) Revalidate(validator *core.Revalidator, next handlerF if sfWriter, ok := sfValue.(singleflightValue); ok { if shared { - s.Configuration.GetLogger().Sugar().Infof("Reused response from concurrent request with the key %s", cachedKey) + s.Configuration.GetLogger().Infof("Reused response from concurrent request with the key %s", cachedKey) } _, _ = customWriter.Write(sfWriter.body) maps.Copy(customWriter.Header(), sfWriter.headers) @@ -546,7 +546,7 @@ func (s *SouinBaseHandler) ServeHTTP(rw http.ResponseWriter, rq *http.Request, n defer func(s time.Time) { prometheus.Add(prometheus.AvgResponseTime, float64(time.Since(s).Milliseconds())) }(start) - s.Configuration.GetLogger().Sugar().Debugf("Incomming request %+v", rq) + s.Configuration.GetLogger().Debugf("Incomming request %+v", rq) if b, handler := s.HandleInternally(rq); b { handler(rw, rq) return nil @@ -614,7 +614,7 @@ func (s *SouinBaseHandler) ServeHTTP(rw http.ResponseWriter, rq *http.Request, n crw.headersSent = true crw.mutex.Unlock() }(req, customWriter) - s.Configuration.GetLogger().Sugar().Debugf("Request cache-control %+v", requestCc) + s.Configuration.GetLogger().Debugf("Request cache-control %+v", requestCc) if modeContext.Bypass_request || !requestCc.NoCache { validator := rfc.ParseRequest(req) var fresh, stale *http.Response @@ -626,7 +626,7 @@ func (s *SouinBaseHandler) ServeHTTP(rw http.ResponseWriter, rq *http.Request, n fresh, stale = currentStorer.GetMultiLevel(finalKey, req, validator) if fresh != nil || stale != nil { - s.Configuration.GetLogger().Sugar().Debugf("Found at least one valid response in the %s storage", currentStorer.Name()) + s.Configuration.GetLogger().Debugf("Found at least one valid response in the %s storage", currentStorer.Name()) break } } @@ -673,7 +673,7 @@ func (s *SouinBaseHandler) ServeHTTP(rw http.ResponseWriter, rq *http.Request, n customWriter.Header()[h] = v } customWriter.WriteHeader(response.StatusCode) - s.Configuration.GetLogger().Sugar().Debugf("Serve from cache %+v", req) + s.Configuration.GetLogger().Debugf("Serve from cache %+v", req) _, _ = io.Copy(customWriter.Buf, response.Body) _, err := customWriter.Send() prometheus.Increment(prometheus.CachedResponseCounter) @@ -779,7 +779,7 @@ func (s *SouinBaseHandler) ServeHTTP(rw http.ResponseWriter, rq *http.Request, n switch req.Context().Err() { case baseCtx.DeadlineExceeded: customWriter.WriteHeader(http.StatusGatewayTimeout) - s.Configuration.GetLogger().Sugar().Infof("Internal server error on endpoint %s: %v", req.URL, s.Storers) + s.Configuration.GetLogger().Infof("Internal server error on endpoint %s: %v", req.URL, s.Storers) rw.Header().Set("Cache-Status", cacheName+"; fwd=bypass; detail=DEADLINE-EXCEEDED") _, _ = customWriter.Rw.Write([]byte("Internal server error")) return baseCtx.DeadlineExceeded diff --git a/pkg/storage/defaultProvider.go b/pkg/storage/defaultProvider.go index 8eb9ebf05..ee5b7a808 100644 --- a/pkg/storage/defaultProvider.go +++ b/pkg/storage/defaultProvider.go @@ -12,14 +12,13 @@ import ( "github.com/darkweak/souin/pkg/storage/types" "github.com/darkweak/storages/core" "github.com/pierrec/lz4/v4" - "go.uber.org/zap" ) // Default provider type type Default struct { m *sync.Map stale time.Duration - logger *zap.Logger + logger core.Logger } type item struct { @@ -135,7 +134,7 @@ func (provider *Default) SetMultiLevel(baseKey, variedKey string, value []byte, var e error compressed := new(bytes.Buffer) if _, e = lz4.NewWriter(compressed).ReadFrom(bytes.NewReader(value)); e != nil { - provider.logger.Sugar().Errorf("Impossible to compress the key %s into Badger, %v", variedKey, e) + provider.logger.Errorf("Impossible to compress the key %s into Badger, %v", variedKey, e) return e } @@ -156,7 +155,7 @@ func (provider *Default) SetMultiLevel(baseKey, variedKey string, value []byte, return e } - provider.logger.Sugar().Debugf("Store the new mapping for the key %s in Default", variedKey) + provider.logger.Debugf("Store the new mapping for the key %s in Default", variedKey) provider.m.Store(mappingKey, val) return nil } diff --git a/pkg/surrogate/providers/common.go b/pkg/surrogate/providers/common.go index 311cf1ebf..dc5786721 100644 --- a/pkg/surrogate/providers/common.go +++ b/pkg/surrogate/providers/common.go @@ -11,7 +11,6 @@ import ( "github.com/darkweak/souin/configurationtypes" "github.com/darkweak/souin/pkg/storage/types" "github.com/darkweak/storages/core" - "go.uber.org/zap" ) const ( @@ -95,7 +94,7 @@ type baseStorage struct { keysRegexp map[string]keysRegexpInner dynamic bool keepStale bool - logger *zap.Logger + logger core.Logger mu *sync.Mutex duration time.Duration } @@ -106,7 +105,7 @@ func (s *baseStorage) init(config configurationtypes.AbstractConfigurationInterf if storer == nil { storer = core.GetRegisteredStorer(types.DefaultStorageName + "-") if storer == nil { - config.GetLogger().Sugar().Errorf("Impossible to retrieve the storers %s for the surrogate-keys from it's configuration", configuration.SurrogateConfiguration.Storer) + config.GetLogger().Errorf("Impossible to retrieve the storers %s for the surrogate-keys from it's configuration", configuration.SurrogateConfiguration.Storer) } } @@ -114,10 +113,10 @@ func (s *baseStorage) init(config configurationtypes.AbstractConfigurationInterf } else { storer := core.GetRegisteredStorer(defaultStorerName) if storer == nil { - config.GetLogger().Sugar().Errorf("Impossible to retrieve the storers %s for the surrogate-keys fallback to the default storage", configuration.SurrogateConfiguration.Storer) + config.GetLogger().Errorf("Impossible to retrieve the storers %s for the surrogate-keys fallback to the default storage", configuration.SurrogateConfiguration.Storer) storer = core.GetRegisteredStorer(types.DefaultStorageName + "-") if storer == nil { - config.GetLogger().Sugar().Error("Impossible to retrieve the default storer") + config.GetLogger().Error("Impossible to retrieve the default storer") } } @@ -159,7 +158,7 @@ func (s *baseStorage) storeTag(tag string, cacheKey string, re *regexp.Regexp) { s.mu.Lock() currentValue := string(s.Storage.Get(surrogatePrefix + tag)) if !re.MatchString(currentValue) { - s.logger.Sugar().Debugf("Store the tag %s", tag) + s.logger.Debugf("Store the tag %s", tag) _ = s.Storage.Set(surrogatePrefix+tag, []byte(currentValue+souinStorageSeparator+cacheKey), s.duration) } } @@ -200,7 +199,7 @@ func (s *baseStorage) getSurrogateKey(header http.Header) string { func (s *baseStorage) purgeTag(tag string) []string { toInvalidate := string(s.Storage.Get(surrogatePrefix + tag)) - s.logger.Sugar().Debugf("Purge the tag %s", tag) + s.logger.Debugf("Purge the tag %s", tag) if !s.keepStale { s.Storage.Delete(surrogatePrefix + tag) } @@ -246,7 +245,7 @@ func (s *baseStorage) Purge(header http.Header) (cacheKeys []string, surrogateKe toInvalidate = append(toInvalidate, s.purgeTag(su)...) } - s.logger.Sugar().Debugf("Purge the following tags: %+v", toInvalidate) + s.logger.Debugf("Purge the following tags: %+v", toInvalidate) return uniqueTag(toInvalidate), surrogates } diff --git a/pkg/surrogate/providers/common_test.go b/pkg/surrogate/providers/common_test.go index 147d07853..584f98cb2 100644 --- a/pkg/surrogate/providers/common_test.go +++ b/pkg/surrogate/providers/common_test.go @@ -31,7 +31,7 @@ func mockCommonProvider() *baseStorage { keysRegexp: make(map[string]keysRegexpInner), dynamic: true, mu: &sync.Mutex{}, - logger: zap.NewNop(), + logger: zap.NewNop().Sugar(), }, } diff --git a/pkg/surrogate/providers/factory_test.go b/pkg/surrogate/providers/factory_test.go index 6620eb599..957c17294 100644 --- a/pkg/surrogate/providers/factory_test.go +++ b/pkg/surrogate/providers/factory_test.go @@ -61,10 +61,10 @@ func (*testConfiguration) GetAPI() configurationtypes.API { func (*testConfiguration) GetLogLevel() string { return "" } -func (*testConfiguration) GetLogger() *zap.Logger { - return zap.NewNop() +func (*testConfiguration) GetLogger() core.Logger { + return zap.NewNop().Sugar() } -func (*testConfiguration) SetLogger(*zap.Logger) { +func (*testConfiguration) SetLogger(core.Logger) { } func (*testConfiguration) GetYkeys() map[string]configurationtypes.SurrogateKeys { return nil @@ -100,7 +100,7 @@ func mockConfiguration(configurationToLoad func() string) *testConfiguration { }, } logger, _ := cfg.Build() - config.SetLogger(logger) + config.SetLogger(logger.Sugar()) return &config } diff --git a/plugins/beego/go.mod b/plugins/beego/go.mod index 36b74c23e..7d35779e2 100644 --- a/plugins/beego/go.mod +++ b/plugins/beego/go.mod @@ -41,7 +41,7 @@ require ( github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect github.com/darkweak/go-esi v0.0.5 // indirect github.com/darkweak/storages/badger v0.0.4 // indirect - github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 // indirect + github.com/darkweak/storages/core v0.0.6 // indirect github.com/darkweak/storages/etcd v0.0.4 // indirect github.com/darkweak/storages/nats v0.0.5 // indirect github.com/darkweak/storages/nuts v0.0.4 // indirect diff --git a/plugins/beego/go.sum b/plugins/beego/go.sum index 812c75ca0..ab6f171ae 100644 --- a/plugins/beego/go.sum +++ b/plugins/beego/go.sum @@ -143,8 +143,8 @@ github.com/darkweak/go-esi v0.0.5 h1:b9LHI8Tz46R+i6p8avKPHAIBRQUCZDebNmKm5w/Zrns github.com/darkweak/go-esi v0.0.5/go.mod h1:koCJqwum1u6mslyZuq/Phm6hfG1K3ZK5Y7jrUBTH654= github.com/darkweak/storages/badger v0.0.4 h1:m6VSwrsKQ17+7/ZDOiEuQT8IHmr+WW5Y6ZfgTKFxIxM= github.com/darkweak/storages/badger v0.0.4/go.mod h1:v/U7iLG6qYT0qp9IKLmr/m6doBaQNcSJxvB3UlfsIbU= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 h1:WsLHVD+WDxYh5SuZzpZXnhnDruZVrrvnYsHw08izs1s= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881/go.mod h1:mWN/TO5aLYt2jK6EweZ4JFXXPYmYwYFUQ4YBuIlBkpc= +github.com/darkweak/storages/core v0.0.6 h1:Yj0Z98HsC9Kd9cmjAXLagx6aU7AtXuL++RV2W+FSjc0= +github.com/darkweak/storages/core v0.0.6/go.mod h1:ajTpB9IFLRIRY0EEFLjM5vtsrcNTh+TJK9yRxgG5/wY= github.com/darkweak/storages/etcd v0.0.4 h1:IdghlFAP+uv2sErKgtDfccPWQmVrlqk3GlvFVuRvdz4= github.com/darkweak/storages/etcd v0.0.4/go.mod h1:kBMLaPQDZBrxPQqS1u+oooNwWfP6Y3qUHnNCLnfLcIA= github.com/darkweak/storages/nats v0.0.5 h1:IKTx6teTIFSMvv7kRXz1jbSDeaJjE0BmB0GKe1XW3Mw= diff --git a/plugins/caddy/cleaner.go b/plugins/caddy/cleaner.go index da9db8e81..d736ffa14 100644 --- a/plugins/caddy/cleaner.go +++ b/plugins/caddy/cleaner.go @@ -28,7 +28,7 @@ func (s *storage_providers) Add(key interface{}) { } func (s *SouinCaddyMiddleware) Cleanup() error { - s.logger.Sugar().Debug("Cleanup...") + s.logger.Debug("Cleanup...") td := []interface{}{} sp, _ := up.LoadOrStore(stored_providers_key, newStorageProvider()) stored_providers := sp.(*storage_providers) @@ -43,7 +43,7 @@ func (s *SouinCaddyMiddleware) Cleanup() error { }) for _, v := range td { - s.logger.Sugar().Debugf("Cleaning %v\n", v) + s.logger.Debugf("Cleaning %v\n", v) _, _ = up.Delete(v) } diff --git a/plugins/caddy/configuration.go b/plugins/caddy/configuration.go index d0486abb7..3e35ddf3f 100644 --- a/plugins/caddy/configuration.go +++ b/plugins/caddy/configuration.go @@ -8,7 +8,7 @@ import ( "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" "github.com/darkweak/souin/configurationtypes" - "go.uber.org/zap" + "github.com/darkweak/storages/core" ) // DefaultCache the struct @@ -182,7 +182,7 @@ type Configuration struct { LogLevel string // SurrogateKeys contains the surrogate keys to use with a predefined mapping SurrogateKeys map[string]configurationtypes.SurrogateKeys - logger *zap.Logger + logger core.Logger } // GetUrls get the urls list in the configuration @@ -211,12 +211,12 @@ func (c *Configuration) GetLogLevel() string { } // GetLogger get the logger -func (c *Configuration) GetLogger() *zap.Logger { +func (c *Configuration) GetLogger() core.Logger { return c.logger } // SetLogger set the logger -func (c *Configuration) SetLogger(l *zap.Logger) { +func (c *Configuration) SetLogger(l core.Logger) { c.logger = l } diff --git a/plugins/caddy/dispatch.go b/plugins/caddy/dispatch.go index 23fbeae6a..17ab2e2fb 100644 --- a/plugins/caddy/dispatch.go +++ b/plugins/caddy/dispatch.go @@ -11,7 +11,7 @@ func (s *SouinCaddyMiddleware) parseStorages(ctx caddy.Context) { if s.Configuration.DefaultCache.Badger.Found { e := dispatchStorage(ctx, "badger", s.Configuration.DefaultCache.Badger, s.Configuration.DefaultCache.GetStale()) if e != nil { - s.logger.Sugar().Errorf("Error during Badger init, did you include the Badger storage (--with github.com/darkweak/storages/badger/caddy)? %v", e) + s.logger.Errorf("Error during Badger init, did you include the Badger storage (--with github.com/darkweak/storages/badger/caddy)? %v", e) } else { badger := s.Configuration.DefaultCache.Badger dir := "" @@ -39,7 +39,7 @@ func (s *SouinCaddyMiddleware) parseStorages(ctx caddy.Context) { if s.Configuration.DefaultCache.Etcd.Found { e := dispatchStorage(ctx, "etcd", s.Configuration.DefaultCache.Etcd, s.Configuration.DefaultCache.GetStale()) if e != nil { - s.logger.Sugar().Errorf("Error during Etcd init, did you include the Etcd storage (--with github.com/darkweak/storages/etcd/caddy)? %v", e) + s.logger.Errorf("Error during Etcd init, did you include the Etcd storage (--with github.com/darkweak/storages/etcd/caddy)? %v", e) } else { etcd := s.Configuration.DefaultCache.Etcd endpoints := etcd.URL @@ -71,7 +71,7 @@ func (s *SouinCaddyMiddleware) parseStorages(ctx caddy.Context) { if s.Configuration.DefaultCache.Nats.Found { e := dispatchStorage(ctx, "nats", s.Configuration.DefaultCache.Nats, s.Configuration.DefaultCache.GetStale()) if e != nil { - s.logger.Sugar().Errorf("Error during Nats init, did you include the Nats storage (--with github.com/darkweak/storages/nats/caddy)? %v", e) + s.logger.Errorf("Error during Nats init, did you include the Nats storage (--with github.com/darkweak/storages/nats/caddy)? %v", e) } else { s.Configuration.DefaultCache.Nuts.Uuid = fmt.Sprintf("NATS-%s-%s", s.Configuration.DefaultCache.Nats.URL, s.Configuration.DefaultCache.GetStale()) } @@ -79,7 +79,7 @@ func (s *SouinCaddyMiddleware) parseStorages(ctx caddy.Context) { if s.Configuration.DefaultCache.Nuts.Found { e := dispatchStorage(ctx, "nuts", s.Configuration.DefaultCache.Nuts, s.Configuration.DefaultCache.GetStale()) if e != nil { - s.logger.Sugar().Errorf("Error during Nuts init, did you include the Nuts storage (--with github.com/darkweak/storages/nuts/caddy)? %v", e) + s.logger.Errorf("Error during Nuts init, did you include the Nuts storage (--with github.com/darkweak/storages/nuts/caddy)? %v", e) } else { nuts := s.Configuration.DefaultCache.Nuts dir := "/tmp/souin-nuts" @@ -99,7 +99,7 @@ func (s *SouinCaddyMiddleware) parseStorages(ctx caddy.Context) { if s.Configuration.DefaultCache.Olric.Found { e := dispatchStorage(ctx, "olric", s.Configuration.DefaultCache.Olric, s.Configuration.DefaultCache.GetStale()) if e != nil { - s.logger.Sugar().Errorf("Error during Olric init, did you include the Olric storage (--with github.com/darkweak/storages/olric/caddy)? %v", e) + s.logger.Errorf("Error during Olric init, did you include the Olric storage (--with github.com/darkweak/storages/olric/caddy)? %v", e) } else { s.Configuration.DefaultCache.Nuts.Uuid = fmt.Sprintf("OLRIC-%s-%s", s.Configuration.DefaultCache.Olric.URL, s.Configuration.DefaultCache.GetStale()) } @@ -107,7 +107,7 @@ func (s *SouinCaddyMiddleware) parseStorages(ctx caddy.Context) { if s.Configuration.DefaultCache.Otter.Found { e := dispatchStorage(ctx, "otter", s.Configuration.DefaultCache.Otter, s.Configuration.DefaultCache.GetStale()) if e != nil { - s.logger.Sugar().Errorf("Error during Otter init, did you include the Otter storage (--with github.com/darkweak/storages/otter/caddy)? %v", e) + s.logger.Errorf("Error during Otter init, did you include the Otter storage (--with github.com/darkweak/storages/otter/caddy)? %v", e) } else { s.Configuration.DefaultCache.Otter.Uuid = fmt.Sprintf("OTTER-%s", s.Configuration.DefaultCache.GetStale()) } @@ -115,7 +115,7 @@ func (s *SouinCaddyMiddleware) parseStorages(ctx caddy.Context) { if s.Configuration.DefaultCache.Redis.Found { e := dispatchStorage(ctx, "redis", s.Configuration.DefaultCache.Redis, s.Configuration.DefaultCache.GetStale()) if e != nil { - s.logger.Sugar().Errorf("Error during Redis init, did you include the Redis storage (--with github.com/darkweak/storages/redis/caddy or github.com/darkweak/storages/go-redis/caddy)? %v", e) + s.logger.Errorf("Error during Redis init, did you include the Redis storage (--with github.com/darkweak/storages/redis/caddy or github.com/darkweak/storages/go-redis/caddy)? %v", e) } else { redis := s.Configuration.DefaultCache.Redis address := redis.URL diff --git a/plugins/caddy/go.mod b/plugins/caddy/go.mod index 5c92098ee..08a46e0a6 100644 --- a/plugins/caddy/go.mod +++ b/plugins/caddy/go.mod @@ -5,7 +5,7 @@ go 1.22.1 require ( github.com/caddyserver/caddy/v2 v2.8.4 github.com/darkweak/souin v1.6.49 - github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 + github.com/darkweak/storages/core v0.0.6 go.uber.org/zap v1.27.0 ) diff --git a/plugins/caddy/go.sum b/plugins/caddy/go.sum index 6c3ea5489..ca634d643 100644 --- a/plugins/caddy/go.sum +++ b/plugins/caddy/go.sum @@ -109,8 +109,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= github.com/darkweak/go-esi v0.0.5 h1:b9LHI8Tz46R+i6p8avKPHAIBRQUCZDebNmKm5w/Zrns= github.com/darkweak/go-esi v0.0.5/go.mod h1:koCJqwum1u6mslyZuq/Phm6hfG1K3ZK5Y7jrUBTH654= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 h1:WsLHVD+WDxYh5SuZzpZXnhnDruZVrrvnYsHw08izs1s= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881/go.mod h1:mWN/TO5aLYt2jK6EweZ4JFXXPYmYwYFUQ4YBuIlBkpc= +github.com/darkweak/storages/core v0.0.6 h1:Yj0Z98HsC9Kd9cmjAXLagx6aU7AtXuL++RV2W+FSjc0= +github.com/darkweak/storages/core v0.0.6/go.mod h1:ajTpB9IFLRIRY0EEFLjM5vtsrcNTh+TJK9yRxgG5/wY= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/plugins/caddy/httpcache.go b/plugins/caddy/httpcache.go index b94bc2f24..5687cf76d 100644 --- a/plugins/caddy/httpcache.go +++ b/plugins/caddy/httpcache.go @@ -14,7 +14,6 @@ import ( "github.com/darkweak/souin/pkg/middleware" surrogates_providers "github.com/darkweak/souin/pkg/surrogate/providers" "github.com/darkweak/storages/core" - "go.uber.org/zap" ) const moduleName = "cache" @@ -34,7 +33,7 @@ func init() { // storage, key generation tweaking. type SouinCaddyMiddleware struct { *middleware.SouinBaseHandler - logger *zap.Logger + logger core.Logger cacheKeys configurationtypes.CacheKeys Configuration Configuration // Logger level, fallback on caddy's one when not redefined. @@ -232,7 +231,7 @@ func dispatchStorage(ctx caddy.Context, name string, provider configurationtypes // Provision to do the provisioning part. func (s *SouinCaddyMiddleware) Provision(ctx caddy.Context) error { - s.logger = ctx.Logger(s) + s.logger = ctx.Logger(s).Sugar() if err := s.configurationPropertyMapper(); err != nil { return err diff --git a/plugins/chi/go.mod b/plugins/chi/go.mod index e2c1c8fa0..a8368ce52 100644 --- a/plugins/chi/go.mod +++ b/plugins/chi/go.mod @@ -38,7 +38,7 @@ require ( github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect github.com/darkweak/go-esi v0.0.6 // indirect github.com/darkweak/storages/badger v0.0.4 // indirect - github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 // indirect + github.com/darkweak/storages/core v0.0.6 // indirect github.com/darkweak/storages/etcd v0.0.4 // indirect github.com/darkweak/storages/nats v0.0.5 // indirect github.com/darkweak/storages/nuts v0.0.4 // indirect diff --git a/plugins/chi/go.sum b/plugins/chi/go.sum index 9b6a8220a..653c46f40 100644 --- a/plugins/chi/go.sum +++ b/plugins/chi/go.sum @@ -141,8 +141,8 @@ github.com/darkweak/go-esi v0.0.6 h1:eVHCJfqrZwOHPfRK7JTlSYG9F8lfpX/d4lz/41RQkd8 github.com/darkweak/go-esi v0.0.6/go.mod h1:IJSayeQZDUh5R5ayyDC3wUEBykti12aUa0eUxZZeodk= github.com/darkweak/storages/badger v0.0.4 h1:m6VSwrsKQ17+7/ZDOiEuQT8IHmr+WW5Y6ZfgTKFxIxM= github.com/darkweak/storages/badger v0.0.4/go.mod h1:v/U7iLG6qYT0qp9IKLmr/m6doBaQNcSJxvB3UlfsIbU= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 h1:WsLHVD+WDxYh5SuZzpZXnhnDruZVrrvnYsHw08izs1s= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881/go.mod h1:mWN/TO5aLYt2jK6EweZ4JFXXPYmYwYFUQ4YBuIlBkpc= +github.com/darkweak/storages/core v0.0.6 h1:Yj0Z98HsC9Kd9cmjAXLagx6aU7AtXuL++RV2W+FSjc0= +github.com/darkweak/storages/core v0.0.6/go.mod h1:ajTpB9IFLRIRY0EEFLjM5vtsrcNTh+TJK9yRxgG5/wY= github.com/darkweak/storages/etcd v0.0.4 h1:IdghlFAP+uv2sErKgtDfccPWQmVrlqk3GlvFVuRvdz4= github.com/darkweak/storages/etcd v0.0.4/go.mod h1:kBMLaPQDZBrxPQqS1u+oooNwWfP6Y3qUHnNCLnfLcIA= github.com/darkweak/storages/nats v0.0.5 h1:IKTx6teTIFSMvv7kRXz1jbSDeaJjE0BmB0GKe1XW3Mw= diff --git a/plugins/dotweb/go.mod b/plugins/dotweb/go.mod index 73f142f6c..69e504484 100644 --- a/plugins/dotweb/go.mod +++ b/plugins/dotweb/go.mod @@ -38,7 +38,7 @@ require ( github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect github.com/darkweak/go-esi v0.0.6 // indirect github.com/darkweak/storages/badger v0.0.4 // indirect - github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 // indirect + github.com/darkweak/storages/core v0.0.6 // indirect github.com/darkweak/storages/etcd v0.0.4 // indirect github.com/darkweak/storages/nats v0.0.5 // indirect github.com/darkweak/storages/nuts v0.0.4 // indirect diff --git a/plugins/dotweb/go.sum b/plugins/dotweb/go.sum index d0bedc643..75864e4e3 100644 --- a/plugins/dotweb/go.sum +++ b/plugins/dotweb/go.sum @@ -141,8 +141,8 @@ github.com/darkweak/go-esi v0.0.6 h1:eVHCJfqrZwOHPfRK7JTlSYG9F8lfpX/d4lz/41RQkd8 github.com/darkweak/go-esi v0.0.6/go.mod h1:IJSayeQZDUh5R5ayyDC3wUEBykti12aUa0eUxZZeodk= github.com/darkweak/storages/badger v0.0.4 h1:m6VSwrsKQ17+7/ZDOiEuQT8IHmr+WW5Y6ZfgTKFxIxM= github.com/darkweak/storages/badger v0.0.4/go.mod h1:v/U7iLG6qYT0qp9IKLmr/m6doBaQNcSJxvB3UlfsIbU= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 h1:WsLHVD+WDxYh5SuZzpZXnhnDruZVrrvnYsHw08izs1s= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881/go.mod h1:mWN/TO5aLYt2jK6EweZ4JFXXPYmYwYFUQ4YBuIlBkpc= +github.com/darkweak/storages/core v0.0.6 h1:Yj0Z98HsC9Kd9cmjAXLagx6aU7AtXuL++RV2W+FSjc0= +github.com/darkweak/storages/core v0.0.6/go.mod h1:ajTpB9IFLRIRY0EEFLjM5vtsrcNTh+TJK9yRxgG5/wY= github.com/darkweak/storages/etcd v0.0.4 h1:IdghlFAP+uv2sErKgtDfccPWQmVrlqk3GlvFVuRvdz4= github.com/darkweak/storages/etcd v0.0.4/go.mod h1:kBMLaPQDZBrxPQqS1u+oooNwWfP6Y3qUHnNCLnfLcIA= github.com/darkweak/storages/nats v0.0.5 h1:IKTx6teTIFSMvv7kRXz1jbSDeaJjE0BmB0GKe1XW3Mw= diff --git a/plugins/echo/go.mod b/plugins/echo/go.mod index 3d39667f0..604ae505f 100644 --- a/plugins/echo/go.mod +++ b/plugins/echo/go.mod @@ -5,7 +5,7 @@ go 1.22.1 require ( github.com/darkweak/souin v1.6.49 github.com/darkweak/souin/plugins/souin/storages v0.0.0-00010101000000-000000000000 - github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 + github.com/darkweak/storages/core v0.0.6 github.com/labstack/echo/v4 v4.11.1 ) diff --git a/plugins/echo/go.sum b/plugins/echo/go.sum index 242699cb5..3860eda80 100644 --- a/plugins/echo/go.sum +++ b/plugins/echo/go.sum @@ -141,8 +141,8 @@ github.com/darkweak/go-esi v0.0.6 h1:eVHCJfqrZwOHPfRK7JTlSYG9F8lfpX/d4lz/41RQkd8 github.com/darkweak/go-esi v0.0.6/go.mod h1:IJSayeQZDUh5R5ayyDC3wUEBykti12aUa0eUxZZeodk= github.com/darkweak/storages/badger v0.0.4 h1:m6VSwrsKQ17+7/ZDOiEuQT8IHmr+WW5Y6ZfgTKFxIxM= github.com/darkweak/storages/badger v0.0.4/go.mod h1:v/U7iLG6qYT0qp9IKLmr/m6doBaQNcSJxvB3UlfsIbU= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 h1:WsLHVD+WDxYh5SuZzpZXnhnDruZVrrvnYsHw08izs1s= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881/go.mod h1:mWN/TO5aLYt2jK6EweZ4JFXXPYmYwYFUQ4YBuIlBkpc= +github.com/darkweak/storages/core v0.0.6 h1:Yj0Z98HsC9Kd9cmjAXLagx6aU7AtXuL++RV2W+FSjc0= +github.com/darkweak/storages/core v0.0.6/go.mod h1:ajTpB9IFLRIRY0EEFLjM5vtsrcNTh+TJK9yRxgG5/wY= github.com/darkweak/storages/etcd v0.0.4 h1:IdghlFAP+uv2sErKgtDfccPWQmVrlqk3GlvFVuRvdz4= github.com/darkweak/storages/etcd v0.0.4/go.mod h1:kBMLaPQDZBrxPQqS1u+oooNwWfP6Y3qUHnNCLnfLcIA= github.com/darkweak/storages/nats v0.0.5 h1:IKTx6teTIFSMvv7kRXz1jbSDeaJjE0BmB0GKe1XW3Mw= diff --git a/plugins/fiber/go.mod b/plugins/fiber/go.mod index abf240537..60a51d9a0 100644 --- a/plugins/fiber/go.mod +++ b/plugins/fiber/go.mod @@ -40,7 +40,7 @@ require ( github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect github.com/darkweak/go-esi v0.0.6 // indirect github.com/darkweak/storages/badger v0.0.4 // indirect - github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 // indirect + github.com/darkweak/storages/core v0.0.6 // indirect github.com/darkweak/storages/etcd v0.0.4 // indirect github.com/darkweak/storages/nats v0.0.5 // indirect github.com/darkweak/storages/nuts v0.0.4 // indirect diff --git a/plugins/fiber/go.sum b/plugins/fiber/go.sum index 2bb2078c8..d552e8ba1 100644 --- a/plugins/fiber/go.sum +++ b/plugins/fiber/go.sum @@ -143,8 +143,8 @@ github.com/darkweak/go-esi v0.0.6 h1:eVHCJfqrZwOHPfRK7JTlSYG9F8lfpX/d4lz/41RQkd8 github.com/darkweak/go-esi v0.0.6/go.mod h1:IJSayeQZDUh5R5ayyDC3wUEBykti12aUa0eUxZZeodk= github.com/darkweak/storages/badger v0.0.4 h1:m6VSwrsKQ17+7/ZDOiEuQT8IHmr+WW5Y6ZfgTKFxIxM= github.com/darkweak/storages/badger v0.0.4/go.mod h1:v/U7iLG6qYT0qp9IKLmr/m6doBaQNcSJxvB3UlfsIbU= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 h1:WsLHVD+WDxYh5SuZzpZXnhnDruZVrrvnYsHw08izs1s= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881/go.mod h1:mWN/TO5aLYt2jK6EweZ4JFXXPYmYwYFUQ4YBuIlBkpc= +github.com/darkweak/storages/core v0.0.6 h1:Yj0Z98HsC9Kd9cmjAXLagx6aU7AtXuL++RV2W+FSjc0= +github.com/darkweak/storages/core v0.0.6/go.mod h1:ajTpB9IFLRIRY0EEFLjM5vtsrcNTh+TJK9yRxgG5/wY= github.com/darkweak/storages/etcd v0.0.4 h1:IdghlFAP+uv2sErKgtDfccPWQmVrlqk3GlvFVuRvdz4= github.com/darkweak/storages/etcd v0.0.4/go.mod h1:kBMLaPQDZBrxPQqS1u+oooNwWfP6Y3qUHnNCLnfLcIA= github.com/darkweak/storages/nats v0.0.5 h1:IKTx6teTIFSMvv7kRXz1jbSDeaJjE0BmB0GKe1XW3Mw= diff --git a/plugins/gin/go.mod b/plugins/gin/go.mod index 808badcc4..5d1c875c7 100644 --- a/plugins/gin/go.mod +++ b/plugins/gin/go.mod @@ -41,7 +41,7 @@ require ( github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect github.com/darkweak/go-esi v0.0.6 // indirect github.com/darkweak/storages/badger v0.0.4 // indirect - github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 // indirect + github.com/darkweak/storages/core v0.0.6 // indirect github.com/darkweak/storages/etcd v0.0.4 // indirect github.com/darkweak/storages/nats v0.0.5 // indirect github.com/darkweak/storages/nuts v0.0.4 // indirect diff --git a/plugins/gin/go.sum b/plugins/gin/go.sum index a60fd96b6..11b0c70ee 100644 --- a/plugins/gin/go.sum +++ b/plugins/gin/go.sum @@ -151,8 +151,8 @@ github.com/darkweak/go-esi v0.0.6 h1:eVHCJfqrZwOHPfRK7JTlSYG9F8lfpX/d4lz/41RQkd8 github.com/darkweak/go-esi v0.0.6/go.mod h1:IJSayeQZDUh5R5ayyDC3wUEBykti12aUa0eUxZZeodk= github.com/darkweak/storages/badger v0.0.4 h1:m6VSwrsKQ17+7/ZDOiEuQT8IHmr+WW5Y6ZfgTKFxIxM= github.com/darkweak/storages/badger v0.0.4/go.mod h1:v/U7iLG6qYT0qp9IKLmr/m6doBaQNcSJxvB3UlfsIbU= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 h1:WsLHVD+WDxYh5SuZzpZXnhnDruZVrrvnYsHw08izs1s= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881/go.mod h1:mWN/TO5aLYt2jK6EweZ4JFXXPYmYwYFUQ4YBuIlBkpc= +github.com/darkweak/storages/core v0.0.6 h1:Yj0Z98HsC9Kd9cmjAXLagx6aU7AtXuL++RV2W+FSjc0= +github.com/darkweak/storages/core v0.0.6/go.mod h1:ajTpB9IFLRIRY0EEFLjM5vtsrcNTh+TJK9yRxgG5/wY= github.com/darkweak/storages/etcd v0.0.4 h1:IdghlFAP+uv2sErKgtDfccPWQmVrlqk3GlvFVuRvdz4= github.com/darkweak/storages/etcd v0.0.4/go.mod h1:kBMLaPQDZBrxPQqS1u+oooNwWfP6Y3qUHnNCLnfLcIA= github.com/darkweak/storages/nats v0.0.5 h1:IKTx6teTIFSMvv7kRXz1jbSDeaJjE0BmB0GKe1XW3Mw= diff --git a/plugins/go-zero/go.mod b/plugins/go-zero/go.mod index 08bd9374c..e493a437f 100644 --- a/plugins/go-zero/go.mod +++ b/plugins/go-zero/go.mod @@ -39,7 +39,7 @@ require ( github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect github.com/darkweak/go-esi v0.0.5 // indirect github.com/darkweak/storages/badger v0.0.4 // indirect - github.com/darkweak/storages/core v0.0.5 // indirect + github.com/darkweak/storages/core v0.0.6 // indirect github.com/darkweak/storages/etcd v0.0.4 // indirect github.com/darkweak/storages/nats v0.0.5 // indirect github.com/darkweak/storages/nuts v0.0.4 // indirect @@ -192,7 +192,7 @@ require ( google.golang.org/genproto/googleapis/api v0.0.0-20240506185236-b8a5c65736ae // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240429193739-8cf5692501f6 // indirect google.golang.org/grpc v1.64.0 // indirect - google.golang.org/protobuf v1.34.1 // indirect + google.golang.org/protobuf v1.34.2 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect howett.net/plist v1.0.0 // indirect diff --git a/plugins/go-zero/go.sum b/plugins/go-zero/go.sum index 20a4b1257..8153fb475 100644 --- a/plugins/go-zero/go.sum +++ b/plugins/go-zero/go.sum @@ -143,8 +143,8 @@ github.com/darkweak/go-esi v0.0.5 h1:b9LHI8Tz46R+i6p8avKPHAIBRQUCZDebNmKm5w/Zrns github.com/darkweak/go-esi v0.0.5/go.mod h1:koCJqwum1u6mslyZuq/Phm6hfG1K3ZK5Y7jrUBTH654= github.com/darkweak/storages/badger v0.0.4 h1:m6VSwrsKQ17+7/ZDOiEuQT8IHmr+WW5Y6ZfgTKFxIxM= github.com/darkweak/storages/badger v0.0.4/go.mod h1:v/U7iLG6qYT0qp9IKLmr/m6doBaQNcSJxvB3UlfsIbU= -github.com/darkweak/storages/core v0.0.5 h1:pRkzBvVjVVgEXTdUIfqq10ihPMfYveWfqRc2C9OGTwI= -github.com/darkweak/storages/core v0.0.5/go.mod h1:wLp1cOAB4WUd46BBOtV4Lwot4GD+8fZbtIw6QM7fYuc= +github.com/darkweak/storages/core v0.0.6 h1:Yj0Z98HsC9Kd9cmjAXLagx6aU7AtXuL++RV2W+FSjc0= +github.com/darkweak/storages/core v0.0.6/go.mod h1:ajTpB9IFLRIRY0EEFLjM5vtsrcNTh+TJK9yRxgG5/wY= github.com/darkweak/storages/etcd v0.0.4 h1:IdghlFAP+uv2sErKgtDfccPWQmVrlqk3GlvFVuRvdz4= github.com/darkweak/storages/etcd v0.0.4/go.mod h1:kBMLaPQDZBrxPQqS1u+oooNwWfP6Y3qUHnNCLnfLcIA= github.com/darkweak/storages/nats v0.0.5 h1:IKTx6teTIFSMvv7kRXz1jbSDeaJjE0BmB0GKe1XW3Mw= @@ -929,8 +929,8 @@ google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpAD google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg= -google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= +google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= +google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/plugins/goa/go.mod b/plugins/goa/go.mod index 848d82f54..a49782f6f 100644 --- a/plugins/goa/go.mod +++ b/plugins/goa/go.mod @@ -5,7 +5,7 @@ go 1.22.1 require ( github.com/darkweak/souin v1.6.49 github.com/darkweak/souin/plugins/souin/storages v0.0.0-00010101000000-000000000000 - github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 + github.com/darkweak/storages/core v0.0.6 goa.design/goa/v3 v3.12.3 ) diff --git a/plugins/goa/go.sum b/plugins/goa/go.sum index c77a63079..263f75072 100644 --- a/plugins/goa/go.sum +++ b/plugins/goa/go.sum @@ -141,8 +141,8 @@ github.com/darkweak/go-esi v0.0.6 h1:eVHCJfqrZwOHPfRK7JTlSYG9F8lfpX/d4lz/41RQkd8 github.com/darkweak/go-esi v0.0.6/go.mod h1:IJSayeQZDUh5R5ayyDC3wUEBykti12aUa0eUxZZeodk= github.com/darkweak/storages/badger v0.0.4 h1:m6VSwrsKQ17+7/ZDOiEuQT8IHmr+WW5Y6ZfgTKFxIxM= github.com/darkweak/storages/badger v0.0.4/go.mod h1:v/U7iLG6qYT0qp9IKLmr/m6doBaQNcSJxvB3UlfsIbU= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 h1:WsLHVD+WDxYh5SuZzpZXnhnDruZVrrvnYsHw08izs1s= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881/go.mod h1:mWN/TO5aLYt2jK6EweZ4JFXXPYmYwYFUQ4YBuIlBkpc= +github.com/darkweak/storages/core v0.0.6 h1:Yj0Z98HsC9Kd9cmjAXLagx6aU7AtXuL++RV2W+FSjc0= +github.com/darkweak/storages/core v0.0.6/go.mod h1:ajTpB9IFLRIRY0EEFLjM5vtsrcNTh+TJK9yRxgG5/wY= github.com/darkweak/storages/etcd v0.0.4 h1:IdghlFAP+uv2sErKgtDfccPWQmVrlqk3GlvFVuRvdz4= github.com/darkweak/storages/etcd v0.0.4/go.mod h1:kBMLaPQDZBrxPQqS1u+oooNwWfP6Y3qUHnNCLnfLcIA= github.com/darkweak/storages/nats v0.0.5 h1:IKTx6teTIFSMvv7kRXz1jbSDeaJjE0BmB0GKe1XW3Mw= diff --git a/plugins/goyave/go.mod b/plugins/goyave/go.mod index f6172fe4c..eb07872aa 100644 --- a/plugins/goyave/go.mod +++ b/plugins/goyave/go.mod @@ -39,7 +39,7 @@ require ( github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect github.com/darkweak/go-esi v0.0.6 // indirect github.com/darkweak/storages/badger v0.0.4 // indirect - github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 // indirect + github.com/darkweak/storages/core v0.0.6 // indirect github.com/darkweak/storages/etcd v0.0.4 // indirect github.com/darkweak/storages/nats v0.0.5 // indirect github.com/darkweak/storages/nuts v0.0.4 // indirect diff --git a/plugins/goyave/go.sum b/plugins/goyave/go.sum index ee8970653..dcbe3fcac 100644 --- a/plugins/goyave/go.sum +++ b/plugins/goyave/go.sum @@ -143,8 +143,8 @@ github.com/darkweak/go-esi v0.0.6 h1:eVHCJfqrZwOHPfRK7JTlSYG9F8lfpX/d4lz/41RQkd8 github.com/darkweak/go-esi v0.0.6/go.mod h1:IJSayeQZDUh5R5ayyDC3wUEBykti12aUa0eUxZZeodk= github.com/darkweak/storages/badger v0.0.4 h1:m6VSwrsKQ17+7/ZDOiEuQT8IHmr+WW5Y6ZfgTKFxIxM= github.com/darkweak/storages/badger v0.0.4/go.mod h1:v/U7iLG6qYT0qp9IKLmr/m6doBaQNcSJxvB3UlfsIbU= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 h1:WsLHVD+WDxYh5SuZzpZXnhnDruZVrrvnYsHw08izs1s= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881/go.mod h1:mWN/TO5aLYt2jK6EweZ4JFXXPYmYwYFUQ4YBuIlBkpc= +github.com/darkweak/storages/core v0.0.6 h1:Yj0Z98HsC9Kd9cmjAXLagx6aU7AtXuL++RV2W+FSjc0= +github.com/darkweak/storages/core v0.0.6/go.mod h1:ajTpB9IFLRIRY0EEFLjM5vtsrcNTh+TJK9yRxgG5/wY= github.com/darkweak/storages/etcd v0.0.4 h1:IdghlFAP+uv2sErKgtDfccPWQmVrlqk3GlvFVuRvdz4= github.com/darkweak/storages/etcd v0.0.4/go.mod h1:kBMLaPQDZBrxPQqS1u+oooNwWfP6Y3qUHnNCLnfLcIA= github.com/darkweak/storages/nats v0.0.5 h1:IKTx6teTIFSMvv7kRXz1jbSDeaJjE0BmB0GKe1XW3Mw= diff --git a/plugins/hertz/go.mod b/plugins/hertz/go.mod index ea3a374e3..c212bf22e 100644 --- a/plugins/hertz/go.mod +++ b/plugins/hertz/go.mod @@ -33,7 +33,7 @@ require ( github.com/coreos/go-systemd/v22 v22.5.0 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect github.com/darkweak/storages/badger v0.0.4 // indirect - github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 // indirect + github.com/darkweak/storages/core v0.0.6 // indirect github.com/darkweak/storages/etcd v0.0.4 // indirect github.com/darkweak/storages/nats v0.0.5 // indirect github.com/darkweak/storages/nuts v0.0.4 // indirect diff --git a/plugins/hertz/go.sum b/plugins/hertz/go.sum index 0a9b105e6..a786975ee 100644 --- a/plugins/hertz/go.sum +++ b/plugins/hertz/go.sum @@ -155,8 +155,8 @@ github.com/darkweak/go-esi v0.0.6 h1:eVHCJfqrZwOHPfRK7JTlSYG9F8lfpX/d4lz/41RQkd8 github.com/darkweak/go-esi v0.0.6/go.mod h1:IJSayeQZDUh5R5ayyDC3wUEBykti12aUa0eUxZZeodk= github.com/darkweak/storages/badger v0.0.4 h1:m6VSwrsKQ17+7/ZDOiEuQT8IHmr+WW5Y6ZfgTKFxIxM= github.com/darkweak/storages/badger v0.0.4/go.mod h1:v/U7iLG6qYT0qp9IKLmr/m6doBaQNcSJxvB3UlfsIbU= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 h1:WsLHVD+WDxYh5SuZzpZXnhnDruZVrrvnYsHw08izs1s= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881/go.mod h1:mWN/TO5aLYt2jK6EweZ4JFXXPYmYwYFUQ4YBuIlBkpc= +github.com/darkweak/storages/core v0.0.6 h1:Yj0Z98HsC9Kd9cmjAXLagx6aU7AtXuL++RV2W+FSjc0= +github.com/darkweak/storages/core v0.0.6/go.mod h1:ajTpB9IFLRIRY0EEFLjM5vtsrcNTh+TJK9yRxgG5/wY= github.com/darkweak/storages/etcd v0.0.4 h1:IdghlFAP+uv2sErKgtDfccPWQmVrlqk3GlvFVuRvdz4= github.com/darkweak/storages/etcd v0.0.4/go.mod h1:kBMLaPQDZBrxPQqS1u+oooNwWfP6Y3qUHnNCLnfLcIA= github.com/darkweak/storages/nats v0.0.5 h1:IKTx6teTIFSMvv7kRXz1jbSDeaJjE0BmB0GKe1XW3Mw= diff --git a/plugins/kratos/go.mod b/plugins/kratos/go.mod index f8b7c1169..0fb3193b9 100644 --- a/plugins/kratos/go.mod +++ b/plugins/kratos/go.mod @@ -39,7 +39,7 @@ require ( github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect github.com/darkweak/go-esi v0.0.6 // indirect github.com/darkweak/storages/badger v0.0.4 // indirect - github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 // indirect + github.com/darkweak/storages/core v0.0.6 // indirect github.com/darkweak/storages/etcd v0.0.4 // indirect github.com/darkweak/storages/nats v0.0.5 // indirect github.com/darkweak/storages/nuts v0.0.4 // indirect diff --git a/plugins/kratos/go.sum b/plugins/kratos/go.sum index 1791f570e..2a7abb8cd 100644 --- a/plugins/kratos/go.sum +++ b/plugins/kratos/go.sum @@ -141,8 +141,8 @@ github.com/darkweak/go-esi v0.0.6 h1:eVHCJfqrZwOHPfRK7JTlSYG9F8lfpX/d4lz/41RQkd8 github.com/darkweak/go-esi v0.0.6/go.mod h1:IJSayeQZDUh5R5ayyDC3wUEBykti12aUa0eUxZZeodk= github.com/darkweak/storages/badger v0.0.4 h1:m6VSwrsKQ17+7/ZDOiEuQT8IHmr+WW5Y6ZfgTKFxIxM= github.com/darkweak/storages/badger v0.0.4/go.mod h1:v/U7iLG6qYT0qp9IKLmr/m6doBaQNcSJxvB3UlfsIbU= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 h1:WsLHVD+WDxYh5SuZzpZXnhnDruZVrrvnYsHw08izs1s= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881/go.mod h1:mWN/TO5aLYt2jK6EweZ4JFXXPYmYwYFUQ4YBuIlBkpc= +github.com/darkweak/storages/core v0.0.6 h1:Yj0Z98HsC9Kd9cmjAXLagx6aU7AtXuL++RV2W+FSjc0= +github.com/darkweak/storages/core v0.0.6/go.mod h1:ajTpB9IFLRIRY0EEFLjM5vtsrcNTh+TJK9yRxgG5/wY= github.com/darkweak/storages/etcd v0.0.4 h1:IdghlFAP+uv2sErKgtDfccPWQmVrlqk3GlvFVuRvdz4= github.com/darkweak/storages/etcd v0.0.4/go.mod h1:kBMLaPQDZBrxPQqS1u+oooNwWfP6Y3qUHnNCLnfLcIA= github.com/darkweak/storages/nats v0.0.5 h1:IKTx6teTIFSMvv7kRXz1jbSDeaJjE0BmB0GKe1XW3Mw= diff --git a/plugins/roadrunner/go.mod b/plugins/roadrunner/go.mod index 4eb59fbcf..cdc6206f5 100644 --- a/plugins/roadrunner/go.mod +++ b/plugins/roadrunner/go.mod @@ -41,7 +41,7 @@ require ( github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect github.com/darkweak/go-esi v0.0.6 // indirect github.com/darkweak/storages/badger v0.0.4 // indirect - github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 // indirect + github.com/darkweak/storages/core v0.0.6 // indirect github.com/darkweak/storages/etcd v0.0.4 // indirect github.com/darkweak/storages/nats v0.0.5 // indirect github.com/darkweak/storages/nuts v0.0.4 // indirect diff --git a/plugins/roadrunner/go.sum b/plugins/roadrunner/go.sum index 3a62c9dfd..302409267 100644 --- a/plugins/roadrunner/go.sum +++ b/plugins/roadrunner/go.sum @@ -141,8 +141,8 @@ github.com/darkweak/go-esi v0.0.6 h1:eVHCJfqrZwOHPfRK7JTlSYG9F8lfpX/d4lz/41RQkd8 github.com/darkweak/go-esi v0.0.6/go.mod h1:IJSayeQZDUh5R5ayyDC3wUEBykti12aUa0eUxZZeodk= github.com/darkweak/storages/badger v0.0.4 h1:m6VSwrsKQ17+7/ZDOiEuQT8IHmr+WW5Y6ZfgTKFxIxM= github.com/darkweak/storages/badger v0.0.4/go.mod h1:v/U7iLG6qYT0qp9IKLmr/m6doBaQNcSJxvB3UlfsIbU= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 h1:WsLHVD+WDxYh5SuZzpZXnhnDruZVrrvnYsHw08izs1s= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881/go.mod h1:mWN/TO5aLYt2jK6EweZ4JFXXPYmYwYFUQ4YBuIlBkpc= +github.com/darkweak/storages/core v0.0.6 h1:Yj0Z98HsC9Kd9cmjAXLagx6aU7AtXuL++RV2W+FSjc0= +github.com/darkweak/storages/core v0.0.6/go.mod h1:ajTpB9IFLRIRY0EEFLjM5vtsrcNTh+TJK9yRxgG5/wY= github.com/darkweak/storages/etcd v0.0.4 h1:IdghlFAP+uv2sErKgtDfccPWQmVrlqk3GlvFVuRvdz4= github.com/darkweak/storages/etcd v0.0.4/go.mod h1:kBMLaPQDZBrxPQqS1u+oooNwWfP6Y3qUHnNCLnfLcIA= github.com/darkweak/storages/nats v0.0.5 h1:IKTx6teTIFSMvv7kRXz1jbSDeaJjE0BmB0GKe1XW3Mw= diff --git a/plugins/roadrunner/httpcache.go b/plugins/roadrunner/httpcache.go index 8b6faedf8..94db0c861 100644 --- a/plugins/roadrunner/httpcache.go +++ b/plugins/roadrunner/httpcache.go @@ -47,7 +47,7 @@ func (m *Plugin) Init(cfg Configurer, log Logger) error { } c := parseConfiguration(cfg) - c.SetLogger(log.NamedLogger(pluginName)) + c.SetLogger(log.NamedLogger(pluginName).Sugar()) storages.InitFromConfiguration(&c) m.SouinBaseHandler = middleware.NewHTTPCacheHandler(&c) diff --git a/plugins/skipper/go.mod b/plugins/skipper/go.mod index 5aa603d57..3cd1ba92d 100644 --- a/plugins/skipper/go.mod +++ b/plugins/skipper/go.mod @@ -48,7 +48,7 @@ require ( github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect github.com/darkweak/go-esi v0.0.6 // indirect github.com/darkweak/storages/badger v0.0.4 // indirect - github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 // indirect + github.com/darkweak/storages/core v0.0.6 // indirect github.com/darkweak/storages/etcd v0.0.4 // indirect github.com/darkweak/storages/nats v0.0.5 // indirect github.com/darkweak/storages/nuts v0.0.4 // indirect diff --git a/plugins/skipper/go.sum b/plugins/skipper/go.sum index c267ae25f..229bc2355 100644 --- a/plugins/skipper/go.sum +++ b/plugins/skipper/go.sum @@ -175,8 +175,8 @@ github.com/darkweak/go-esi v0.0.6 h1:eVHCJfqrZwOHPfRK7JTlSYG9F8lfpX/d4lz/41RQkd8 github.com/darkweak/go-esi v0.0.6/go.mod h1:IJSayeQZDUh5R5ayyDC3wUEBykti12aUa0eUxZZeodk= github.com/darkweak/storages/badger v0.0.4 h1:m6VSwrsKQ17+7/ZDOiEuQT8IHmr+WW5Y6ZfgTKFxIxM= github.com/darkweak/storages/badger v0.0.4/go.mod h1:v/U7iLG6qYT0qp9IKLmr/m6doBaQNcSJxvB3UlfsIbU= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 h1:WsLHVD+WDxYh5SuZzpZXnhnDruZVrrvnYsHw08izs1s= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881/go.mod h1:mWN/TO5aLYt2jK6EweZ4JFXXPYmYwYFUQ4YBuIlBkpc= +github.com/darkweak/storages/core v0.0.6 h1:Yj0Z98HsC9Kd9cmjAXLagx6aU7AtXuL++RV2W+FSjc0= +github.com/darkweak/storages/core v0.0.6/go.mod h1:ajTpB9IFLRIRY0EEFLjM5vtsrcNTh+TJK9yRxgG5/wY= github.com/darkweak/storages/etcd v0.0.4 h1:IdghlFAP+uv2sErKgtDfccPWQmVrlqk3GlvFVuRvdz4= github.com/darkweak/storages/etcd v0.0.4/go.mod h1:kBMLaPQDZBrxPQqS1u+oooNwWfP6Y3qUHnNCLnfLcIA= github.com/darkweak/storages/nats v0.0.5 h1:IKTx6teTIFSMvv7kRXz1jbSDeaJjE0BmB0GKe1XW3Mw= diff --git a/plugins/souin/configuration/configuration.go b/plugins/souin/configuration/configuration.go index a17abaf62..191ece8fc 100644 --- a/plugins/souin/configuration/configuration.go +++ b/plugins/souin/configuration/configuration.go @@ -5,7 +5,7 @@ import ( "os" "github.com/darkweak/souin/configurationtypes" - "go.uber.org/zap" + "github.com/darkweak/storages/core" yaml "gopkg.in/yaml.v3" ) @@ -18,7 +18,7 @@ type Configuration struct { SSLProviders []string `yaml:"ssl_providers"` URLs map[string]configurationtypes.URL `yaml:"urls"` LogLevel string `yaml:"log_level"` - logger *zap.Logger + logger core.Logger PluginName string Ykeys map[string]configurationtypes.SurrogateKeys `yaml:"ykeys"` SurrogateKeys map[string]configurationtypes.SurrogateKeys `yaml:"surrogate_keys"` @@ -76,12 +76,12 @@ func (c *Configuration) GetLogLevel() string { } // GetLogger get the logger -func (c *Configuration) GetLogger() *zap.Logger { +func (c *Configuration) GetLogger() core.Logger { return c.logger } // SetLogger set the logger -func (c *Configuration) SetLogger(l *zap.Logger) { +func (c *Configuration) SetLogger(l core.Logger) { c.logger = l } diff --git a/plugins/souin/storages/storages.go b/plugins/souin/storages/storages.go index 30f0ee0cf..07a20e912 100644 --- a/plugins/souin/storages/storages.go +++ b/plugins/souin/storages/storages.go @@ -16,7 +16,7 @@ import ( "go.uber.org/zap/zapcore" ) -type factory func(providerConfiguration core.CacheProvider, logger *zap.Logger, stale time.Duration) (core.Storer, error) +type factory func(providerConfiguration core.CacheProvider, logger core.Logger, stale time.Duration) (core.Storer, error) func isProviderEmpty(p configurationtypes.CacheProvider) bool { return p.Configuration == nil && p.Path == "" && p.URL == "" @@ -28,7 +28,7 @@ func toCoreCacheProvider(p configurationtypes.CacheProvider) core.CacheProvider URL: p.URL, } } -func tryToRegisterStorage(p configurationtypes.CacheProvider, f factory, logger *zap.Logger, stale time.Duration) { +func tryToRegisterStorage(p configurationtypes.CacheProvider, f factory, logger core.Logger, stale time.Duration) { if !isProviderEmpty(p) { if s, err := f(toCoreCacheProvider(p), logger, stale); err == nil { core.RegisterStorage(s) @@ -63,7 +63,7 @@ func InitFromConfiguration(configuration configurationtypes.AbstractConfiguratio }, } logger, _ := cfg.Build() - configuration.SetLogger(logger) + configuration.SetLogger(logger.Sugar()) } logger := configuration.GetLogger() stale := configuration.GetDefaultCache().GetStale() diff --git a/plugins/traefik/go.mod b/plugins/traefik/go.mod index f5a880492..2694348ab 100644 --- a/plugins/traefik/go.mod +++ b/plugins/traefik/go.mod @@ -22,7 +22,7 @@ require ( github.com/caddyserver/zerossl v0.1.3 // indirect github.com/chzyer/readline v1.5.1 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect - github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 // indirect + github.com/darkweak/storages/core v0.0.6 // indirect github.com/dgraph-io/badger v1.6.2 // indirect github.com/dgraph-io/badger/v2 v2.2007.4 // indirect github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect diff --git a/plugins/traefik/go.sum b/plugins/traefik/go.sum index 2cb440ffd..b14d244dd 100644 --- a/plugins/traefik/go.sum +++ b/plugins/traefik/go.sum @@ -100,8 +100,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= github.com/darkweak/go-esi v0.0.5 h1:b9LHI8Tz46R+i6p8avKPHAIBRQUCZDebNmKm5w/Zrns= github.com/darkweak/go-esi v0.0.5/go.mod h1:koCJqwum1u6mslyZuq/Phm6hfG1K3ZK5Y7jrUBTH654= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 h1:WsLHVD+WDxYh5SuZzpZXnhnDruZVrrvnYsHw08izs1s= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881/go.mod h1:mWN/TO5aLYt2jK6EweZ4JFXXPYmYwYFUQ4YBuIlBkpc= +github.com/darkweak/storages/core v0.0.6 h1:Yj0Z98HsC9Kd9cmjAXLagx6aU7AtXuL++RV2W+FSjc0= +github.com/darkweak/storages/core v0.0.6/go.mod h1:ajTpB9IFLRIRY0EEFLjM5vtsrcNTh+TJK9yRxgG5/wY= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/plugins/traefik/vendor/github.com/darkweak/souin/api/main.go b/plugins/traefik/vendor/github.com/darkweak/souin/api/main.go new file mode 100644 index 000000000..03230b136 --- /dev/null +++ b/plugins/traefik/vendor/github.com/darkweak/souin/api/main.go @@ -0,0 +1,50 @@ +package api + +import ( + "net/http" + + "github.com/darkweak/souin/configurationtypes" + "github.com/darkweak/souin/pkg/storage/types" + "github.com/darkweak/souin/pkg/surrogate/providers" +) + +// MapHandler is a map to store the available http Handlers +type MapHandler struct { + Handlers *map[string]http.HandlerFunc +} + +// GenerateHandlerMap generate the MapHandler +func GenerateHandlerMap( + configuration configurationtypes.AbstractConfigurationInterface, + storers []types.Storer, + surrogateStorage providers.SurrogateInterface, +) *MapHandler { + hm := make(map[string]http.HandlerFunc) + shouldEnable := false + + souinAPI := configuration.GetAPI() + basePathAPIS := souinAPI.BasePath + if basePathAPIS == "" { + basePathAPIS = "/souin-api" + } + + for _, endpoint := range Initialize(configuration, storers, surrogateStorage) { + if endpoint.IsEnabled() { + shouldEnable = true + if e, ok := endpoint.(*SouinAPI); ok { + hm[basePathAPIS+endpoint.GetBasePath()] = e.HandleRequest + } + } + } + + if shouldEnable { + return &MapHandler{Handlers: &hm} + } + + return nil +} + +// Initialize contains all apis that should be enabled +func Initialize(c configurationtypes.AbstractConfigurationInterface, storers []types.Storer, surrogateStorage providers.SurrogateInterface) []EndpointInterface { + return []EndpointInterface{initializeSouin(c, storers, surrogateStorage)} +} diff --git a/plugins/traefik/vendor/github.com/darkweak/souin/api/prometheus/prometheus.go b/plugins/traefik/vendor/github.com/darkweak/souin/api/prometheus/prometheus.go new file mode 100644 index 000000000..2712571d6 --- /dev/null +++ b/plugins/traefik/vendor/github.com/darkweak/souin/api/prometheus/prometheus.go @@ -0,0 +1,5 @@ +package prometheus + +const RequestCounter = "" + +func Increment(string) {} diff --git a/plugins/traefik/vendor/github.com/darkweak/souin/api/souin.go b/plugins/traefik/vendor/github.com/darkweak/souin/api/souin.go new file mode 100644 index 000000000..8f5305cb9 --- /dev/null +++ b/plugins/traefik/vendor/github.com/darkweak/souin/api/souin.go @@ -0,0 +1,233 @@ +package api + +import ( + "encoding/json" + "fmt" + "net/http" + "regexp" + "strings" + + "github.com/darkweak/souin/configurationtypes" + "github.com/darkweak/souin/pkg/storage/types" + "github.com/darkweak/souin/pkg/surrogate/providers" +) + +// SouinAPI object contains informations related to the endpoints +type SouinAPI struct { + basePath string + enabled bool + storers []types.Storer + surrogateStorage providers.SurrogateInterface + allowedMethods []string +} + +type invalidationType string + +const ( + uriInvalidationType invalidationType = "uri" + uriPrefixInvalidationType invalidationType = "uri-prefix" + originInvalidationType invalidationType = "origin" + groupInvalidationType invalidationType = "group" +) + +type invalidation struct { + Type invalidationType `json:"type"` + Selectors []string `json:"selectors"` + Groups []string `json:"groups"` + Purge bool `json:"purge"` +} + +func initializeSouin( + configuration configurationtypes.AbstractConfigurationInterface, + storers []types.Storer, + surrogateStorage providers.SurrogateInterface, +) *SouinAPI { + basePath := configuration.GetAPI().Souin.BasePath + if basePath == "" { + basePath = "/souin" + } + + allowedMethods := configuration.GetDefaultCache().GetAllowedHTTPVerbs() + if len(allowedMethods) == 0 { + allowedMethods = []string{http.MethodGet, http.MethodHead} + } + + return &SouinAPI{ + basePath, + configuration.GetAPI().Souin.Enable, + storers, + surrogateStorage, + allowedMethods, + } +} + +// BulkDelete allow user to delete multiple items with regexp +func (s *SouinAPI) BulkDelete(key string) { + for _, current := range s.storers { + current.DeleteMany(key) + } +} + +// Delete will delete a record into the provider cache system and will update the Souin API if enabled +func (s *SouinAPI) Delete(key string) { + for _, current := range s.storers { + current.Delete(key) + } +} + +// GetAll will retrieve all stored keys in the provider +func (s *SouinAPI) GetAll() []string { + keys := []string{} + for _, current := range s.storers { + keys = append(keys, current.ListKeys()...) + } + + return keys +} + +// GetBasePath will return the basepath for this resource +func (s *SouinAPI) GetBasePath() string { + return s.basePath +} + +// IsEnabled will return enabled status +func (s *SouinAPI) IsEnabled() bool { + return s.enabled +} + +func (s *SouinAPI) listKeys(search string) []string { + res := []string{} + re, err := regexp.Compile(search) + if err != nil { + return res + } + for _, key := range s.GetAll() { + if re.MatchString(key) { + res = append(res, key) + } + } + + return res +} + +// HandleRequest will handle the request +func (s *SouinAPI) HandleRequest(w http.ResponseWriter, r *http.Request) { + res := []byte{} + compile := regexp.MustCompile(s.GetBasePath()+"/.+").FindString(r.RequestURI) != "" + switch r.Method { + case http.MethodGet: + if regexp.MustCompile(s.GetBasePath()+"/surrogate_keys").FindString(r.RequestURI) != "" { + res, _ = json.Marshal(s.surrogateStorage.List()) + } else if compile { + search := regexp.MustCompile(s.GetBasePath()+"/(.+)").FindAllStringSubmatch(r.RequestURI, -1)[0][1] + res, _ = json.Marshal(s.listKeys(search)) + if len(res) == 2 { + w.WriteHeader(http.StatusNotFound) + } + } else { + res, _ = json.Marshal(s.GetAll()) + } + w.Header().Set("Content-Type", "application/json") + case http.MethodPost: + var invalidator invalidation + err := json.NewDecoder(r.Body).Decode(&invalidator) + if err != nil { + w.WriteHeader(http.StatusBadRequest) + return + } + + keysToInvalidate := []string{} + switch invalidator.Type { + case groupInvalidationType: + keysToInvalidate, _ = s.surrogateStorage.Purge(http.Header{"Surrogate-Key": invalidator.Groups}) + case uriPrefixInvalidationType, uriInvalidationType: + bodyKeys := []string{} + listedKeys := s.GetAll() + for _, k := range invalidator.Selectors { + if !strings.Contains(k, "//") { + rq, err := http.NewRequest(http.MethodGet, "//"+k, nil) + if err != nil { + continue + } + + bodyKeys = append(bodyKeys, rq.Host+"-"+rq.URL.Path) + } + } + + for _, allKey := range listedKeys { + for _, bk := range bodyKeys { + if invalidator.Type == uriInvalidationType { + if strings.Contains(allKey, bk) && strings.Contains(allKey, bk+"-") && strings.HasSuffix(allKey, bk) { + keysToInvalidate = append(keysToInvalidate, allKey) + break + } + } else { + if strings.Contains(allKey, bk) && + (strings.Contains(allKey, bk+"-") || strings.Contains(allKey, bk+"?") || strings.Contains(allKey, bk+"/") || strings.HasSuffix(allKey, bk)) { + keysToInvalidate = append(keysToInvalidate, allKey) + break + } + } + } + } + case originInvalidationType: + bodyKeys := []string{} + listedKeys := s.GetAll() + for _, k := range invalidator.Selectors { + if !strings.Contains(k, "//") { + rq, err := http.NewRequest(http.MethodGet, "//"+k, nil) + if err != nil { + continue + } + + bodyKeys = append(bodyKeys, rq.Host) + } + } + + for _, allKey := range listedKeys { + for _, bk := range bodyKeys { + if strings.Contains(allKey, bk) { + keysToInvalidate = append(keysToInvalidate, allKey) + break + } + } + } + } + + for _, k := range keysToInvalidate { + for _, current := range s.storers { + current.Delete(k) + } + } + w.WriteHeader(http.StatusOK) + case "PURGE": + if compile { + keysRg := regexp.MustCompile(s.GetBasePath() + "/(.+)") + flushRg := regexp.MustCompile(s.GetBasePath() + "/flush$") + + if flushRg.FindString(r.RequestURI) != "" { + for _, current := range s.storers { + current.DeleteMany(".+") + } + e := s.surrogateStorage.Destruct() + if e != nil { + fmt.Printf("Error while purging the surrogate keys: %+v.", e) + } + fmt.Println("Successfully clear the cache and the surrogate keys storage.") + } else { + submatch := keysRg.FindAllStringSubmatch(r.RequestURI, -1)[0][1] + s.BulkDelete(submatch) + } + } else { + ck, _ := s.surrogateStorage.Purge(r.Header) + for _, k := range ck { + for _, current := range s.storers { + current.Delete(k) + } + } + } + w.WriteHeader(http.StatusNoContent) + default: + } + _, _ = w.Write(res) +} diff --git a/plugins/traefik/vendor/github.com/darkweak/souin/api/types.go b/plugins/traefik/vendor/github.com/darkweak/souin/api/types.go new file mode 100644 index 000000000..a1c5c53e9 --- /dev/null +++ b/plugins/traefik/vendor/github.com/darkweak/souin/api/types.go @@ -0,0 +1,12 @@ +package api + +import ( + "net/http" +) + +// EndpointInterface is the contract to be able to enable your custom endpoints +type EndpointInterface interface { + GetBasePath() string + IsEnabled() bool + HandleRequest(http.ResponseWriter, *http.Request) +} diff --git a/plugins/traefik/vendor/github.com/darkweak/souin/cache/ykey.go b/plugins/traefik/vendor/github.com/darkweak/souin/cache/ykey.go new file mode 100644 index 000000000..b710369c0 --- /dev/null +++ b/plugins/traefik/vendor/github.com/darkweak/souin/cache/ykey.go @@ -0,0 +1,136 @@ +package ykeys + +import ( + "fmt" + "net/http" + "regexp" + "strings" + "time" + + "github.com/akyoto/cache" + + "github.com/darkweak/souin/configurationtypes" +) + +// The YKey system is like the Varnish one. You can invalidate cache from ykey based instead of the regexp or the plain +// URL to invalidate. It will target the referred URLs to this tag +// e.g. +// Given YKey data as +// |---------------|-----------------------------------------------------------------------------------| +// | YKey | URLs | +// |---------------|-----------------------------------------------------------------------------------| +// | GROUP_KEY_ONE | http://domain.com/,http://domain.com/1,http://domain.com/2,http://domain.com/4 | +// | GROUP_KEY_TWO | http://domain.com/1,http://domain.com/2,http://domain.com/3,http://domain.com/xyz | +// |---------------|-----------------------------------------------------------------------------------| +// When I send a purge request to /ykey/GROUP_KEY_ONE +// Then the cache will be purged for the list +// * http://domain.com/ +// * http://domain.com/1 +// * http://domain.com/2 +// * http://domain.com/4 +// And the data in the YKey table storage will contain +// |---------------|-------------------------------------------| +// | YKey | URLs | +// |---------------|-------------------------------------------| +// | GROUP_KEY_ONE | | +// | GROUP_KEY_TWO | http://domain.com/3,http://domain.com/xyz | +// |---------------|-------------------------------------------| + +// YKeyStorage is the layer for YKey support storage +type YKeyStorage struct { + *cache.Cache + Keys map[string]configurationtypes.SurrogateKeys +} + +// InitializeYKeys will initialize the ykey storage system +func InitializeYKeys(keys map[string]configurationtypes.SurrogateKeys) *YKeyStorage { + if len(keys) == 0 { + return nil + } + + c := cache.New(1 * time.Second) + + for key := range keys { + c.Set(key, "", 1) + } + + return &YKeyStorage{Cache: c, Keys: keys} +} + +// GetValidatedTags returns the validated tags based on the key x headers +func (y *YKeyStorage) GetValidatedTags(key string, headers http.Header) []string { + var tags []string + for k, v := range y.Keys { + valid := true + if v.URL != "" { + if r, e := regexp.MatchString(v.URL, key); !r || e != nil { + continue + } + } + if v.Headers != nil { + for h, hValue := range v.Headers { + if res, err := regexp.MatchString(hValue, headers.Get(h)); !res || err != nil { + valid = false + break + } + } + } + if valid { + tags = append(tags, k) + } + } + + return tags +} + +// InvalidateTags invalidate a tag list +func (y *YKeyStorage) InvalidateTags(tags []string) []string { + var u []string + for _, tag := range tags { + if v, e := y.Cache.Get(tag); e { + u = append(u, y.InvalidateTagURLs(v.(string))...) + } + } + + return u +} + +// InvalidateTagURLs invalidate URLs in the stored map +func (y *YKeyStorage) InvalidateTagURLs(urls string) []string { + u := strings.Split(urls, ",") + for _, url := range u { + y.invalidateURL(url) + } + + return u +} + +func (y *YKeyStorage) invalidateURL(url string) { + urlRegexp := regexp.MustCompile(fmt.Sprintf("(%s,)|(,%s$)|(^%s$)", url, url, url)) + for key := range y.Keys { + v, found := y.Cache.Get(key) + if found && urlRegexp.MatchString(v.(string)) { + y.Set(key, urlRegexp.ReplaceAllString(v.(string), ""), 1) + } + } +} + +// AddToTags add an URL to a tag list +func (y *YKeyStorage) AddToTags(url string, tags []string) { + for _, tag := range tags { + y.addToTag(url, tag) + } +} + +func (y *YKeyStorage) addToTag(url string, tag string) { + if v, e := y.Cache.Get(tag); e { + urlRegexp := regexp.MustCompile(url) + tmpStr := v.(string) + if !urlRegexp.MatchString(tmpStr) { + if tmpStr != "" { + tmpStr += "," + } + y.Cache.Set(tag, tmpStr+url, 1) + } + } +} diff --git a/plugins/traefik/vendor/github.com/darkweak/souin/configurationtypes/types.go b/plugins/traefik/vendor/github.com/darkweak/souin/configurationtypes/types.go index f0ffb9535..684b56241 100644 --- a/plugins/traefik/vendor/github.com/darkweak/souin/configurationtypes/types.go +++ b/plugins/traefik/vendor/github.com/darkweak/souin/configurationtypes/types.go @@ -9,7 +9,6 @@ import ( "strings" "time" - "go.uber.org/zap" yaml "gopkg.in/yaml.v3" ) @@ -184,10 +183,6 @@ type URL struct { // CacheProvider config type CacheProvider struct { - // Uuid to identify a unique instance. - Uuid string - // Found to determine if we can use that storage. - Found bool `json:"found" yaml:"found"` // URL to connect to the storage system. URL string `json:"url" yaml:"url"` // Path to the configuration file. @@ -251,7 +246,7 @@ type DefaultCache struct { Timeout Timeout `json:"timeout" yaml:"timeout"` TTL Duration `json:"ttl" yaml:"ttl"` DefaultCacheControl string `json:"default_cache_control" yaml:"default_cache_control"` - MaxBodyBytes uint64 `json:"max_cacheable_body_bytes" yaml:"max_cacheable_body_bytes"` + MaxBodyBytes uint64 `json:"max_cachable_body_bytes" yaml:"max_cachable_body_bytes"` DisableCoalescing bool `json:"disable_coalescing" yaml:"disable_coalescing"` } @@ -375,7 +370,6 @@ type DefaultCacheInterface interface { GetEtcd() CacheProvider GetMode() string GetOtter() CacheProvider - GetNats() CacheProvider GetNuts() CacheProvider GetOlric() CacheProvider GetRedis() CacheProvider @@ -388,7 +382,6 @@ type DefaultCacheInterface interface { GetTTL() time.Duration GetDefaultCacheControl() string GetMaxBodyBytes() uint64 - IsCoalescingDisable() bool } // APIEndpoint is the minimal structure to define an endpoint @@ -439,8 +432,6 @@ type AbstractConfigurationInterface interface { GetDefaultCache() DefaultCacheInterface GetAPI() API GetLogLevel() string - GetLogger() *zap.Logger - SetLogger(*zap.Logger) GetYkeys() map[string]SurrogateKeys GetSurrogateKeys() map[string]SurrogateKeys GetCacheKeys() CacheKeys diff --git a/plugins/traefik/vendor/github.com/darkweak/souin/context/cache.go b/plugins/traefik/vendor/github.com/darkweak/souin/context/cache.go index a2ff6ff33..fbdd89cdc 100644 --- a/plugins/traefik/vendor/github.com/darkweak/souin/context/cache.go +++ b/plugins/traefik/vendor/github.com/darkweak/souin/context/cache.go @@ -19,16 +19,11 @@ type cacheContext struct { cacheName string } -func (*cacheContext) SetContextWithBaseRequest(req *http.Request, _ *http.Request) *http.Request { - return req -} - func (cc *cacheContext) SetupContext(c configurationtypes.AbstractConfigurationInterface) { cc.cacheName = defaultCacheName if c.GetDefaultCache().GetCacheName() != "" { cc.cacheName = c.GetDefaultCache().GetCacheName() } - c.GetLogger().Sugar().Debugf("Set %s as Cache-Status name", cc.cacheName) } func (cc *cacheContext) SetContext(req *http.Request) *http.Request { diff --git a/plugins/traefik/vendor/github.com/darkweak/souin/context/graphql.go b/plugins/traefik/vendor/github.com/darkweak/souin/context/graphql.go index 625c6746d..8557de703 100644 --- a/plugins/traefik/vendor/github.com/darkweak/souin/context/graphql.go +++ b/plugins/traefik/vendor/github.com/darkweak/souin/context/graphql.go @@ -21,36 +21,9 @@ type graphQLContext struct { custom bool } -func (g *graphQLContext) SetContextWithBaseRequest(req *http.Request, baseRq *http.Request) *http.Request { - ctx := req.Context() - ctx = context.WithValue(ctx, GraphQL, g.custom) - ctx = context.WithValue(ctx, HashBody, "") - ctx = context.WithValue(ctx, IsMutationRequest, false) - - if g.custom && req.Body != nil { - b := bytes.NewBuffer([]byte{}) - _, _ = io.Copy(b, req.Body) - req.Body = io.NopCloser(b) - baseRq.Body = io.NopCloser(b) - - if b.Len() > 0 { - if isMutation(b.Bytes()) { - ctx = context.WithValue(ctx, IsMutationRequest, true) - } else { - h := sha256.New() - h.Write(b.Bytes()) - ctx = context.WithValue(ctx, HashBody, fmt.Sprintf("-%x", h.Sum(nil))) - } - } - } - - return req.WithContext(ctx) -} - func (g *graphQLContext) SetupContext(c configurationtypes.AbstractConfigurationInterface) { if len(c.GetDefaultCache().GetAllowedHTTPVerbs()) != 0 { g.custom = true - c.GetLogger().Debug("Enable GraphQL logic due to your custom HTTP verbs setup.") } } diff --git a/plugins/traefik/vendor/github.com/darkweak/souin/context/key.go b/plugins/traefik/vendor/github.com/darkweak/souin/context/key.go index a3e7a3884..352fd05b6 100644 --- a/plugins/traefik/vendor/github.com/darkweak/souin/context/key.go +++ b/plugins/traefik/vendor/github.com/darkweak/souin/context/key.go @@ -5,8 +5,6 @@ import ( "net/http" "regexp" - "github.com/caddyserver/caddy/v2" - "github.com/caddyserver/caddy/v2/modules/caddyhttp" "github.com/darkweak/souin/configurationtypes" ) @@ -14,7 +12,6 @@ const ( Key ctxKey = "souin_ctx.CACHE_KEY" DisplayableKey ctxKey = "souin_ctx.DISPLAYABLE_KEY" IgnoredHeaders ctxKey = "souin_ctx.IGNORE_HEADERS" - Hashed ctxKey = "souin_ctx.HASHED" ) type keyContext struct { @@ -23,17 +20,10 @@ type keyContext struct { disable_method bool disable_query bool disable_scheme bool - displayable bool hash bool + displayable bool headers []string - template string overrides []map[*regexp.Regexp]keyContext - - initializer func(r *http.Request) *http.Request -} - -func (*keyContext) SetContextWithBaseRequest(req *http.Request, _ *http.Request) *http.Request { - return req } func (g *keyContext) SetupContext(c configurationtypes.AbstractConfigurationInterface) { @@ -45,7 +35,6 @@ func (g *keyContext) SetupContext(c configurationtypes.AbstractConfigurationInte g.disable_scheme = k.DisableScheme g.hash = k.Hash g.displayable = !k.Hide - g.template = k.Template g.headers = k.Headers g.overrides = make([]map[*regexp.Regexp]keyContext, 0) @@ -60,76 +49,75 @@ func (g *keyContext) SetupContext(c configurationtypes.AbstractConfigurationInte disable_scheme: v.DisableScheme, hash: v.Hash, displayable: !v.Hide, - template: v.Template, headers: v.Headers, }}) } } - - switch c.GetPluginName() { - case "caddy": - g.initializer = func(r *http.Request) *http.Request { - return r - } - default: - g.initializer = func(r *http.Request) *http.Request { - repl := caddy.NewReplacer() - - return caddyhttp.PrepareRequest(r, repl, nil, nil) - } - } } -func parseKeyInformations(req *http.Request, kCtx keyContext) (query, body, host, scheme, method, headerValues string, headers []string, displayable, hash bool) { - displayable = kCtx.displayable - hash = kCtx.hash +func (g *keyContext) SetContext(req *http.Request) *http.Request { + key := req.URL.Path + var headers []string - if !kCtx.disable_query && len(req.URL.RawQuery) > 0 { + scheme := "http-" + if req.TLS != nil { + scheme = "https-" + } + query := "" + body := "" + host := "" + method := "" + headerValues := "" + displayable := g.displayable + + if !g.disable_query && len(req.URL.RawQuery) > 0 { query += "?" + req.URL.RawQuery } - if !kCtx.disable_body { + if !g.disable_body { body = req.Context().Value(HashBody).(string) } - if !kCtx.disable_host { + if !g.disable_host { host = req.Host + "-" } - if !kCtx.disable_scheme { - scheme = "http-" - if req.TLS != nil { - scheme = "https-" - } - } - - if !kCtx.disable_method { + if !g.disable_method { method = req.Method + "-" } - headers = kCtx.headers - for _, hn := range kCtx.headers { + headers = g.headers + for _, hn := range g.headers { headerValues += "-" + req.Header.Get(hn) } - return -} - -func (g *keyContext) computeKey(req *http.Request) (key string, headers []string, hash, displayable bool) { - if g.template != "" { - return req.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer).ReplaceAll(g.template, ""), g.headers, g.hash, g.displayable - } - key = req.URL.Path - query, body, host, scheme, method, headerValues, headers, displayable, hash := parseKeyInformations(req, *g) - hasOverride := false for _, current := range g.overrides { for k, v := range current { if k.MatchString(req.RequestURI) { - if v.template != "" { - return req.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer).ReplaceAll(v.template, ""), v.headers, v.hash, v.displayable + displayable = v.displayable + host = "" + method = "" + query = "" + if !v.disable_query && len(req.URL.RawQuery) > 0 { + query = "?" + req.URL.RawQuery + } + if !v.disable_body { + body = req.Context().Value(HashBody).(string) + } + if !v.disable_method { + method = req.Method + "-" + } + if !v.disable_host { + host = req.Host + "-" + } + if len(v.headers) > 0 { + headerValues = "" + for _, hn := range v.headers { + headers = v.headers + headerValues += "-" + req.Header.Get(hn) + } } - query, body, host, scheme, method, headerValues, headers, displayable, hash = parseKeyInformations(req, v) hasOverride = true break } @@ -140,26 +128,13 @@ func (g *keyContext) computeKey(req *http.Request) (key string, headers []string } } - key = method + scheme + host + key + query + body + headerValues - - return -} - -func (g *keyContext) SetContext(req *http.Request) *http.Request { - rq := g.initializer(req) - key, headers, hash, displayable := g.computeKey(rq) - return req.WithContext( context.WithValue( context.WithValue( context.WithValue( - context.WithValue( - req.Context(), - Key, - key, - ), - Hashed, - hash, + req.Context(), + Key, + method+scheme+host+key+query+body+headerValues, ), IgnoredHeaders, headers, diff --git a/plugins/traefik/vendor/github.com/darkweak/souin/context/method.go b/plugins/traefik/vendor/github.com/darkweak/souin/context/method.go index 74becc87b..1e6417cbb 100644 --- a/plugins/traefik/vendor/github.com/darkweak/souin/context/method.go +++ b/plugins/traefik/vendor/github.com/darkweak/souin/context/method.go @@ -16,17 +16,12 @@ type methodContext struct { custom bool } -func (*methodContext) SetContextWithBaseRequest(req *http.Request, _ *http.Request) *http.Request { - return req -} - func (m *methodContext) SetupContext(c configurationtypes.AbstractConfigurationInterface) { m.allowedVerbs = defaultVerbs if len(c.GetDefaultCache().GetAllowedHTTPVerbs()) != 0 { m.allowedVerbs = c.GetDefaultCache().GetAllowedHTTPVerbs() m.custom = true } - c.GetLogger().Sugar().Debugf("Allow %d method(s). %v.", len(m.allowedVerbs), m.allowedVerbs) } func (m *methodContext) SetContext(req *http.Request) *http.Request { diff --git a/plugins/traefik/vendor/github.com/darkweak/souin/context/mode.go b/plugins/traefik/vendor/github.com/darkweak/souin/context/mode.go index dbe682d1c..b041abb15 100644 --- a/plugins/traefik/vendor/github.com/darkweak/souin/context/mode.go +++ b/plugins/traefik/vendor/github.com/darkweak/souin/context/mode.go @@ -13,20 +13,15 @@ type ModeContext struct { Strict, Bypass_request, Bypass_response bool } -func (*ModeContext) SetContextWithBaseRequest(req *http.Request, _ *http.Request) *http.Request { - return req -} - func (mc *ModeContext) SetupContext(c configurationtypes.AbstractConfigurationInterface) { mode := c.GetDefaultCache().GetMode() mc.Bypass_request = mode == "bypass" || mode == "bypass_request" mc.Bypass_response = mode == "bypass" || mode == "bypass_response" mc.Strict = !mc.Bypass_request && !mc.Bypass_response - c.GetLogger().Sugar().Debugf("The cache logic will run as %s: %+v", mode, mc) } func (mc *ModeContext) SetContext(req *http.Request) *http.Request { return req.WithContext(context.WithValue(req.Context(), Mode, mc)) } -var _ ctx = (*ModeContext)(nil) +var _ ctx = (*cacheContext)(nil) diff --git a/plugins/traefik/vendor/github.com/darkweak/souin/context/now.go b/plugins/traefik/vendor/github.com/darkweak/souin/context/now.go index d0d4e0f3b..898cc18fe 100644 --- a/plugins/traefik/vendor/github.com/darkweak/souin/context/now.go +++ b/plugins/traefik/vendor/github.com/darkweak/souin/context/now.go @@ -12,10 +12,6 @@ const Now ctxKey = "souin_ctx.NOW" type nowContext struct{} -func (*nowContext) SetContextWithBaseRequest(req *http.Request, _ *http.Request) *http.Request { - return req -} - func (cc *nowContext) SetupContext(_ configurationtypes.AbstractConfigurationInterface) {} func (cc *nowContext) SetContext(req *http.Request) *http.Request { diff --git a/plugins/traefik/vendor/github.com/darkweak/souin/context/timeout.go b/plugins/traefik/vendor/github.com/darkweak/souin/context/timeout.go index e2260bdf0..6c737d24c 100644 --- a/plugins/traefik/vendor/github.com/darkweak/souin/context/timeout.go +++ b/plugins/traefik/vendor/github.com/darkweak/souin/context/timeout.go @@ -22,10 +22,6 @@ type timeoutContext struct { timeoutCache, timeoutBackend time.Duration } -func (*timeoutContext) SetContextWithBaseRequest(req *http.Request, _ *http.Request) *http.Request { - return req -} - func (t *timeoutContext) SetupContext(c configurationtypes.AbstractConfigurationInterface) { t.timeoutBackend = defaultTimeoutBackend t.timeoutCache = defaultTimeoutCache @@ -35,8 +31,6 @@ func (t *timeoutContext) SetupContext(c configurationtypes.AbstractConfiguration if c.GetDefaultCache().GetTimeout().Backend.Duration != 0 { t.timeoutBackend = c.GetDefaultCache().GetTimeout().Backend.Duration } - c.GetLogger().Sugar().Infof("Set backend timeout to %v", t.timeoutBackend) - c.GetLogger().Sugar().Infof("Set cache timeout to %v", t.timeoutBackend) } func (t *timeoutContext) SetContext(req *http.Request) *http.Request { @@ -44,4 +38,4 @@ func (t *timeoutContext) SetContext(req *http.Request) *http.Request { return req.WithContext(context.WithValue(context.WithValue(ctx, TimeoutCancel, cancel), TimeoutCache, t.timeoutCache)) } -var _ ctx = (*timeoutContext)(nil) +var _ ctx = (*cacheContext)(nil) diff --git a/plugins/traefik/vendor/github.com/darkweak/souin/context/types.go b/plugins/traefik/vendor/github.com/darkweak/souin/context/types.go index 34e56363f..38bf5ed19 100644 --- a/plugins/traefik/vendor/github.com/darkweak/souin/context/types.go +++ b/plugins/traefik/vendor/github.com/darkweak/souin/context/types.go @@ -12,7 +12,6 @@ type ( ctx interface { SetupContext(c configurationtypes.AbstractConfigurationInterface) SetContext(req *http.Request) *http.Request - SetContextWithBaseRequest(req *http.Request, baseRq *http.Request) *http.Request } Context struct { @@ -54,6 +53,6 @@ func (c *Context) SetBaseContext(req *http.Request) *http.Request { return c.Mode.SetContext(c.Timeout.SetContext(c.Method.SetContext(c.CacheName.SetContext(c.Now.SetContext(req))))) } -func (c *Context) SetContext(req *http.Request, baseRq *http.Request) *http.Request { - return c.Key.SetContext(c.GraphQL.SetContextWithBaseRequest(req, baseRq)) +func (c *Context) SetContext(req *http.Request) *http.Request { + return c.Key.SetContext(c.GraphQL.SetContext(req)) } diff --git a/plugins/traefik/vendor/github.com/darkweak/souin/pkg/api/debug/debug.go b/plugins/traefik/vendor/github.com/darkweak/souin/pkg/api/debug/debug.go deleted file mode 100644 index 00856f5c5..000000000 --- a/plugins/traefik/vendor/github.com/darkweak/souin/pkg/api/debug/debug.go +++ /dev/null @@ -1,87 +0,0 @@ -package debug - -import ( - "net/http" - "net/http/pprof" - "strings" - - "github.com/darkweak/souin/configurationtypes" -) - -// DebugAPI object contains informations related to the endpoints -type DebugAPI struct { - basePath string - enabled bool -} - -type DefaultHandler struct{} - -func (d *DefaultHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - pprof.Index(w, r) -} - -// InitializeDebug initialize the debug endpoints -func InitializeDebug(configuration configurationtypes.AbstractConfigurationInterface) *DebugAPI { - basePath := configuration.GetAPI().Debug.BasePath - enabled := configuration.GetAPI().Debug.Enable - if basePath == "" { - basePath = "/debug/" - } - - return &DebugAPI{ - basePath, - enabled, - } -} - -// GetBasePath will return the basepath for this resource -func (p *DebugAPI) GetBasePath() string { - return p.basePath -} - -// IsEnabled will return enabled status -func (p *DebugAPI) IsEnabled() bool { - return p.enabled -} - -// HandleRequest will handle the request -func (p *DebugAPI) HandleRequest(w http.ResponseWriter, r *http.Request) { - var executor http.Handler - executor = &DefaultHandler{} - - if strings.Contains(r.RequestURI, "allocs") { - executor = pprof.Handler("allocs") - } - if strings.Contains(r.RequestURI, "cmdline") { - executor = pprof.Handler("cmdline") - } - if strings.Contains(r.RequestURI, "profile") { - executor = pprof.Handler("profile") - } - if strings.Contains(r.RequestURI, "symbol") { - executor = pprof.Handler("symbol") - } - if strings.Contains(r.RequestURI, "trace") { - executor = pprof.Handler("trace") - } - if strings.Contains(r.RequestURI, "goroutine") { - executor = pprof.Handler("goroutine") - } - if strings.Contains(r.RequestURI, "heap") { - executor = pprof.Handler("heap") - } - if strings.Contains(r.RequestURI, "block") { - executor = pprof.Handler("block") - } - if strings.Contains(r.RequestURI, "heap") { - executor = pprof.Handler("heap") - } - if strings.Contains(r.RequestURI, "mutex") { - executor = pprof.Handler("mutex") - } - if strings.Contains(r.RequestURI, "threadcreate") { - executor = pprof.Handler("threadcreate") - } - - executor.ServeHTTP(w, r) -} diff --git a/plugins/traefik/vendor/github.com/darkweak/souin/pkg/api/main.go b/plugins/traefik/vendor/github.com/darkweak/souin/pkg/api/main.go index 810f447ff..03230b136 100644 --- a/plugins/traefik/vendor/github.com/darkweak/souin/pkg/api/main.go +++ b/plugins/traefik/vendor/github.com/darkweak/souin/pkg/api/main.go @@ -4,8 +4,6 @@ import ( "net/http" "github.com/darkweak/souin/configurationtypes" - "github.com/darkweak/souin/pkg/api/debug" - "github.com/darkweak/souin/pkg/api/prometheus" "github.com/darkweak/souin/pkg/storage/types" "github.com/darkweak/souin/pkg/surrogate/providers" ) @@ -33,7 +31,9 @@ func GenerateHandlerMap( for _, endpoint := range Initialize(configuration, storers, surrogateStorage) { if endpoint.IsEnabled() { shouldEnable = true - hm[basePathAPIS+endpoint.GetBasePath()] = endpoint.HandleRequest + if e, ok := endpoint.(*SouinAPI); ok { + hm[basePathAPIS+endpoint.GetBasePath()] = e.HandleRequest + } } } @@ -46,6 +46,5 @@ func GenerateHandlerMap( // Initialize contains all apis that should be enabled func Initialize(c configurationtypes.AbstractConfigurationInterface, storers []types.Storer, surrogateStorage providers.SurrogateInterface) []EndpointInterface { - return []EndpointInterface{initializeSouin(c, storers, - surrogateStorage), debug.InitializeDebug(c), prometheus.InitializePrometheus(c)} + return []EndpointInterface{initializeSouin(c, storers, surrogateStorage)} } diff --git a/plugins/traefik/vendor/github.com/darkweak/souin/pkg/api/prometheus/prometheus.go b/plugins/traefik/vendor/github.com/darkweak/souin/pkg/api/prometheus/prometheus.go index 051d122f6..2712571d6 100644 --- a/plugins/traefik/vendor/github.com/darkweak/souin/pkg/api/prometheus/prometheus.go +++ b/plugins/traefik/vendor/github.com/darkweak/souin/pkg/api/prometheus/prometheus.go @@ -1,107 +1,5 @@ package prometheus -import ( - "net/http" +const RequestCounter = "" - "github.com/darkweak/souin/configurationtypes" - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/promauto" - "github.com/prometheus/client_golang/prometheus/promhttp" -) - -const ( - counter = "counter" - average = "average" - - RequestCounter = "souin_request_upstream_counter" - RequestRevalidationCounter = "souin_request_revalidation_counter" - NoCachedResponseCounter = "souin_no_cached_response_counter" - CachedResponseCounter = "souin_cached_response_counter" - AvgResponseTime = "souin_avg_response_time" -) - -// PrometheusAPI object contains informations related to the endpoints -type PrometheusAPI struct { - basePath string - enabled bool -} - -// InitializePrometheus initialize the prometheus endpoints -func InitializePrometheus(configuration configurationtypes.AbstractConfigurationInterface) *PrometheusAPI { - basePath := configuration.GetAPI().Prometheus.BasePath - enabled := configuration.GetAPI().Prometheus.Enable - if basePath == "" { - basePath = "/metrics" - } - - if registered == nil { - run() - } - return &PrometheusAPI{ - basePath, - enabled, - } -} - -// GetBasePath will return the basepath for this resource -func (p *PrometheusAPI) GetBasePath() string { - return p.basePath -} - -// IsEnabled will return enabled status -func (p *PrometheusAPI) IsEnabled() bool { - return p.enabled -} - -// HandleRequest will handle the request -func (p *PrometheusAPI) HandleRequest(w http.ResponseWriter, r *http.Request) { - promhttp.Handler().ServeHTTP(w, r) -} - -var registered map[string]interface{} - -// Increment will increment the counter. -func Increment(name string) { - if _, ok := registered[name]; ok { - registered[name].(prometheus.Counter).Inc() - } -} - -// Increment will add the referred value the counter. -func Add(name string, value float64) { - if c, ok := registered[name].(prometheus.Counter); ok { - c.Add(value) - } - if g, ok := registered[name].(prometheus.Histogram); ok { - g.Observe(value) - } -} - -func push(promType, name, help string) { - switch promType { - case counter: - registered[name] = promauto.NewCounter(prometheus.CounterOpts{ - Name: name, - Help: help, - }) - - return - case average: - avg := prometheus.NewHistogram(prometheus.HistogramOpts{ - Name: name, - Help: help, - }) - prometheus.MustRegister(avg) - registered[name] = avg - } -} - -// Run populate and prepare the map with the default values. -func run() { - registered = make(map[string]interface{}) - push(counter, RequestCounter, "Total upstream request counter") - push(counter, RequestRevalidationCounter, "Total revalidation request revalidation counter") - push(counter, NoCachedResponseCounter, "No cached response counter") - push(counter, CachedResponseCounter, "Cached response counter") - push(average, AvgResponseTime, "Average response time") -} +func Increment(string) {} diff --git a/plugins/traefik/vendor/github.com/darkweak/souin/pkg/api/souin.go b/plugins/traefik/vendor/github.com/darkweak/souin/pkg/api/souin.go index 0f11e2e09..8f5305cb9 100644 --- a/plugins/traefik/vendor/github.com/darkweak/souin/pkg/api/souin.go +++ b/plugins/traefik/vendor/github.com/darkweak/souin/pkg/api/souin.go @@ -6,14 +6,10 @@ import ( "net/http" "regexp" "strings" - "time" "github.com/darkweak/souin/configurationtypes" "github.com/darkweak/souin/pkg/storage/types" "github.com/darkweak/souin/pkg/surrogate/providers" - "github.com/darkweak/storages/core" - "google.golang.org/protobuf/proto" - "google.golang.org/protobuf/types/known/timestamppb" ) // SouinAPI object contains informations related to the endpoints @@ -66,43 +62,16 @@ func initializeSouin( } // BulkDelete allow user to delete multiple items with regexp -func (s *SouinAPI) BulkDelete(key string, purge bool) { - key, _ = strings.CutPrefix(key, core.MappingKeyPrefix) +func (s *SouinAPI) BulkDelete(key string) { for _, current := range s.storers { - if b := current.Get(core.MappingKeyPrefix + key); len(b) > 0 { - var mapping core.StorageMapper - - if e := proto.Unmarshal(b, &mapping); e == nil { - for k := range mapping.GetMapping() { - current.Delete(k) - } - } - - if purge { - current.Delete(core.MappingKeyPrefix + key) - } else { - newFreshTime := time.Now() - for k, v := range mapping.Mapping { - v.FreshTime = timestamppb.New(newFreshTime) - mapping.Mapping[k] = v - } - } - } + current.DeleteMany(key) } - - s.Delete(key) } // Delete will delete a record into the provider cache system and will update the Souin API if enabled -// The key can be a regexp to delete multiple items func (s *SouinAPI) Delete(key string) { - _, err := regexp.Compile(key) for _, current := range s.storers { - if err != nil { - current.DeleteMany(key) - } else { - current.Delete(key) - } + current.Delete(key) } } @@ -141,52 +110,6 @@ func (s *SouinAPI) listKeys(search string) []string { return res } -var storageToInfiniteTTLMap = map[string]time.Duration{ - "BADGER": types.OneYearDuration, - "ETCD": types.OneYearDuration, - "NUTS": 0, - "OLRIC": types.OneYearDuration, - "OTTER": types.OneYearDuration, - "REDIS": -1, - types.DefaultStorageName: types.OneYearDuration, -} - -func (s *SouinAPI) purgeMapping() { - now := time.Now() - for _, current := range s.storers { - infiniteStoreDuration := storageToInfiniteTTLMap[current.Name()] - values := current.MapKeys(core.MappingKeyPrefix) - for k, v := range values { - mapping := &core.StorageMapper{} - - e := proto.Unmarshal([]byte(v), mapping) - if e != nil { - current.Delete(core.MappingKeyPrefix + k) - continue - } - - updated := false - for key, val := range mapping.GetMapping() { - if now.Sub(val.FreshTime.AsTime()) > 0 && now.Sub(val.StaleTime.AsTime()) > 0 { - delete(mapping.GetMapping(), key) - updated = true - } - } - - if updated { - v, e := proto.Marshal(mapping) - if e != nil { - fmt.Println("Impossible to re-encode the mapping", core.MappingKeyPrefix+k) - current.Delete(core.MappingKeyPrefix + k) - } - _ = current.Set(core.MappingKeyPrefix+k, v, infiniteStoreDuration) - } - } - } - - fmt.Println("Successfully clear the mappings.") -} - // HandleRequest will handle the request func (s *SouinAPI) HandleRequest(w http.ResponseWriter, r *http.Request) { res := []byte{} @@ -207,7 +130,6 @@ func (s *SouinAPI) HandleRequest(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") case http.MethodPost: var invalidator invalidation - defer r.Body.Close() err := json.NewDecoder(r.Body).Decode(&invalidator) if err != nil { w.WriteHeader(http.StatusBadRequest) @@ -273,14 +195,15 @@ func (s *SouinAPI) HandleRequest(w http.ResponseWriter, r *http.Request) { } for _, k := range keysToInvalidate { - s.BulkDelete(k, invalidator.Purge) + for _, current := range s.storers { + current.Delete(k) + } } w.WriteHeader(http.StatusOK) case "PURGE": if compile { keysRg := regexp.MustCompile(s.GetBasePath() + "/(.+)") flushRg := regexp.MustCompile(s.GetBasePath() + "/flush$") - mappingRg := regexp.MustCompile(s.GetBasePath() + "/mapping$") if flushRg.FindString(r.RequestURI) != "" { for _, current := range s.storers { @@ -291,16 +214,16 @@ func (s *SouinAPI) HandleRequest(w http.ResponseWriter, r *http.Request) { fmt.Printf("Error while purging the surrogate keys: %+v.", e) } fmt.Println("Successfully clear the cache and the surrogate keys storage.") - } else if mappingRg.FindString(r.RequestURI) != "" { - s.purgeMapping() } else { submatch := keysRg.FindAllStringSubmatch(r.RequestURI, -1)[0][1] - s.BulkDelete(submatch, true) + s.BulkDelete(submatch) } } else { ck, _ := s.surrogateStorage.Purge(r.Header) for _, k := range ck { - s.BulkDelete(k, true) + for _, current := range s.storers { + current.Delete(k) + } } } w.WriteHeader(http.StatusNoContent) diff --git a/plugins/traefik/vendor/github.com/darkweak/souin/pkg/middleware/configuration.go b/plugins/traefik/vendor/github.com/darkweak/souin/pkg/middleware/configuration.go index ad137138c..3f31ad46b 100644 --- a/plugins/traefik/vendor/github.com/darkweak/souin/pkg/middleware/configuration.go +++ b/plugins/traefik/vendor/github.com/darkweak/souin/pkg/middleware/configuration.go @@ -2,7 +2,6 @@ package middleware import ( "github.com/darkweak/souin/configurationtypes" - "go.uber.org/zap" ) // BaseConfiguration holder @@ -12,7 +11,6 @@ type BaseConfiguration struct { CacheKeys configurationtypes.CacheKeys `json:"cache_keys" yaml:"cache_keys"` URLs map[string]configurationtypes.URL `json:"urls" yaml:"urls"` LogLevel string `json:"log_level" yaml:"log_level"` - Logger *zap.Logger PluginName string Ykeys map[string]configurationtypes.SurrogateKeys `json:"ykeys" yaml:"ykeys"` SurrogateKeys map[string]configurationtypes.SurrogateKeys `json:"surrogate_keys" yaml:"surrogate_keys"` @@ -43,16 +41,6 @@ func (c *BaseConfiguration) GetLogLevel() string { return c.LogLevel } -// GetLogger get the logger -func (c *BaseConfiguration) GetLogger() *zap.Logger { - return c.Logger -} - -// SetLogger set the logger -func (c *BaseConfiguration) SetLogger(l *zap.Logger) { - c.Logger = l -} - // GetYkeys get the ykeys list func (c *BaseConfiguration) GetYkeys() map[string]configurationtypes.SurrogateKeys { return c.SurrogateKeys diff --git a/plugins/traefik/vendor/github.com/darkweak/souin/pkg/middleware/middleware.go b/plugins/traefik/vendor/github.com/darkweak/souin/pkg/middleware/middleware.go index 2aa0b0b98..2fe9f0551 100644 --- a/plugins/traefik/vendor/github.com/darkweak/souin/pkg/middleware/middleware.go +++ b/plugins/traefik/vendor/github.com/darkweak/souin/pkg/middleware/middleware.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "io" - "maps" "net/http" "net/http/httputil" "regexp" @@ -14,91 +13,27 @@ import ( "sync" "time" - "github.com/cespare/xxhash/v2" "github.com/darkweak/souin/configurationtypes" "github.com/darkweak/souin/context" "github.com/darkweak/souin/helpers" "github.com/darkweak/souin/pkg/api" - "github.com/darkweak/souin/pkg/api/prometheus" "github.com/darkweak/souin/pkg/rfc" "github.com/darkweak/souin/pkg/storage" "github.com/darkweak/souin/pkg/storage/types" "github.com/darkweak/souin/pkg/surrogate" "github.com/darkweak/souin/pkg/surrogate/providers" - "github.com/darkweak/storages/core" - "github.com/google/uuid" "github.com/pquerna/cachecontrol/cacheobject" - "go.uber.org/zap" - "go.uber.org/zap/zapcore" - "golang.org/x/sync/singleflight" ) func NewHTTPCacheHandler(c configurationtypes.AbstractConfigurationInterface) *SouinBaseHandler { - if c.GetLogger() == nil { - var logLevel zapcore.Level - if c.GetLogLevel() == "" { - logLevel = zapcore.FatalLevel - } else if err := logLevel.UnmarshalText([]byte(c.GetLogLevel())); err != nil { - logLevel = zapcore.FatalLevel - } - cfg := zap.Config{ - Encoding: "json", - Level: zap.NewAtomicLevelAt(logLevel), - OutputPaths: []string{"stderr"}, - ErrorOutputPaths: []string{"stderr"}, - EncoderConfig: zapcore.EncoderConfig{ - MessageKey: "message", - - LevelKey: "level", - EncodeLevel: zapcore.CapitalLevelEncoder, - - TimeKey: "time", - EncodeTime: zapcore.ISO8601TimeEncoder, - - CallerKey: "caller", - EncodeCaller: zapcore.ShortCallerEncoder, - }, - } - logger, _ := cfg.Build() - c.SetLogger(logger) - } - - storedStorers := core.GetRegisteredStorers() - storers := []types.Storer{} - if len(storedStorers) != 0 { - dc := c.GetDefaultCache() - for _, s := range []string{dc.GetBadger().Uuid, dc.GetEtcd().Uuid, dc.GetNuts().Uuid, dc.GetOlric().Uuid, dc.GetOtter().Uuid, dc.GetRedis().Uuid} { - if s != "" { - if st := core.GetRegisteredStorer(s); st != nil { - storers = append(storers, st.(types.Storer)) - } - } - } - - if len(storers) > 0 { - names := []string{} - for _, storer := range storers { - names = append(names, storer.Name()) - } - c.GetLogger().Sugar().Debugf("You're running Souin with the following storages %s", strings.Join(names, ", ")) - } - } - if len(storers) == 0 { - c.GetLogger().Warn("You're running Souin with the default storage that is not optimized and for development purpose. We recommend to use at least one of the storages from https://github.com/darkweak/storages") - - memoryStorer, _ := storage.Factory(c) - if st := core.GetRegisteredStorer(types.DefaultStorageName + "-"); st != nil { - memoryStorer = st.(types.Storer) - } else { - core.RegisterStorage(memoryStorer) - } - storers = append(storers, memoryStorer) + storers, err := storage.NewStorages(c) + if err != nil { + panic(err) } - - c.GetLogger().Sugar().Debugf("Storer initialized: %#v.", storers) + fmt.Println("Storers initialized.") regexpUrls := helpers.InitializeRegexp(c) - surrogateStorage := surrogate.InitializeSurrogate(c, fmt.Sprintf("%s-%s", storers[0].Name(), storers[0].Uuid())) - c.GetLogger().Debug("Surrogate storage initialized.") + surrogateStorage := surrogate.InitializeSurrogate(c, storers[0].Name()) + fmt.Println("Surrogate storage initialized.") var excludedRegexp *regexp.Regexp = nil if c.GetDefaultCache().GetRegex().Exclude != "" { excludedRegexp = regexp.MustCompile(c.GetDefaultCache().GetRegex().Exclude) @@ -117,7 +52,7 @@ func NewHTTPCacheHandler(c configurationtypes.AbstractConfigurationInterface) *S Headers: c.GetDefaultCache().GetHeaders(), DefaultCacheControl: c.GetDefaultCache().GetDefaultCacheControl(), } - c.GetLogger().Info("Souin configuration is now loaded.") + fmt.Println("Souin configuration is now loaded.") return &SouinBaseHandler{ Configuration: c, @@ -130,7 +65,6 @@ func NewHTTPCacheHandler(c configurationtypes.AbstractConfigurationInterface) *S context: ctx, bufPool: bufPool, storersLen: len(storers), - singleflightPool: singleflight.Group{}, } } @@ -144,17 +78,14 @@ type SouinBaseHandler struct { SurrogateKeyStorer providers.SurrogateInterface DefaultMatchedUrl configurationtypes.URL context *context.Context - singleflightPool singleflight.Group bufPool *sync.Pool storersLen int } -var Upstream50xError = upstream50xError{} +type upsreamError struct{} -type upstream50xError struct{} - -func (upstream50xError) Error() string { - return "Upstream 50x error" +func (upsreamError) Error() string { + return "Upstream error" } func isCacheableCode(code int) bool { @@ -166,15 +97,6 @@ func isCacheableCode(code int) bool { return false } -func canStatusCodeEmptyContent(code int) bool { - switch code { - case 204, 301, 405: - return true - } - - return false -} - func canBypassAuthorizationRestriction(headers http.Header, bypassed []string) bool { for _, header := range bypassed { if strings.ToLower(header) == "authorization" { @@ -191,38 +113,35 @@ func (s *SouinBaseHandler) Store( requestCc *cacheobject.RequestCacheDirectives, cachedKey string, ) error { - statusCode := customWriter.GetStatusCode() - if !isCacheableCode(statusCode) { - customWriter.Header().Set("Cache-Status", fmt.Sprintf("%s; fwd=uri-miss; key=%s; detail=UNCACHEABLE-STATUS-CODE", rq.Context().Value(context.CacheName), rfc.GetCacheKeyFromCtx(rq.Context()))) + if !isCacheableCode(customWriter.statusCode) { + customWriter.Headers.Set("Cache-Status", fmt.Sprintf("%s; fwd=uri-miss; key=%s; detail=UNCACHEABLE-STATUS-CODE", rq.Context().Value(context.CacheName), rfc.GetCacheKeyFromCtx(rq.Context()))) - switch statusCode { + switch customWriter.statusCode { case 500, 502, 503, 504: - return Upstream50xError + return new(upsreamError) } return nil } - headerName, cacheControl := s.SurrogateKeyStorer.GetSurrogateControl(customWriter.Header()) - if cacheControl == "" { + if customWriter.Header().Get("Cache-Control") == "" { // TODO see with @mnot if mandatory to not store the response when no Cache-Control given. // if s.DefaultMatchedUrl.DefaultCacheControl == "" { - // customWriter.Header().Set("Cache-Status", fmt.Sprintf("%s; fwd=uri-miss; key=%s; detail=EMPTY-RESPONSE-CACHE-CONTROL", rq.Context().Value(context.CacheName), rfc.GetCacheKeyFromCtx(rq.Context()))) + // customWriter.Headers.Set("Cache-Status", fmt.Sprintf("%s; fwd=uri-miss; key=%s; detail=EMPTY-RESPONSE-CACHE-CONTROL", rq.Context().Value(context.CacheName), rfc.GetCacheKeyFromCtx(rq.Context()))) // return nil // } - customWriter.Header().Set(headerName, s.DefaultMatchedUrl.DefaultCacheControl) + customWriter.Header().Set("Cache-Control", s.DefaultMatchedUrl.DefaultCacheControl) } - responseCc, _ := cacheobject.ParseResponseCacheControl(rfc.HeaderAllCommaSepValuesString(customWriter.Header(), headerName)) - s.Configuration.GetLogger().Sugar().Debugf("Response cache-control %+v", responseCc) + responseCc, _ := cacheobject.ParseResponseCacheControl(customWriter.Header().Get("Cache-Control")) if responseCc == nil { - customWriter.Header().Set("Cache-Status", fmt.Sprintf("%s; fwd=uri-miss; key=%s; detail=INVALID-RESPONSE-CACHE-CONTROL", rq.Context().Value(context.CacheName), rfc.GetCacheKeyFromCtx(rq.Context()))) + customWriter.Headers.Set("Cache-Status", fmt.Sprintf("%s; fwd=uri-miss; key=%s; detail=INVALID-RESPONSE-CACHE-CONTROL", rq.Context().Value(context.CacheName), rfc.GetCacheKeyFromCtx(rq.Context()))) return nil } modeContext := rq.Context().Value(context.Mode).(*context.ModeContext) if !modeContext.Bypass_request && (responseCc.PrivatePresent || rq.Header.Get("Authorization") != "") && !canBypassAuthorizationRestriction(customWriter.Header(), rq.Context().Value(context.IgnoredHeaders).([]string)) { - customWriter.Header().Set("Cache-Status", fmt.Sprintf("%s; fwd=uri-miss; key=%s; detail=PRIVATE-OR-AUTHENTICATED-RESPONSE", rq.Context().Value(context.CacheName), rfc.GetCacheKeyFromCtx(rq.Context()))) + customWriter.Headers.Set("Cache-Status", fmt.Sprintf("%s; fwd=uri-miss; key=%s; detail=PRIVATE-OR-AUTHENTICATED-RESPONSE", rq.Context().Value(context.CacheName), rfc.GetCacheKeyFromCtx(rq.Context()))) return nil } @@ -243,10 +162,13 @@ func (s *SouinBaseHandler) Store( } else if responseCc.MaxAge >= 0 { ma = time.Duration(responseCc.MaxAge) * time.Second } + if ma > currentMatchedURL.TTL.Duration { + ma = currentMatchedURL.TTL.Duration + } now := rq.Context().Value(context.Now).(time.Time) date, _ := http.ParseTime(now.Format(http.TimeFormat)) - customWriter.Header().Set(rfc.StoredTTLHeader, ma.String()) + customWriter.Headers.Set(rfc.StoredTTLHeader, ma.String()) ma = ma - time.Since(date) if exp := customWriter.Header().Get("Expires"); exp != "" { @@ -259,21 +181,15 @@ func (s *SouinBaseHandler) Store( status := fmt.Sprintf("%s; fwd=uri-miss", rq.Context().Value(context.CacheName)) if (modeContext.Bypass_request || !requestCc.NoStore) && (modeContext.Bypass_response || !responseCc.NoStore) { - headers := customWriter.Header().Clone() + headers := customWriter.Headers.Clone() for hname, shouldDelete := range responseCc.NoCache { if shouldDelete { headers.Del(hname) } } - - customWriter.mutex.Lock() - b := customWriter.Buf.Bytes() - bLen := customWriter.Buf.Len() - customWriter.mutex.Unlock() - res := http.Response{ - StatusCode: statusCode, - Body: io.NopCloser(bytes.NewBuffer(b)), + StatusCode: customWriter.statusCode, + Body: io.NopCloser(bytes.NewBuffer(customWriter.Buf.Bytes())), Header: headers, } @@ -281,28 +197,17 @@ func (s *SouinBaseHandler) Store( res.Header.Set("Date", now.Format(http.TimeFormat)) } if res.Header.Get("Content-Length") == "" { - res.Header.Set("Content-Length", fmt.Sprint(bLen)) - } - respBodyMaxSize := int(s.Configuration.GetDefaultCache().GetMaxBodyBytes()) - if respBodyMaxSize > 0 && bLen > respBodyMaxSize { - customWriter.Header().Set("Cache-Status", status+"; detail=UPSTREAM-RESPONSE-TOO-LARGE; key="+rfc.GetCacheKeyFromCtx(rq.Context())) - - return nil + res.Header.Set("Content-Length", fmt.Sprint(customWriter.Buf.Len())) } res.Header.Set(rfc.StoredLengthHeader, res.Header.Get("Content-Length")) response, err := httputil.DumpResponse(&res, true) - if err == nil && (bLen > 0 || canStatusCodeEmptyContent(statusCode)) { + if err == nil { variedHeaders, isVaryStar := rfc.VariedHeaderAllCommaSepValues(res.Header) if isVaryStar { // "Implies that the response is uncacheable" status += "; detail=UPSTREAM-VARY-STAR" } else { - variedKey := cachedKey + rfc.GetVariedCacheKey(rq, variedHeaders) - if rq.Context().Value(context.Hashed).(bool) { - cachedKey = fmt.Sprint(xxhash.Sum64String(cachedKey)) - variedKey = fmt.Sprint(xxhash.Sum64String(variedKey)) - } - s.Configuration.GetLogger().Sugar().Debugf("Store the response for %s with duration %v", variedKey, ma) + cachedKey += rfc.GetVariedCacheKey(rq, variedHeaders) var wg sync.WaitGroup mu := sync.Mutex{} @@ -311,25 +216,11 @@ func (s *SouinBaseHandler) Store( case <-rq.Context().Done(): status += "; detail=REQUEST-CANCELED-OR-UPSTREAM-BROKEN-PIPE" default: - vhs := http.Header{} - for _, hname := range variedHeaders { - hn := strings.Split(hname, ":") - vhs.Set(hn[0], rq.Header.Get(hn[0])) - } for _, storer := range s.Storers { wg.Add(1) go func(currentStorer types.Storer) { defer wg.Done() - if currentStorer.SetMultiLevel( - cachedKey, - variedKey, - response, - vhs, - res.Header.Get("Etag"), ma, - variedKey, - ) == nil { - s.Configuration.GetLogger().Sugar().Debugf("Stored the key %s in the %s provider", variedKey, currentStorer.Name()) - } else { + if currentStorer.Set(cachedKey, response, ma) != nil { mu.Lock() fails = append(fails, fmt.Sprintf("; detail=%s-INSERTION-ERROR", currentStorer.Name())) mu.Unlock() @@ -341,7 +232,7 @@ func (s *SouinBaseHandler) Store( if len(fails) < s.storersLen { go func(rs http.Response, key string) { _ = s.SurrogateKeyStorer.Store(&rs, key) - }(res, variedKey) + }(res, cachedKey) status += "; stored" } @@ -350,25 +241,15 @@ func (s *SouinBaseHandler) Store( } } } - - } else { - status += "; detail=UPSTREAM-ERROR-OR-EMPTY-RESPONSE" } } else { status += "; detail=NO-STORE-DIRECTIVE" } - customWriter.Header().Set("Cache-Status", status+"; key="+rfc.GetCacheKeyFromCtx(rq.Context())) + customWriter.Headers.Set("Cache-Status", status+"; key="+rfc.GetCacheKeyFromCtx(rq.Context())) return nil } -type singleflightValue struct { - body []byte - headers http.Header - requestHeaders http.Header - code int -} - func (s *SouinBaseHandler) Upstream( customWriter *CustomWriter, rq *http.Request, @@ -376,145 +257,71 @@ func (s *SouinBaseHandler) Upstream( requestCc *cacheobject.RequestCacheDirectives, cachedKey string, ) error { - s.Configuration.GetLogger().Sugar().Debug("Request the upstream server") - prometheus.Increment(prometheus.RequestCounter) - - var recoveredFromErr error = nil - defer func() { - // In case of "http.ErrAbortHandler" panic, - // prevent singleflight from wrapping it into "singleflight.panicError". - if r := recover(); r != nil { - err, ok := r.(error) - // Sometimes, the error is a string. - if !ok || errors.Is(err, http.ErrAbortHandler) { - recoveredFromErr = http.ErrAbortHandler - } else { - panic(err) - } - } - }() - - singleflightCacheKey := cachedKey - if s.Configuration.GetDefaultCache().IsCoalescingDisable() { - singleflightCacheKey += uuid.NewString() + if err := next(customWriter, rq); err != nil { + customWriter.Header().Set("Cache-Status", fmt.Sprintf("%s; fwd=uri-miss; key=%s; detail=SERVE-HTTP-ERROR", rq.Context().Value(context.CacheName), rfc.GetCacheKeyFromCtx(rq.Context()))) + return err } - sfValue, err, shared := s.singleflightPool.Do(singleflightCacheKey, func() (interface{}, error) { - if e := next(customWriter, rq); e != nil { - s.Configuration.GetLogger().Sugar().Warnf("%#v", e) - customWriter.Header().Set("Cache-Status", fmt.Sprintf("%s; fwd=uri-miss; key=%s; detail=SERVE-HTTP-ERROR", rq.Context().Value(context.CacheName), rfc.GetCacheKeyFromCtx(rq.Context()))) - return nil, e - } - - s.SurrogateKeyStorer.Invalidate(rq.Method, customWriter.Header()) - statusCode := customWriter.GetStatusCode() - if !isCacheableCode(statusCode) { - customWriter.Header().Set("Cache-Status", fmt.Sprintf("%s; fwd=uri-miss; key=%s; detail=UNCACHEABLE-STATUS-CODE", rq.Context().Value(context.CacheName), rfc.GetCacheKeyFromCtx(rq.Context()))) - - switch statusCode { - case 500, 502, 503, 504: - return nil, Upstream50xError - } - } + s.SurrogateKeyStorer.Invalidate(rq.Method, customWriter.Header()) + if !isCacheableCode(customWriter.statusCode) { + customWriter.Headers.Set("Cache-Status", fmt.Sprintf("%s; fwd=uri-miss; key=%s; detail=UNCACHEABLE-STATUS-CODE", rq.Context().Value(context.CacheName), rfc.GetCacheKeyFromCtx(rq.Context()))) - headerName, cacheControl := s.SurrogateKeyStorer.GetSurrogateControl(customWriter.Header()) - if cacheControl == "" { - customWriter.Header().Set(headerName, s.DefaultMatchedUrl.DefaultCacheControl) + switch customWriter.statusCode { + case 500, 502, 503, 504: + return new(upsreamError) } - err := s.Store(customWriter, rq, requestCc, cachedKey) - defer customWriter.Buf.Reset() - - return singleflightValue{ - body: customWriter.Buf.Bytes(), - headers: customWriter.Header().Clone(), - requestHeaders: rq.Header, - code: statusCode, - }, err - }) - if recoveredFromErr != nil { - panic(recoveredFromErr) - } - if err != nil { - return err + return nil } - if sfWriter, ok := sfValue.(singleflightValue); ok { - if vary := sfWriter.headers.Get("Vary"); vary != "" { - variedHeaders, isVaryStar := rfc.VariedHeaderAllCommaSepValues(sfWriter.headers) - if !isVaryStar { - for _, vh := range variedHeaders { - if rq.Header.Get(vh) != sfWriter.requestHeaders.Get(vh) { - // cachedKey += rfc.GetVariedCacheKey(rq, variedHeaders) - return s.Upstream(customWriter, rq, next, requestCc, cachedKey) - } - } - } - } - if shared { - s.Configuration.GetLogger().Sugar().Infof("Reused response from concurrent request with the key %s", cachedKey) - } - _, _ = customWriter.Write(sfWriter.body) - maps.Copy(customWriter.Header(), sfWriter.headers) - customWriter.WriteHeader(sfWriter.code) + if customWriter.Header().Get("Cache-Control") == "" { + // TODO see with @mnot if mandatory to not store the response when no Cache-Control given. + // if s.DefaultMatchedUrl.DefaultCacheControl == "" { + // customWriter.Headers.Set("Cache-Status", fmt.Sprintf("%s; fwd=uri-miss; key=%s; detail=EMPTY-RESPONSE-CACHE-CONTROL", rq.Context().Value(context.CacheName), rfc.GetCacheKeyFromCtx(rq.Context()))) + // return nil + // } + customWriter.Header().Set("Cache-Control", s.DefaultMatchedUrl.DefaultCacheControl) } - return nil -} - -func (s *SouinBaseHandler) Revalidate(validator *core.Revalidator, next handlerFunc, customWriter *CustomWriter, rq *http.Request, requestCc *cacheobject.RequestCacheDirectives, cachedKey string) error { - s.Configuration.GetLogger().Sugar().Debug("Revalidate the request with the upstream server") - prometheus.Increment(prometheus.RequestRevalidationCounter) - - singleflightCacheKey := cachedKey - if s.Configuration.GetDefaultCache().IsCoalescingDisable() { - singleflightCacheKey += uuid.NewString() + select { + case <-rq.Context().Done(): + return baseCtx.Canceled + default: + return s.Store(customWriter, rq, requestCc, cachedKey) } - sfValue, err, shared := s.singleflightPool.Do(singleflightCacheKey, func() (interface{}, error) { - err := next(customWriter, rq) - s.SurrogateKeyStorer.Invalidate(rq.Method, customWriter.Header()) +} - statusCode := customWriter.GetStatusCode() - if err == nil { - if validator.IfUnmodifiedSincePresent && statusCode != http.StatusNotModified { - customWriter.Buf.Reset() - customWriter.Rw.WriteHeader(http.StatusPreconditionFailed) +func (s *SouinBaseHandler) Revalidate(validator *rfc.Revalidator, next handlerFunc, customWriter *CustomWriter, rq *http.Request, requestCc *cacheobject.RequestCacheDirectives, cachedKey string) error { + err := next(customWriter, rq) + s.SurrogateKeyStorer.Invalidate(rq.Method, customWriter.Header()) - return nil, errors.New("") + if err == nil { + if validator.IfUnmodifiedSincePresent && customWriter.statusCode != http.StatusNotModified { + customWriter.Buf.Reset() + for h, v := range customWriter.Headers { + if len(v) > 0 { + customWriter.Rw.Header().Set(h, strings.Join(v, ", ")) + } } + customWriter.Rw.WriteHeader(http.StatusPreconditionFailed) - if statusCode != http.StatusNotModified { - err = s.Store(customWriter, rq, requestCc, cachedKey) - } + return errors.New("") } - customWriter.Header().Set( - "Cache-Status", - fmt.Sprintf( - "%s; fwd=request; fwd-status=%d; key=%s; detail=REQUEST-REVALIDATION", - rq.Context().Value(context.CacheName), - statusCode, - rfc.GetCacheKeyFromCtx(rq.Context()), - ), - ) - - defer customWriter.Buf.Reset() - return singleflightValue{ - body: customWriter.Buf.Bytes(), - headers: customWriter.Header().Clone(), - code: statusCode, - }, err - }) - - if sfWriter, ok := sfValue.(singleflightValue); ok { - if shared { - s.Configuration.GetLogger().Sugar().Infof("Reused response from concurrent request with the key %s", cachedKey) + if customWriter.statusCode != http.StatusNotModified { + err = s.Store(customWriter, rq, requestCc, cachedKey) } - _, _ = customWriter.Write(sfWriter.body) - maps.Copy(customWriter.Header(), sfWriter.headers) - customWriter.WriteHeader(sfWriter.code) } + customWriter.Header().Set( + "Cache-Status", + fmt.Sprintf( + "%s; fwd=request; fwd-status=%d; key=%s; detail=REQUEST-REVALIDATION", + rq.Context().Value(context.CacheName), + customWriter.statusCode, + rfc.GetCacheKeyFromCtx(rq.Context()), + ), + ) return err } @@ -527,127 +334,94 @@ func (s *SouinBaseHandler) HandleInternally(r *http.Request) (bool, http.Handler } } - return false, nil + // Because YƦgi interpretation sucks, we have to return the empty function instead of nil. + return false, func(w http.ResponseWriter, r *http.Request) {} } type handlerFunc = func(http.ResponseWriter, *http.Request) error -type statusCodeLogger struct { - http.ResponseWriter - statusCode int -} - -func (s *statusCodeLogger) WriteHeader(code int) { - s.statusCode = code - s.ResponseWriter.WriteHeader(code) -} func (s *SouinBaseHandler) ServeHTTP(rw http.ResponseWriter, rq *http.Request, next handlerFunc) error { - start := time.Now() - defer func(s time.Time) { - prometheus.Add(prometheus.AvgResponseTime, float64(time.Since(s).Milliseconds())) - }(start) - s.Configuration.GetLogger().Sugar().Debugf("Incomming request %+v", rq) - if b, handler := s.HandleInternally(rq); b { + b, handler := s.HandleInternally(rq) + if b { handler(rw, rq) return nil } - req := s.context.SetBaseContext(rq) - cacheName := req.Context().Value(context.CacheName).(string) + rq = s.context.SetBaseContext(rq) + cacheName := rq.Context().Value(context.CacheName).(string) if rq.Header.Get("Upgrade") == "websocket" || (s.ExcludeRegex != nil && s.ExcludeRegex.MatchString(rq.RequestURI)) { rw.Header().Set("Cache-Status", cacheName+"; fwd=bypass; detail=EXCLUDED-REQUEST-URI") - return next(rw, req) + return next(rw, rq) } - if !req.Context().Value(context.SupportedMethod).(bool) { + if !rq.Context().Value(context.SupportedMethod).(bool) { rw.Header().Set("Cache-Status", cacheName+"; fwd=bypass; detail=UNSUPPORTED-METHOD") - nrw := &statusCodeLogger{ - ResponseWriter: rw, - statusCode: 0, - } - err := next(nrw, req) - s.SurrogateKeyStorer.Invalidate(req.Method, rw.Header()) - - if err == nil && req.Method != http.MethodGet && nrw.statusCode < http.StatusBadRequest { - // Invalidate related GET keys when the method is not allowed and the response is valid - req.Method = http.MethodGet - keyname := s.context.SetContext(req, rq).Context().Value(context.Key).(string) - for _, storer := range s.Storers { - storer.Delete(core.MappingKeyPrefix + keyname) - } - } + err := next(rw, rq) + s.SurrogateKeyStorer.Invalidate(rq.Method, rw.Header()) return err } - requestCc, coErr := cacheobject.ParseRequestCacheControl(rfc.HeaderAllCommaSepValuesString(req.Header, "Cache-Control")) + requestCc, coErr := cacheobject.ParseRequestCacheControl(rq.Header.Get("Cache-Control")) - modeContext := req.Context().Value(context.Mode).(*context.ModeContext) + modeContext := rq.Context().Value(context.Mode).(*context.ModeContext) if !modeContext.Bypass_request && (coErr != nil || requestCc == nil) { rw.Header().Set("Cache-Status", cacheName+"; fwd=bypass; detail=CACHE-CONTROL-EXTRACTION-ERROR") - err := next(rw, req) - s.SurrogateKeyStorer.Invalidate(req.Method, rw.Header()) + err := next(rw, rq) + s.SurrogateKeyStorer.Invalidate(rq.Method, rw.Header()) return err } - req = s.context.SetContext(req, rq) - if req.Context().Value(context.IsMutationRequest).(bool) { + rq = s.context.SetContext(rq) + + // Yaegi sucks again, it considers false as true + isMutationRequest := rq.Context().Value(context.IsMutationRequest).(bool) + if isMutationRequest { rw.Header().Set("Cache-Status", cacheName+"; fwd=bypass; detail=IS-MUTATION-REQUEST") - err := next(rw, req) - s.SurrogateKeyStorer.Invalidate(req.Method, rw.Header()) + err := next(rw, rq) + s.SurrogateKeyStorer.Invalidate(rq.Method, rw.Header()) return err } - cachedKey := req.Context().Value(context.Key).(string) + cachedKey := rq.Context().Value(context.Key).(string) bufPool := s.bufPool.Get().(*bytes.Buffer) bufPool.Reset() defer s.bufPool.Put(bufPool) - customWriter := NewCustomWriter(req, rw, bufPool) + customWriter := NewCustomWriter(rq, rw, bufPool) go func(req *http.Request, crw *CustomWriter) { <-req.Context().Done() crw.mutex.Lock() crw.headersSent = true crw.mutex.Unlock() - }(req, customWriter) - s.Configuration.GetLogger().Sugar().Debugf("Request cache-control %+v", requestCc) + }(rq, customWriter) if modeContext.Bypass_request || !requestCc.NoCache { - validator := rfc.ParseRequest(req) - var fresh, stale *http.Response - finalKey := cachedKey - if req.Context().Value(context.Hashed).(bool) { - finalKey = fmt.Sprint(xxhash.Sum64String(finalKey)) - } + validator := rfc.ParseRequest(rq) + var response *http.Response for _, currentStorer := range s.Storers { - fresh, stale = currentStorer.GetMultiLevel(finalKey, req, validator) - - if fresh != nil || stale != nil { - s.Configuration.GetLogger().Sugar().Debugf("Found at least one valid response in the %s storage", currentStorer.Name()) + response = currentStorer.Prefix(cachedKey, rq, validator) + if response != nil { break } } - headerName, _ := s.SurrogateKeyStorer.GetSurrogateControl(customWriter.Header()) - if fresh != nil && (!modeContext.Strict || rfc.ValidateCacheControl(fresh, requestCc)) { - response := fresh + if response != nil && (!modeContext.Strict || rfc.ValidateCacheControl(response, requestCc)) { if validator.ResponseETag != "" && validator.Matched { rfc.SetCacheStatusHeader(response) - for h, v := range response.Header { - customWriter.Header()[h] = v - } + customWriter.Headers = response.Header if validator.NotModified { - customWriter.WriteHeader(http.StatusNotModified) + customWriter.statusCode = http.StatusNotModified customWriter.Buf.Reset() _, _ = customWriter.Send() return nil } - customWriter.WriteHeader(response.StatusCode) + customWriter.statusCode = response.StatusCode _, _ = io.Copy(customWriter.Buf, response.Body) _, _ = customWriter.Send() @@ -655,51 +429,48 @@ func (s *SouinBaseHandler) ServeHTTP(rw http.ResponseWriter, rq *http.Request, n } if validator.NeedRevalidation { - err := s.Revalidate(validator, next, customWriter, req, requestCc, cachedKey) + err := s.Revalidate(validator, next, customWriter, rq, requestCc, cachedKey) _, _ = customWriter.Send() return err } - if resCc, _ := cacheobject.ParseResponseCacheControl(rfc.HeaderAllCommaSepValuesString(response.Header, headerName)); resCc.NoCachePresent { - prometheus.Increment(prometheus.NoCachedResponseCounter) - err := s.Revalidate(validator, next, customWriter, req, requestCc, cachedKey) + if resCc, _ := cacheobject.ParseResponseCacheControl(response.Header.Get("Cache-Control")); resCc.NoCachePresent { + err := s.Revalidate(validator, next, customWriter, rq, requestCc, cachedKey) _, _ = customWriter.Send() return err } rfc.SetCacheStatusHeader(response) if !modeContext.Strict || rfc.ValidateMaxAgeCachedResponse(requestCc, response) != nil { - for h, v := range response.Header { - customWriter.Header()[h] = v - } - customWriter.WriteHeader(response.StatusCode) - s.Configuration.GetLogger().Sugar().Debugf("Serve from cache %+v", req) + customWriter.Headers = response.Header + customWriter.statusCode = response.StatusCode _, _ = io.Copy(customWriter.Buf, response.Body) _, err := customWriter.Send() - prometheus.Increment(prometheus.CachedResponseCounter) return err } - } else if !requestCc.OnlyIfCached && (requestCc.MaxStaleSet || requestCc.MaxStale > -1) { - response := stale - + } else if response == nil && !requestCc.OnlyIfCached && (requestCc.MaxStaleSet || requestCc.MaxStale > -1) { + for _, currentStorer := range s.Storers { + response = currentStorer.Prefix(storage.StalePrefix+cachedKey, rq, validator) + if response != nil { + break + } + } if nil != response && (!modeContext.Strict || rfc.ValidateCacheControl(response, requestCc)) { addTime, _ := time.ParseDuration(response.Header.Get(rfc.StoredTTLHeader)) rfc.SetCacheStatusHeader(response) - responseCc, _ := cacheobject.ParseResponseCacheControl(rfc.HeaderAllCommaSepValuesString(response.Header, "Cache-Control")) + responseCc, _ := cacheobject.ParseResponseCacheControl(response.Header.Get("Cache-Control")) if responseCc.StaleWhileRevalidate > 0 { - for h, v := range response.Header { - customWriter.Header()[h] = v - } - customWriter.WriteHeader(response.StatusCode) + customWriter.Headers = response.Header + customWriter.statusCode = response.StatusCode rfc.HitStaleCache(&response.Header) _, _ = io.Copy(customWriter.Buf, response.Body) _, err := customWriter.Send() - customWriter = NewCustomWriter(req, rw, bufPool) - go func(v *core.Revalidator, goCw *CustomWriter, goRq *http.Request, goNext func(http.ResponseWriter, *http.Request) error, goCc *cacheobject.RequestCacheDirectives, goCk string) { + customWriter = NewCustomWriter(rq, rw, bufPool) + go func(v *rfc.Revalidator, goCw *CustomWriter, goRq *http.Request, goNext func(http.ResponseWriter, *http.Request) error, goCc *cacheobject.RequestCacheDirectives, goCk string) { _ = s.Revalidate(v, goNext, goCw, goRq, goCc, goCk) - }(validator, customWriter, req, next, requestCc, cachedKey) + }(validator, customWriter, rq, next, requestCc, cachedKey) buf := s.bufPool.Get().(*bytes.Buffer) buf.Reset() defer s.bufPool.Put(buf) @@ -708,16 +479,15 @@ func (s *SouinBaseHandler) ServeHTTP(rw http.ResponseWriter, rq *http.Request, n } if responseCc.MustRevalidate || responseCc.NoCachePresent || validator.NeedRevalidation { - req.Header["If-None-Match"] = append(req.Header["If-None-Match"], validator.ResponseETag) - err := s.Revalidate(validator, next, customWriter, req, requestCc, cachedKey) - statusCode := customWriter.GetStatusCode() + rq.Header["If-None-Match"] = append(rq.Header["If-None-Match"], validator.ResponseETag) + err := s.Revalidate(validator, next, customWriter, rq, requestCc, cachedKey) if err != nil { if responseCc.StaleIfError > -1 || requestCc.StaleIfError > 0 { - code := fmt.Sprintf("; fwd-status=%d", statusCode) + code := fmt.Sprintf("; fwd-status=%d", customWriter.statusCode) + customWriter.Headers = response.Header + customWriter.statusCode = response.StatusCode rfc.HitStaleCache(&response.Header) response.Header.Set("Cache-Status", response.Header.Get("Cache-Status")+code) - maps.Copy(customWriter.Header(), response.Header) - customWriter.WriteHeader(response.StatusCode) _, _ = io.Copy(customWriter.Buf, response.Body) _, err := customWriter.Send() @@ -730,11 +500,11 @@ func (s *SouinBaseHandler) ServeHTTP(rw http.ResponseWriter, rq *http.Request, n return err } - if statusCode == http.StatusNotModified { + if customWriter.statusCode == http.StatusNotModified { if !validator.Matched { rfc.SetCacheStatusHeader(response) - customWriter.WriteHeader(response.StatusCode) - maps.Copy(customWriter.Header(), response.Header) + customWriter.statusCode = response.StatusCode + customWriter.Headers = response.Header _, _ = io.Copy(customWriter.Buf, response.Body) _, _ = customWriter.Send() @@ -742,8 +512,8 @@ func (s *SouinBaseHandler) ServeHTTP(rw http.ResponseWriter, rq *http.Request, n } } - if statusCode != http.StatusNotModified && validator.Matched { - customWriter.WriteHeader(http.StatusNotModified) + if customWriter.statusCode != http.StatusNotModified && validator.Matched { + customWriter.statusCode = http.StatusNotModified customWriter.Buf.Reset() _, _ = customWriter.Send() @@ -756,9 +526,9 @@ func (s *SouinBaseHandler) ServeHTTP(rw http.ResponseWriter, rq *http.Request, n } if rfc.ValidateMaxAgeCachedStaleResponse(requestCc, response, int(addTime.Seconds())) != nil { - customWriter.WriteHeader(response.StatusCode) + customWriter.Headers = response.Header + customWriter.statusCode = response.StatusCode rfc.HitStaleCache(&response.Header) - maps.Copy(customWriter.Header(), response.Header) _, _ = io.Copy(customWriter.Buf, response.Body) _, err := customWriter.Send() @@ -769,17 +539,15 @@ func (s *SouinBaseHandler) ServeHTTP(rw http.ResponseWriter, rq *http.Request, n } errorCacheCh := make(chan error) - go func(vr *http.Request, cw *CustomWriter) { - prometheus.Increment(prometheus.NoCachedResponseCounter) - errorCacheCh <- s.Upstream(cw, vr, next, requestCc, cachedKey) - }(req, customWriter) + go func() { + errorCacheCh <- s.Upstream(customWriter, rq, next, requestCc, cachedKey) + }() select { - case <-req.Context().Done(): - switch req.Context().Err() { + case <-rq.Context().Done(): + switch rq.Context().Err() { case baseCtx.DeadlineExceeded: customWriter.WriteHeader(http.StatusGatewayTimeout) - s.Configuration.GetLogger().Sugar().Infof("Internal server error on endpoint %s: %v", req.URL, s.Storers) rw.Header().Set("Cache-Status", cacheName+"; fwd=bypass; detail=DEADLINE-EXCEEDED") _, _ = customWriter.Rw.Write([]byte("Internal server error")) return baseCtx.DeadlineExceeded @@ -789,12 +557,8 @@ func (s *SouinBaseHandler) ServeHTTP(rw http.ResponseWriter, rq *http.Request, n return nil } case v := <-errorCacheCh: - switch v { - case nil: + if v == nil { _, _ = customWriter.Send() - case Upstream50xError: - _, _ = customWriter.Send() - return nil } return v } diff --git a/plugins/traefik/vendor/github.com/darkweak/souin/pkg/middleware/writer.go b/plugins/traefik/vendor/github.com/darkweak/souin/pkg/middleware/writer.go index d02546b17..ddbccfdbd 100644 --- a/plugins/traefik/vendor/github.com/darkweak/souin/pkg/middleware/writer.go +++ b/plugins/traefik/vendor/github.com/darkweak/souin/pkg/middleware/writer.go @@ -3,7 +3,7 @@ package middleware import ( "bytes" "net/http" - "strconv" + "strings" "sync" "github.com/darkweak/go-esi/esi" @@ -37,6 +37,7 @@ type CustomWriter struct { headersSent bool mutex *sync.Mutex statusCode int + // size int } // Header will write the response headers @@ -49,14 +50,6 @@ func (r *CustomWriter) Header() http.Header { return r.Rw.Header() } -// GetStatusCode returns the response status code -func (r *CustomWriter) GetStatusCode() int { - r.mutex.Lock() - defer r.mutex.Unlock() - - return r.statusCode -} - // WriteHeader will write the response headers func (r *CustomWriter) WriteHeader(code int) { r.mutex.Lock() @@ -64,13 +57,14 @@ func (r *CustomWriter) WriteHeader(code int) { if r.headersSent { return } + r.Headers = r.Rw.Header() r.statusCode = code + // r.headersSent = true + // r.Rw.WriteHeader(code) } // Write will write the response body func (r *CustomWriter) Write(b []byte) (int, error) { - r.mutex.Lock() - defer r.mutex.Unlock() r.Buf.Grow(len(b)) _, _ = r.Buf.Write(b) @@ -79,17 +73,24 @@ func (r *CustomWriter) Write(b []byte) (int, error) { // Send delays the response to handle Cache-Status func (r *CustomWriter) Send() (int, error) { + contentLength := r.Headers.Get(rfc.StoredLengthHeader) + if contentLength != "" { + r.Header().Set("Content-Length", contentLength) + } defer r.Buf.Reset() - r.Header().Set("Content-Length", r.Header().Get(rfc.StoredLengthHeader)) b := esi.Parse(r.Buf.Bytes(), r.Req) - if len(b) != 0 { - r.Header().Set("Content-Length", strconv.Itoa(len(b))) + for h, v := range r.Headers { + if len(v) > 0 { + r.Rw.Header().Set(h, strings.Join(v, ", ")) + } } r.Header().Del(rfc.StoredLengthHeader) r.Header().Del(rfc.StoredTTLHeader) if !r.headersSent { - r.Rw.WriteHeader(r.GetStatusCode()) + + // r.Rw.Header().Set("Content-Length", fmt.Sprintf("%d", len(b))) + r.Rw.WriteHeader(r.statusCode) r.headersSent = true } diff --git a/plugins/traefik/vendor/github.com/darkweak/souin/pkg/rfc/revalidation.go b/plugins/traefik/vendor/github.com/darkweak/souin/pkg/rfc/revalidation.go index c2d63e4aa..c0eb363b3 100644 --- a/plugins/traefik/vendor/github.com/darkweak/souin/pkg/rfc/revalidation.go +++ b/plugins/traefik/vendor/github.com/darkweak/souin/pkg/rfc/revalidation.go @@ -4,8 +4,6 @@ import ( "net/http" "strings" "time" - - "github.com/darkweak/storages/core" ) type Revalidator struct { @@ -25,7 +23,56 @@ type Revalidator struct { ResponseETag string } -func ParseRequest(req *http.Request) *core.Revalidator { +func ValidateETagFromHeader(etag string, validator *Revalidator) { + validator.ResponseETag = etag + validator.NeedRevalidation = validator.NeedRevalidation || validator.ResponseETag != "" + validator.Matched = validator.ResponseETag == "" || (validator.ResponseETag != "" && len(validator.RequestETags) == 0) + + if len(validator.RequestETags) == 0 { + validator.NotModified = false + return + } + + // If-None-Match + if validator.IfNoneMatchPresent { + for _, ifNoneMatch := range validator.IfNoneMatch { + // Asrterisk special char to match any of ETag + if ifNoneMatch == "*" { + validator.Matched = true + return + } + if ifNoneMatch == validator.ResponseETag { + validator.Matched = true + return + } + } + + validator.Matched = false + return + } + + // If-Match + if validator.IfMatchPresent { + validator.Matched = false + if validator.ResponseETag == "" { + return + } + + for _, ifMatch := range validator.IfMatch { + // Asrterisk special char to match any of ETag + if ifMatch == "*" { + validator.Matched = true + return + } + if ifMatch == validator.ResponseETag { + validator.Matched = true + return + } + } + } +} + +func ParseRequest(req *http.Request) *Revalidator { var rqEtags []string if len(req.Header.Get("If-None-Match")) > 0 { rqEtags = strings.Split(req.Header.Get("If-None-Match"), ",") @@ -33,7 +80,7 @@ func ParseRequest(req *http.Request) *core.Revalidator { for i, tag := range rqEtags { rqEtags[i] = strings.Trim(tag, " ") } - validator := core.Revalidator{ + validator := Revalidator{ NotModified: len(rqEtags) > 0, RequestETags: rqEtags, } diff --git a/plugins/traefik/vendor/github.com/darkweak/souin/pkg/rfc/vary.go b/plugins/traefik/vendor/github.com/darkweak/souin/pkg/rfc/vary.go index 2c55dc3dd..6ac7a9e42 100644 --- a/plugins/traefik/vendor/github.com/darkweak/souin/pkg/rfc/vary.go +++ b/plugins/traefik/vendor/github.com/darkweak/souin/pkg/rfc/vary.go @@ -4,7 +4,6 @@ import ( "fmt" "net/http" "net/url" - "slices" "strings" ) @@ -33,8 +32,10 @@ func GetVariedCacheKey(rq *http.Request, headers []string) string { // or '*' alone when the header contains it. func VariedHeaderAllCommaSepValues(headers http.Header) ([]string, bool) { vals := HeaderAllCommaSepValues(headers, "Vary") - if slices.Contains(vals, "*") { - return []string{"*"}, true + for _, v := range vals { + if v == "*" { + return []string{"*"}, true + } } return vals, false } diff --git a/plugins/traefik/vendor/github.com/darkweak/souin/pkg/storage/abstractProvider.go b/plugins/traefik/vendor/github.com/darkweak/souin/pkg/storage/abstractProvider.go new file mode 100644 index 000000000..65b1c81f3 --- /dev/null +++ b/plugins/traefik/vendor/github.com/darkweak/souin/pkg/storage/abstractProvider.go @@ -0,0 +1,57 @@ +package storage + +import ( + "net/http" + "net/url" + "strings" + + "github.com/darkweak/souin/configurationtypes" + "github.com/darkweak/souin/pkg/storage/types" +) + +const ( + VarySeparator = "{-VARY-}" + DecodedHeaderSeparator = ";" + encodedHeaderSemiColonSeparator = "%3B" + encodedHeaderColonSeparator = "%3A" + StalePrefix = "STALE_" +) + +type StorerInstanciator func(configurationtypes.AbstractConfigurationInterface) (types.Storer, error) + +func NewStorages(configuration configurationtypes.AbstractConfigurationInterface) ([]types.Storer, error) { + s, err := CacheConnectionFactory(configuration) + return []types.Storer{s}, err +} + +func varyVoter(baseKey string, req *http.Request, currentKey string) bool { + if currentKey == baseKey { + return true + } + + if strings.Contains(currentKey, VarySeparator) && strings.HasPrefix(currentKey, baseKey+VarySeparator) { + list := currentKey[(strings.LastIndex(currentKey, VarySeparator) + len(VarySeparator)):] + if len(list) == 0 { + return false + } + + for _, item := range strings.Split(list, ";") { + index := strings.LastIndex(item, ":") + if len(item) < index+1 { + return false + } + + hVal := item[index+1:] + if strings.Contains(hVal, encodedHeaderSemiColonSeparator) || strings.Contains(hVal, encodedHeaderColonSeparator) { + hVal, _ = url.QueryUnescape(hVal) + } + if req.Header.Get(item[:index]) != hVal { + return false + } + } + + return true + } + + return false +} diff --git a/plugins/traefik/vendor/github.com/darkweak/souin/pkg/storage/abstractProvider_test.go b/plugins/traefik/vendor/github.com/darkweak/souin/pkg/storage/abstractProvider_test.go new file mode 100644 index 000000000..a3905bbc6 --- /dev/null +++ b/plugins/traefik/vendor/github.com/darkweak/souin/pkg/storage/abstractProvider_test.go @@ -0,0 +1,16 @@ +package storage + +import ( + "testing" + + "github.com/darkweak/souin/tests" +) + +func TestInitializeProvider(t *testing.T) { + c := tests.MockConfiguration(tests.BaseConfiguration) + p := InitializeProvider(c) + err := p.Init() + if nil != err { + t.Error("Init shouldn't crash") + } +} diff --git a/plugins/traefik/vendor/github.com/darkweak/souin/pkg/storage/cacheProvider.go b/plugins/traefik/vendor/github.com/darkweak/souin/pkg/storage/cacheProvider.go new file mode 100644 index 000000000..8331486c6 --- /dev/null +++ b/plugins/traefik/vendor/github.com/darkweak/souin/pkg/storage/cacheProvider.go @@ -0,0 +1,142 @@ +package storage + +import ( + "bufio" + "bytes" + "net/http" + "regexp" + "strings" + "time" + + "github.com/akyoto/cache" + t "github.com/darkweak/souin/configurationtypes" + "github.com/darkweak/souin/pkg/rfc" + "github.com/darkweak/souin/pkg/storage/types" +) + +// Cache provider type +type Cache struct { + *cache.Cache + stale time.Duration +} + +var sharedCache *Cache + +// CacheConnectionFactory function create new Cache instance +func CacheConnectionFactory(c t.AbstractConfigurationInterface) (types.Storer, error) { + provider := cache.New(1 * time.Second) + + if sharedCache == nil { + sharedCache = &Cache{Cache: provider, stale: c.GetDefaultCache().GetStale()} + } + + return sharedCache, nil +} + +// Name returns the storer name +func (provider *Cache) Name() string { + return "CACHE" +} + +// ListKeys method returns the list of existing keys +func (provider *Cache) ListKeys() []string { + var keys []string + provider.Cache.Range(func(key, _ interface{}) bool { + keys = append(keys, key.(string)) + return true + }) + + return keys +} + +// MapKeys method returns the map of existing keys +func (provider *Cache) MapKeys(prefix string) map[string]string { + keys := map[string]string{} + provider.Cache.Range(func(key, value interface{}) bool { + if strings.HasPrefix(key.(string), prefix) { + k, _ := strings.CutPrefix(key.(string), prefix) + keys[k] = string(value.([]byte)) + } + return true + }) + + return keys +} + +// Get method returns the populated response if exists, empty response then +func (provider *Cache) Get(key string) []byte { + result, found := provider.Cache.Get(key) + + if !found { + return []byte{} + } + + return result.([]byte) +} + +// Prefix method returns the populated response if exists, empty response then +func (provider *Cache) Prefix(key string, req *http.Request, validator *rfc.Revalidator) *http.Response { + var result *http.Response + + provider.Cache.Range(func(k, v interface{}) bool { + if !strings.HasPrefix(k.(string), key) { + return true + } + + if k == key || varyVoter(key, req, k.(string)) { + if res, err := http.ReadResponse(bufio.NewReader(bytes.NewBuffer(v.([]byte))), req); err == nil { + rfc.ValidateETagFromHeader(res.Header.Get("etag"), validator) + if validator.Matched { + result = res + return false + } + } + return true + } + + return true + }) + + return result +} + +// Set method will store the response in Cache provider +func (provider *Cache) Set(key string, value []byte, duration time.Duration) error { + provider.Cache.Set(key, value, duration) + provider.Cache.Set(StalePrefix+key, value, provider.stale+duration) + + return nil +} + +// Delete method will delete the response in Cache provider if exists corresponding to key param +func (provider *Cache) Delete(key string) { + provider.Cache.Delete(key) +} + +// DeleteMany method will delete the responses in Cache provider if exists corresponding to the regex key param +func (provider *Cache) DeleteMany(key string) { + re, e := regexp.Compile(key) + + if e != nil { + return + } + + provider.Cache.Range(func(k, _ interface{}) bool { + if re.MatchString(k.(string)) { + provider.Delete(k.(string)) + } + return true + }) +} + +// Init method will +func (provider *Cache) Init() error { + return nil +} + +// Reset method will reset or close provider +func (provider *Cache) Reset() error { + provider.DeleteMany("*") + + return nil +} diff --git a/plugins/traefik/vendor/github.com/darkweak/souin/pkg/storage/defaultProvider.go b/plugins/traefik/vendor/github.com/darkweak/souin/pkg/storage/defaultProvider.go deleted file mode 100644 index 8eb9ebf05..000000000 --- a/plugins/traefik/vendor/github.com/darkweak/souin/pkg/storage/defaultProvider.go +++ /dev/null @@ -1,205 +0,0 @@ -package storage - -import ( - "bytes" - "net/http" - "regexp" - "strings" - "sync" - "time" - - "github.com/darkweak/souin/configurationtypes" - "github.com/darkweak/souin/pkg/storage/types" - "github.com/darkweak/storages/core" - "github.com/pierrec/lz4/v4" - "go.uber.org/zap" -) - -// Default provider type -type Default struct { - m *sync.Map - stale time.Duration - logger *zap.Logger -} - -type item struct { - invalidAt time.Time - value []byte -} - -// Factory function create new Default instance -func Factory(c configurationtypes.AbstractConfigurationInterface) (types.Storer, error) { - return &Default{m: &sync.Map{}, logger: c.GetLogger(), stale: c.GetDefaultCache().GetStale()}, nil -} - -// Name returns the storer name -func (provider *Default) Name() string { - return types.DefaultStorageName -} - -// Uuid returns an unique identifier -func (provider *Default) Uuid() string { - return "" -} - -// MapKeys method returns a map with the key and value -func (provider *Default) MapKeys(prefix string) map[string]string { - now := time.Now() - keys := map[string]string{} - - provider.m.Range(func(key, value any) bool { - if strings.HasPrefix(key.(string), prefix) { - k, _ := strings.CutPrefix(key.(string), prefix) - if v, ok := value.(item); ok { - if v.invalidAt.After(now) { - keys[k] = string(v.value) - } - - return true - } - if v, ok := value.(*core.StorageMapper); ok { - for _, v := range v.Mapping { - if v.StaleTime.AsTime().After(now) { - keys[v.RealKey] = string(provider.Get(v.RealKey)) - } - } - } - } - - return true - }) - - return keys -} - -// ListKeys method returns the list of existing keys -func (provider *Default) ListKeys() []string { - now := time.Now() - keys := []string{} - - provider.m.Range(func(key, value any) bool { - if strings.HasPrefix(key.(string), core.MappingKeyPrefix) { - mapping, err := core.DecodeMapping(value.([]byte)) - if err == nil { - for _, v := range mapping.Mapping { - if v.StaleTime.AsTime().After(now) { - keys = append(keys, v.RealKey) - } else { - provider.m.Delete(v.RealKey) - } - } - } - } - - return true - }) - - return keys -} - -// Get method returns the populated response if exists, empty response then -func (provider *Default) Get(key string) []byte { - result, ok := provider.m.Load(key) - if !ok || result == nil { - return nil - } - - res, ok := result.(item) - if !ok { - return nil - } - - if res.invalidAt.After(time.Now()) { - return res.value - } - - return nil -} - -// GetMultiLevel tries to load the key and check if one of linked keys is a fresh/stale candidate. -func (provider *Default) GetMultiLevel(key string, req *http.Request, validator *core.Revalidator) (fresh *http.Response, stale *http.Response) { - result, found := provider.m.Load(core.MappingKeyPrefix + key) - if !found { - return - } - - fresh, stale, _ = core.MappingElection(provider, result.([]byte), req, validator, provider.logger) - - return -} - -// SetMultiLevel tries to store the key with the given value and update the mapping key to store metadata. -func (provider *Default) SetMultiLevel(baseKey, variedKey string, value []byte, variedHeaders http.Header, etag string, duration time.Duration, realKey string) error { - now := time.Now() - - var e error - compressed := new(bytes.Buffer) - if _, e = lz4.NewWriter(compressed).ReadFrom(bytes.NewReader(value)); e != nil { - provider.logger.Sugar().Errorf("Impossible to compress the key %s into Badger, %v", variedKey, e) - return e - } - - provider.m.Store(variedKey, item{ - invalidAt: now.Add(duration + provider.stale), - value: compressed.Bytes(), - }) - - mappingKey := core.MappingKeyPrefix + baseKey - item, ok := provider.m.Load(mappingKey) - var val []byte - if ok { - val = item.([]byte) - } - - val, e = core.MappingUpdater(variedKey, val, provider.logger, now, now.Add(duration), now.Add(duration+provider.stale), variedHeaders, etag, realKey) - if e != nil { - return e - } - - provider.logger.Sugar().Debugf("Store the new mapping for the key %s in Default", variedKey) - provider.m.Store(mappingKey, val) - return nil -} - -// Set method will store the response in Badger provider -func (provider *Default) Set(key string, value []byte, duration time.Duration) error { - provider.m.Store(key, item{ - invalidAt: time.Now().Add(duration), - value: value, - }) - - return nil -} - -// Delete method will delete the response in Badger provider if exists corresponding to key param -func (provider *Default) Delete(key string) { - provider.m.Delete(key) -} - -// DeleteMany method will delete the responses in Badger provider if exists corresponding to the regex key param -func (provider *Default) DeleteMany(key string) { - re, e := regexp.Compile(key) - - if e != nil { - return - } - - provider.m.Range(func(key, _ any) bool { - if re.MatchString(key.(string)) { - provider.m.Delete(key) - } - - return true - }) -} - -// Init method will -func (provider *Default) Init() error { - return nil -} - -// Reset method will reset or close provider -func (provider *Default) Reset() error { - provider.m = &sync.Map{} - return nil -} diff --git a/plugins/traefik/vendor/github.com/darkweak/souin/pkg/storage/types/types.go b/plugins/traefik/vendor/github.com/darkweak/souin/pkg/storage/types/types.go index e4c8603fc..67031c15e 100644 --- a/plugins/traefik/vendor/github.com/darkweak/souin/pkg/storage/types/types.go +++ b/plugins/traefik/vendor/github.com/darkweak/souin/pkg/storage/types/types.go @@ -4,25 +4,18 @@ import ( "net/http" "time" - "github.com/darkweak/storages/core" + "github.com/darkweak/souin/pkg/rfc" ) -const DefaultStorageName = "DEFAULT" -const OneYearDuration = 365 * 24 * time.Hour - type Storer interface { MapKeys(prefix string) map[string]string ListKeys() []string + Prefix(key string, req *http.Request, validator *rfc.Revalidator) *http.Response Get(key string) []byte Set(key string, value []byte, duration time.Duration) error Delete(key string) DeleteMany(key string) Init() error Name() string - Uuid() string Reset() error - - // Multi level storer to handle fresh/stale at once - GetMultiLevel(key string, req *http.Request, validator *core.Revalidator) (fresh *http.Response, stale *http.Response) - SetMultiLevel(baseKey, variedKey string, value []byte, variedHeaders http.Header, etag string, duration time.Duration, realKey string) error } diff --git a/plugins/traefik/vendor/github.com/darkweak/souin/pkg/surrogate/providers/common.go b/plugins/traefik/vendor/github.com/darkweak/souin/pkg/surrogate/providers/common.go index 4dcab8332..b6ccc973f 100644 --- a/plugins/traefik/vendor/github.com/darkweak/souin/pkg/surrogate/providers/common.go +++ b/plugins/traefik/vendor/github.com/darkweak/souin/pkg/surrogate/providers/common.go @@ -1,6 +1,7 @@ package providers import ( + "fmt" "net/http" "net/url" "regexp" @@ -9,9 +10,8 @@ import ( "time" "github.com/darkweak/souin/configurationtypes" + "github.com/darkweak/souin/pkg/storage" "github.com/darkweak/souin/pkg/storage/types" - "github.com/darkweak/storages/core" - "go.uber.org/zap" ) const ( @@ -28,34 +28,18 @@ const ( cacheTags = "Cache-Tags" cacheTag = "Cache-Tag" + stalePrefix = "STALE_" surrogatePrefix = "SURROGATE_" ) var storageToInfiniteTTLMap = map[string]time.Duration{ - "BADGER": types.OneYearDuration, - "ETCD": types.OneYearDuration, - "NUTS": 0, - "OLRIC": types.OneYearDuration, - "OTTER": types.OneYearDuration, - "REDIS": -1, - types.DefaultStorageName: types.OneYearDuration, + "CACHE": 365 * 24 * time.Hour, } func (s *baseStorage) ParseHeaders(value string) []string { return regexp.MustCompile(s.parent.getHeaderSeparator()+" *").Split(value, -1) } -func getCandidateHeader(header http.Header, getCandidates func() []string) (string, string) { - candidates := getCandidates() - for _, candidate := range candidates { - if h := header.Get(candidate); h != "" { - return candidate, h - } - } - - return candidates[len(candidates)-1], "" -} - func isSafeHTTPMethod(method string) bool { switch method { case http.MethodGet, http.MethodHead, http.MethodOptions, http.MethodTrace: @@ -95,31 +79,18 @@ type baseStorage struct { keysRegexp map[string]keysRegexpInner dynamic bool keepStale bool - logger *zap.Logger mu *sync.Mutex duration time.Duration } -func (s *baseStorage) init(config configurationtypes.AbstractConfigurationInterface, defaultStorerName string) { - if configuration, ok := config.GetSurrogateKeys()["_configuration"]; ok { - storer := core.GetRegisteredStorer(configuration.SurrogateConfiguration.Storer) - if storer == nil { - storer = core.GetRegisteredStorer(types.DefaultStorageName + "-") - if storer == nil { - config.GetLogger().Sugar().Errorf("Impossible to retrieve the storers %s for the surrogate-keys from it's configuration", configuration.SurrogateConfiguration.Storer) - } - } - - s.Storage = storer - } else { - storer := core.GetRegisteredStorer(defaultStorerName) - if storer == nil { - config.GetLogger().Sugar().Errorf("Impossible to retrieve the storers %s for the surrogate-keys", defaultStorerName) - } - - s.Storage = storer +func (s *baseStorage) init(config configurationtypes.AbstractConfigurationInterface, _ string) { + storers, err := storage.NewStorages(config) + if err != nil { + panic(fmt.Sprintf("Impossible to instanciate the storer for the surrogate-keys: %v", err)) } + s.Storage = storers[0] + s.Keys = config.GetSurrogateKeys() s.keepStale = config.GetDefaultCache().GetCDN().Strategy != "hard" keysRegexp := make(map[string]keysRegexpInner, len(s.Keys)) @@ -144,7 +115,6 @@ func (s *baseStorage) init(config configurationtypes.AbstractConfigurationInterf } s.dynamic = config.GetDefaultCache().GetCDN().Dynamic - s.logger = config.GetLogger() s.keysRegexp = keysRegexp s.mu = &sync.Mutex{} s.duration = storageToInfiniteTTLMap[s.Storage.Name()] @@ -155,8 +125,8 @@ func (s *baseStorage) storeTag(tag string, cacheKey string, re *regexp.Regexp) { s.mu.Lock() currentValue := string(s.Storage.Get(surrogatePrefix + tag)) if !re.MatchString(currentValue) { - s.logger.Sugar().Debugf("Store the tag %s", tag) - _ = s.Storage.Set(surrogatePrefix+tag, []byte(currentValue+souinStorageSeparator+cacheKey), s.duration) + fmt.Printf("Store the tag %s", tag) + _ = s.Storage.Set(surrogatePrefix+tag, []byte(currentValue+souinStorageSeparator+cacheKey), -1) } } @@ -182,7 +152,14 @@ func (*baseStorage) getOrderedSurrogateControlHeadersCandidate() []string { } func (s *baseStorage) GetSurrogateControl(header http.Header) (string, string) { - return getCandidateHeader(header, s.parent.getOrderedSurrogateControlHeadersCandidate) + parent := s.parent.getOrderedSurrogateControlHeadersCandidate() + for _, candidate := range parent { + if h := header.Get(candidate); h != "" { + return candidate, h + } + } + + return parent[len(parent)-1], "" } func (s *baseStorage) GetSurrogateControlName() string { @@ -190,15 +167,23 @@ func (s *baseStorage) GetSurrogateControlName() string { } func (s *baseStorage) getSurrogateKey(header http.Header) string { - _, v := getCandidateHeader(header, s.parent.getOrderedSurrogateKeyHeadersCandidate) - return v + for _, candidate := range s.parent.getOrderedSurrogateKeyHeadersCandidate() { + if h := header.Get(candidate); h != "" { + return h + } + } + + return "" } func (s *baseStorage) purgeTag(tag string) []string { - toInvalidate := string(s.Storage.Get(surrogatePrefix + tag)) - s.logger.Sugar().Debugf("Purge the tag %s", tag) + toInvalidate := string(s.Storage.Get(tag)) + fmt.Printf("Purge the tag %s", tag) + s.Storage.Delete(surrogatePrefix + tag) if !s.keepStale { - s.Storage.Delete(surrogatePrefix + tag) + toInvalidate = toInvalidate + "," + string(s.Storage.Get(stalePrefix+tag)) + fmt.Printf("Purge the tag %s", stalePrefix+tag) + s.Storage.Delete(surrogatePrefix + stalePrefix + tag) } return strings.Split(toInvalidate, souinStorageSeparator) } @@ -208,8 +193,11 @@ func (s *baseStorage) Store(response *http.Response, cacheKey string) error { h := response.Header cacheKey = url.QueryEscape(cacheKey) + staleKey := stalePrefix + cacheKey urlRegexp := regexp.MustCompile("(^|" + regexp.QuoteMeta(souinStorageSeparator) + ")" + regexp.QuoteMeta(cacheKey) + "(" + regexp.QuoteMeta(souinStorageSeparator) + "|$)") + staleUrlRegexp := regexp.MustCompile("(^|" + regexp.QuoteMeta(souinStorageSeparator) + ")" + regexp.QuoteMeta(staleKey) + "(" + regexp.QuoteMeta(souinStorageSeparator) + "|$)") + keys := s.ParseHeaders(s.parent.getSurrogateKey(h)) for _, key := range keys { @@ -217,16 +205,19 @@ func (s *baseStorage) Store(response *http.Response, cacheKey string) error { if controls := s.ParseHeaders(v); len(controls) != 0 { if len(controls) == 1 && controls[0] == "" { s.storeTag(key, cacheKey, urlRegexp) + s.storeTag(stalePrefix+key, staleKey, staleUrlRegexp) continue } for _, control := range controls { if s.parent.candidateStore(control) { s.storeTag(key, cacheKey, urlRegexp) + s.storeTag(stalePrefix+key, staleKey, staleUrlRegexp) } } } else { s.storeTag(key, cacheKey, urlRegexp) + s.storeTag(stalePrefix+key, staleKey, staleUrlRegexp) } } @@ -242,8 +233,6 @@ func (s *baseStorage) Purge(header http.Header) (cacheKeys []string, surrogateKe toInvalidate = append(toInvalidate, s.purgeTag(su)...) } - s.logger.Sugar().Debugf("Purge the following tags: %+v", toInvalidate) - return uniqueTag(toInvalidate), surrogates } @@ -263,5 +252,7 @@ func (s *baseStorage) List() map[string]string { // Destruct method will shutdown properly the provider func (s *baseStorage) Destruct() error { - return s.Storage.Reset() + s.Storage.DeleteMany(surrogatePrefix + ".*") + + return nil } diff --git a/plugins/traefik/vendor/github.com/darkweak/souin/tests/mock.go b/plugins/traefik/vendor/github.com/darkweak/souin/tests/mock.go index db3a0d5d7..d88e6f532 100644 --- a/plugins/traefik/vendor/github.com/darkweak/souin/tests/mock.go +++ b/plugins/traefik/vendor/github.com/darkweak/souin/tests/mock.go @@ -6,6 +6,7 @@ import ( "os" "github.com/darkweak/souin/configurationtypes" + "github.com/darkweak/storages/core" "go.uber.org/zap" "go.uber.org/zap/zapcore" "gopkg.in/yaml.v3" @@ -25,7 +26,7 @@ type testConfiguration struct { SSLProviders []string `yaml:"ssl_providers"` URLs map[string]configurationtypes.URL `yaml:"urls"` LogLevel string `yaml:"log_level"` - logger *zap.Logger + logger core.Logger PluginName string Ykeys map[string]configurationtypes.SurrogateKeys `yaml:"ykeys"` SurrogateKeys map[string]configurationtypes.SurrogateKeys `yaml:"surrogate_keys"` @@ -67,12 +68,12 @@ func (c *testConfiguration) GetLogLevel() string { } // GetLogger get the logger -func (c *testConfiguration) GetLogger() *zap.Logger { +func (c *testConfiguration) GetLogger() core.Logger { return c.logger } // SetLogger set the logger -func (c *testConfiguration) SetLogger(l *zap.Logger) { +func (c *testConfiguration) SetLogger(l core.Logger) { c.logger = l } @@ -516,7 +517,7 @@ func MockConfiguration(configurationToLoad func() string) *testConfiguration { }, } logger, _ := cfg.Build() - config.SetLogger(logger) + config.SetLogger(logger.Sugar()) return &config } diff --git a/plugins/traefik/vendor/github.com/darkweak/storages/core/core.go b/plugins/traefik/vendor/github.com/darkweak/storages/core/core.go index 5df72546a..f569856af 100644 --- a/plugins/traefik/vendor/github.com/darkweak/storages/core/core.go +++ b/plugins/traefik/vendor/github.com/darkweak/storages/core/core.go @@ -1,3 +1,5 @@ +//go:build !wasi && !wasm + package core import ( @@ -8,7 +10,6 @@ import ( "time" lz4 "github.com/pierrec/lz4/v4" - "go.uber.org/zap" "google.golang.org/protobuf/proto" "google.golang.org/protobuf/types/known/timestamppb" ) @@ -49,7 +50,7 @@ func DecodeMapping(item []byte) (*StorageMapper, error) { return mapping, e } -func MappingElection(provider Storer, item []byte, req *http.Request, validator *Revalidator, logger *zap.Logger) (resultFresh *http.Response, resultStale *http.Response, e error) { +func MappingElection(provider Storer, item []byte, req *http.Request, validator *Revalidator, logger Logger) (resultFresh *http.Response, resultStale *http.Response, e error) { mapping := &StorageMapper{} if len(item) != 0 { @@ -86,12 +87,12 @@ func MappingElection(provider Storer, item []byte, req *http.Request, validator _, _ = reader.WriteTo(bufW) if resultFresh, e = http.ReadResponse(bufio.NewReader(bufW), req); e != nil { - logger.Sugar().Errorf("An error occurred while reading response for the key %s: %v", keyName, e) + logger.Errorf("An error occurred while reading response for the key %s: %v", keyName, e) return resultFresh, resultStale, e } - logger.Sugar().Debugf("The stored key %s matched the current iteration key ETag %+v", keyName, validator) + logger.Debugf("The stored key %s matched the current iteration key ETag %+v", keyName, validator) return resultFresh, resultStale, e } @@ -106,28 +107,28 @@ func MappingElection(provider Storer, item []byte, req *http.Request, validator _, _ = reader.WriteTo(bufW) if resultStale, e = http.ReadResponse(bufio.NewReader(bufW), req); e != nil { - logger.Sugar().Errorf("An error occurred while reading response for the key %s: %v", keyName, e) + logger.Errorf("An error occurred while reading response for the key %s: %v", keyName, e) return resultFresh, resultStale, e } - logger.Sugar().Debugf("The stored key %s matched the current iteration key ETag %+v as stale", keyName, validator) + logger.Debugf("The stored key %s matched the current iteration key ETag %+v as stale", keyName, validator) } } } else { - logger.Sugar().Debugf("The stored key %s didn't match the current iteration key ETag %+v", keyName, validator) + logger.Debugf("The stored key %s didn't match the current iteration key ETag %+v", keyName, validator) } } return resultFresh, resultStale, e } -func MappingUpdater(key string, item []byte, logger *zap.Logger, now, freshTime, staleTime time.Time, variedHeaders http.Header, etag, realKey string) (val []byte, e error) { +func MappingUpdater(key string, item []byte, logger Logger, now, freshTime, staleTime time.Time, variedHeaders http.Header, etag, realKey string) (val []byte, e error) { mapping := &StorageMapper{} if len(item) != 0 { e = proto.Unmarshal(item, mapping) if e != nil { - logger.Sugar().Errorf("Impossible to decode the key %s, %v", key, e) + logger.Errorf("Impossible to decode the key %s, %v", key, e) return nil, e } @@ -157,7 +158,7 @@ func MappingUpdater(key string, item []byte, logger *zap.Logger, now, freshTime, val, e = proto.Marshal(mapping) if e != nil { - logger.Sugar().Errorf("Impossible to encode the mapping value for the key %s, %v", key, e) + logger.Errorf("Impossible to encode the mapping value for the key %s, %v", key, e) return nil, e } diff --git a/plugins/traefik/vendor/github.com/darkweak/storages/core/core_wasm.go b/plugins/traefik/vendor/github.com/darkweak/storages/core/core_wasm.go new file mode 100644 index 000000000..9c50644c3 --- /dev/null +++ b/plugins/traefik/vendor/github.com/darkweak/storages/core/core_wasm.go @@ -0,0 +1,167 @@ +//go:build wasi || wasm + +package core + +import ( + "bufio" + "bytes" + "net/http" + "strings" + "time" + + lz4 "github.com/pierrec/lz4/v4" + "google.golang.org/protobuf/proto" + "google.golang.org/protobuf/types/known/timestamppb" +) + +type Storer interface { + MapKeys(prefix string) map[string]string + ListKeys() []string + Get(key string) []byte + Set(key string, value []byte, duration time.Duration) error + Delete(key string) + DeleteMany(key string) + Init() error + Name() string + Uuid() string + Reset() error + + // Multi level storer to handle fresh/stale at once + GetMultiLevel(key string, req *http.Request, validator *Revalidator) (fresh *http.Response, stale *http.Response) + SetMultiLevel(baseKey, variedKey string, value []byte, variedHeaders http.Header, etag string, duration time.Duration, realKey string) error +} + +// CacheProvider config. +type CacheProvider struct { + // URL to connect to the storage system. + URL string `json:"url" yaml:"url"` + // Path to the configuration file. + Path string `json:"path" yaml:"path"` + // Declare the cache provider directly in the Souin configuration. + Configuration interface{} `json:"configuration" yaml:"configuration"` +} + +const MappingKeyPrefix = "IDX_" + +func DecodeMapping(item []byte) (*StorageMapper, error) { + mapping := &StorageMapper{} + e := proto.Unmarshal(item, mapping) + + return mapping, e +} + +func MappingElection(provider Storer, item []byte, req *http.Request, validator *Revalidator, logger Logger) (resultFresh *http.Response, resultStale *http.Response, e error) { + mapping := &StorageMapper{} + + if len(item) != 0 { + mapping, e = DecodeMapping(item) + if e != nil { + return resultFresh, resultStale, e + } + } + + for keyName, keyItem := range mapping.GetMapping() { + valid := true + + for hname, hval := range keyItem.GetVariedHeaders() { + if req.Header.Get(hname) != strings.Join(hval.GetHeaderValue(), ", ") { + valid = false + + break + } + } + + if !valid { + continue + } + + ValidateETagFromHeader(keyItem.GetEtag(), validator) + + if validator.Matched { + // If the key is fresh enough. + if time.Since(keyItem.GetFreshTime().AsTime()) < 0 { + response := provider.Get(keyName) + if response != nil { + bufW := new(bytes.Buffer) + reader := lz4.NewReader(bytes.NewBuffer(response)) + _, _ = reader.WriteTo(bufW) + + if resultFresh, e = http.ReadResponse(bufio.NewReader(bufW), req); e != nil { + logger.Errorf("An error occurred while reading response for the key %s: %v", keyName, e) + + return resultFresh, resultStale, e + } + + logger.Debugf("The stored key %s matched the current iteration key ETag %+v", keyName, validator) + + return resultFresh, resultStale, e + } + } + + // If the key is still stale. + if time.Since(keyItem.GetStaleTime().AsTime()) < 0 { + response := provider.Get(keyName) + if response != nil { + bufW := new(bytes.Buffer) + reader := lz4.NewReader(bytes.NewBuffer(response)) + _, _ = reader.WriteTo(bufW) + + if resultStale, e = http.ReadResponse(bufio.NewReader(bufW), req); e != nil { + logger.Errorf("An error occurred while reading response for the key %s: %v", keyName, e) + + return resultFresh, resultStale, e + } + + logger.Debugf("The stored key %s matched the current iteration key ETag %+v as stale", keyName, validator) + } + } + } else { + logger.Debugf("The stored key %s didn't match the current iteration key ETag %+v", keyName, validator) + } + } + + return resultFresh, resultStale, e +} + +func MappingUpdater(key string, item []byte, logger Logger, now, freshTime, staleTime time.Time, variedHeaders http.Header, etag, realKey string) (val []byte, e error) { + mapping := &StorageMapper{} + if len(item) != 0 { + e = proto.Unmarshal(item, mapping) + if e != nil { + logger.Errorf("Impossible to decode the key %s, %v", key, e) + + return nil, e + } + } + + if mapping.GetMapping() == nil { + mapping.Mapping = make(map[string]*KeyIndex) + } + + var pbvariedeheader map[string]*KeyIndexStringList + if variedHeaders != nil { + pbvariedeheader = make(map[string]*KeyIndexStringList) + } + + for k, v := range variedHeaders { + pbvariedeheader[k] = &KeyIndexStringList{HeaderValue: v} + } + + mapping.Mapping[key] = &KeyIndex{ + StoredAt: timestamppb.New(now), + FreshTime: timestamppb.New(freshTime), + StaleTime: timestamppb.New(staleTime), + VariedHeaders: pbvariedeheader, + Etag: etag, + RealKey: realKey, + } + + val, e = proto.Marshal(mapping) + if e != nil { + logger.Errorf("Impossible to encode the mapping value for the key %s, %v", key, e) + + return nil, e + } + + return val, e +} diff --git a/plugins/traefik/vendor/github.com/darkweak/storages/core/logger.go b/plugins/traefik/vendor/github.com/darkweak/storages/core/logger.go new file mode 100644 index 000000000..8e0c9499f --- /dev/null +++ b/plugins/traefik/vendor/github.com/darkweak/storages/core/logger.go @@ -0,0 +1,18 @@ +package core + +type Logger interface { + Debug(args ...interface{}) + Info(args ...interface{}) + Warn(args ...interface{}) + Error(args ...interface{}) + DPanic(args ...interface{}) + Panic(args ...interface{}) + Fatal(args ...interface{}) + Debugf(template string, args ...interface{}) + Infof(template string, args ...interface{}) + Warnf(template string, args ...interface{}) + Errorf(template string, args ...interface{}) + DPanicf(template string, args ...interface{}) + Panicf(template string, args ...interface{}) + Fatalf(template string, args ...interface{}) +} diff --git a/plugins/traefik/vendor/modules.txt b/plugins/traefik/vendor/modules.txt index bcf1bfa3f..cac691281 100644 --- a/plugins/traefik/vendor/modules.txt +++ b/plugins/traefik/vendor/modules.txt @@ -81,7 +81,7 @@ github.com/darkweak/souin/pkg/storage/types github.com/darkweak/souin/pkg/surrogate github.com/darkweak/souin/pkg/surrogate/providers github.com/darkweak/souin/tests -# github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 +# github.com/darkweak/storages/core v0.0.6 ## explicit; go 1.22.1 github.com/darkweak/storages/core # github.com/dgraph-io/badger v1.6.2 diff --git a/plugins/tyk/go.mod b/plugins/tyk/go.mod index f35906b74..9c3c0cd34 100644 --- a/plugins/tyk/go.mod +++ b/plugins/tyk/go.mod @@ -37,7 +37,7 @@ require ( github.com/clbanning/mxj v1.8.4 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect github.com/darkweak/go-esi v0.0.5 // indirect - github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 // indirect + github.com/darkweak/storages/core v0.0.6 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/dgraph-io/badger v1.6.2 // indirect github.com/dgraph-io/badger/v2 v2.2007.4 // indirect diff --git a/plugins/tyk/go.sum b/plugins/tyk/go.sum index 4029223fe..d2b0df3f8 100644 --- a/plugins/tyk/go.sum +++ b/plugins/tyk/go.sum @@ -243,8 +243,8 @@ github.com/cyberdelia/templates v0.0.0-20141128023046-ca7fffd4298c/go.mod h1:GyV github.com/cyphar/filepath-securejoin v0.2.3/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4= github.com/darkweak/go-esi v0.0.5 h1:b9LHI8Tz46R+i6p8avKPHAIBRQUCZDebNmKm5w/Zrns= github.com/darkweak/go-esi v0.0.5/go.mod h1:koCJqwum1u6mslyZuq/Phm6hfG1K3ZK5Y7jrUBTH654= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 h1:WsLHVD+WDxYh5SuZzpZXnhnDruZVrrvnYsHw08izs1s= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881/go.mod h1:mWN/TO5aLYt2jK6EweZ4JFXXPYmYwYFUQ4YBuIlBkpc= +github.com/darkweak/storages/core v0.0.6 h1:Yj0Z98HsC9Kd9cmjAXLagx6aU7AtXuL++RV2W+FSjc0= +github.com/darkweak/storages/core v0.0.6/go.mod h1:ajTpB9IFLRIRY0EEFLjM5vtsrcNTh+TJK9yRxgG5/wY= github.com/dave/jennifer v1.2.0/go.mod h1:fIb+770HOpJ2fmN9EPPKOqm1vMGhB+TwXKMZhrIygKg= github.com/dave/jennifer v1.4.0/go.mod h1:fIb+770HOpJ2fmN9EPPKOqm1vMGhB+TwXKMZhrIygKg= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/plugins/tyk/override/cache/surrogate/providers/common.go b/plugins/tyk/override/cache/surrogate/providers/common.go index ee04e6b12..e2086fb27 100644 --- a/plugins/tyk/override/cache/surrogate/providers/common.go +++ b/plugins/tyk/override/cache/surrogate/providers/common.go @@ -116,7 +116,7 @@ func (s *baseStorage) storeTag(tag string, cacheKey string, re *regexp.Regexp) { } if s.dynamic || b { if !re.MatchString(currentValue.(string)) { - s.logger.Sugar().Debugf("Store the tag %s", tag) + s.logger.Debugf("Store the tag %s", tag) s.Storage.Store(tag, currentValue.(string)+souinStorageSeparator+cacheKey) } } @@ -156,7 +156,7 @@ func (s *baseStorage) purgeTag(tag string) []string { if toInvalidate == nil { toInvalidate = "" } - s.logger.Sugar().Debugf("Purge the tag %s", tag) + s.logger.Debugf("Purge the tag %s", tag) s.Storage.Delete(tag) if !s.keepStale { stale, _ := s.Storage.Load(stalePrefix + tag) @@ -164,7 +164,7 @@ func (s *baseStorage) purgeTag(tag string) []string { stale = "" } toInvalidate = toInvalidate.(string) + "," + stale.(string) - s.logger.Sugar().Debugf("Purge the tag %s", stalePrefix+tag) + s.logger.Debugf("Purge the tag %s", stalePrefix+tag) s.Storage.Delete(stalePrefix + tag) } return strings.Split(toInvalidate.(string), souinStorageSeparator) diff --git a/plugins/tyk/override/middleware/middleware.go b/plugins/tyk/override/middleware/middleware.go index 3fc69a742..e6a66e987 100644 --- a/plugins/tyk/override/middleware/middleware.go +++ b/plugins/tyk/override/middleware/middleware.go @@ -282,7 +282,7 @@ func (s *SouinBaseHandler) ServeHTTP(rw http.ResponseWriter, baseRq *http.Reques fresh, stale = currentStorer.GetMultiLevel(finalKey, rq, validator) if fresh != nil || stale != nil { - s.Configuration.GetLogger().Sugar().Debugf("Found at least one valid response in the %s storage", currentStorer.Name()) + s.Configuration.GetLogger().Debugf("Found at least one valid response in the %s storage", currentStorer.Name()) break } } diff --git a/plugins/tyk/override/storage/abstractProvider.go b/plugins/tyk/override/storage/abstractProvider.go index c1a3bdd3d..bf0f7e160 100644 --- a/plugins/tyk/override/storage/abstractProvider.go +++ b/plugins/tyk/override/storage/abstractProvider.go @@ -100,11 +100,11 @@ func mappingElection(provider types.Storer, item []byte, req *http.Request, vali response := provider.Get(keyName) if response != nil { if resultFresh, e = http.ReadResponse(bufio.NewReader(bytes.NewBuffer(response)), req); e != nil { - logger.Sugar().Errorf("An error occured while reading response for the key %s: %v", string(keyName), e) + logger.Errorf("An error occured while reading response for the key %s: %v", string(keyName), e) return } - logger.Sugar().Debugf("The stored key %s matched the current iteration key ETag %+v", string(keyName), validator) + logger.Debugf("The stored key %s matched the current iteration key ETag %+v", string(keyName), validator) return } } @@ -114,15 +114,15 @@ func mappingElection(provider types.Storer, item []byte, req *http.Request, vali response := provider.Get(keyName) if response != nil { if resultStale, e = http.ReadResponse(bufio.NewReader(bytes.NewBuffer(response)), req); e != nil { - logger.Sugar().Errorf("An error occured while reading response for the key %s: %v", string(keyName), e) + logger.Errorf("An error occured while reading response for the key %s: %v", string(keyName), e) return } - logger.Sugar().Debugf("The stored key %s matched the current iteration key ETag %+v as stale", string(keyName), validator) + logger.Debugf("The stored key %s matched the current iteration key ETag %+v as stale", string(keyName), validator) } } } else { - logger.Sugar().Debugf("The stored key %s didn't match the current iteration key ETag %+v", string(keyName), validator) + logger.Debugf("The stored key %s didn't match the current iteration key ETag %+v", string(keyName), validator) } } @@ -136,7 +136,7 @@ func mappingUpdater(key string, item []byte, logger *zap.Logger, now, freshTime, } else { e = gob.NewDecoder(bytes.NewBuffer(item)).Decode(&mapping) if e != nil { - logger.Sugar().Errorf("Impossible to decode the key %s, %v", key, e) + logger.Errorf("Impossible to decode the key %s, %v", key, e) return nil, e } } @@ -157,7 +157,7 @@ func mappingUpdater(key string, item []byte, logger *zap.Logger, now, freshTime, buf := new(bytes.Buffer) e = gob.NewEncoder(buf).Encode(mapping) if e != nil { - logger.Sugar().Errorf("Impossible to encode the mapping value for the key %s, %v", key, e) + logger.Errorf("Impossible to encode the mapping value for the key %s, %v", key, e) return nil, e } diff --git a/plugins/tyk/override/storage/cacheProvider.go b/plugins/tyk/override/storage/cacheProvider.go index 7734be003..88ee05914 100644 --- a/plugins/tyk/override/storage/cacheProvider.go +++ b/plugins/tyk/override/storage/cacheProvider.go @@ -162,7 +162,7 @@ func (provider *Cache) SetMultiLevel(baseKey, variedKey string, value []byte, va return e } - provider.logger.Sugar().Debugf("Store the new mapping for the key %s in storage", variedKey) + provider.logger.Debugf("Store the new mapping for the key %s in storage", variedKey) provider.Cache.Set(mappingKey, val, -1) return nil diff --git a/plugins/webgo/go.mod b/plugins/webgo/go.mod index 790d4c185..7ab21c1bb 100644 --- a/plugins/webgo/go.mod +++ b/plugins/webgo/go.mod @@ -38,7 +38,7 @@ require ( github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect github.com/darkweak/go-esi v0.0.6 // indirect github.com/darkweak/storages/badger v0.0.4 // indirect - github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 // indirect + github.com/darkweak/storages/core v0.0.6 // indirect github.com/darkweak/storages/etcd v0.0.4 // indirect github.com/darkweak/storages/nats v0.0.5 // indirect github.com/darkweak/storages/nuts v0.0.4 // indirect diff --git a/plugins/webgo/go.sum b/plugins/webgo/go.sum index 1792ff455..680d5919e 100644 --- a/plugins/webgo/go.sum +++ b/plugins/webgo/go.sum @@ -143,8 +143,8 @@ github.com/darkweak/go-esi v0.0.6 h1:eVHCJfqrZwOHPfRK7JTlSYG9F8lfpX/d4lz/41RQkd8 github.com/darkweak/go-esi v0.0.6/go.mod h1:IJSayeQZDUh5R5ayyDC3wUEBykti12aUa0eUxZZeodk= github.com/darkweak/storages/badger v0.0.4 h1:m6VSwrsKQ17+7/ZDOiEuQT8IHmr+WW5Y6ZfgTKFxIxM= github.com/darkweak/storages/badger v0.0.4/go.mod h1:v/U7iLG6qYT0qp9IKLmr/m6doBaQNcSJxvB3UlfsIbU= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881 h1:WsLHVD+WDxYh5SuZzpZXnhnDruZVrrvnYsHw08izs1s= -github.com/darkweak/storages/core v0.0.6-0.20240731100357-2a602dafc881/go.mod h1:mWN/TO5aLYt2jK6EweZ4JFXXPYmYwYFUQ4YBuIlBkpc= +github.com/darkweak/storages/core v0.0.6 h1:Yj0Z98HsC9Kd9cmjAXLagx6aU7AtXuL++RV2W+FSjc0= +github.com/darkweak/storages/core v0.0.6/go.mod h1:ajTpB9IFLRIRY0EEFLjM5vtsrcNTh+TJK9yRxgG5/wY= github.com/darkweak/storages/etcd v0.0.4 h1:IdghlFAP+uv2sErKgtDfccPWQmVrlqk3GlvFVuRvdz4= github.com/darkweak/storages/etcd v0.0.4/go.mod h1:kBMLaPQDZBrxPQqS1u+oooNwWfP6Y3qUHnNCLnfLcIA= github.com/darkweak/storages/nats v0.0.5 h1:IKTx6teTIFSMvv7kRXz1jbSDeaJjE0BmB0GKe1XW3Mw= diff --git a/tests/mock.go b/tests/mock.go index db3a0d5d7..d88e6f532 100644 --- a/tests/mock.go +++ b/tests/mock.go @@ -6,6 +6,7 @@ import ( "os" "github.com/darkweak/souin/configurationtypes" + "github.com/darkweak/storages/core" "go.uber.org/zap" "go.uber.org/zap/zapcore" "gopkg.in/yaml.v3" @@ -25,7 +26,7 @@ type testConfiguration struct { SSLProviders []string `yaml:"ssl_providers"` URLs map[string]configurationtypes.URL `yaml:"urls"` LogLevel string `yaml:"log_level"` - logger *zap.Logger + logger core.Logger PluginName string Ykeys map[string]configurationtypes.SurrogateKeys `yaml:"ykeys"` SurrogateKeys map[string]configurationtypes.SurrogateKeys `yaml:"surrogate_keys"` @@ -67,12 +68,12 @@ func (c *testConfiguration) GetLogLevel() string { } // GetLogger get the logger -func (c *testConfiguration) GetLogger() *zap.Logger { +func (c *testConfiguration) GetLogger() core.Logger { return c.logger } // SetLogger set the logger -func (c *testConfiguration) SetLogger(l *zap.Logger) { +func (c *testConfiguration) SetLogger(l core.Logger) { c.logger = l } @@ -516,7 +517,7 @@ func MockConfiguration(configurationToLoad func() string) *testConfiguration { }, } logger, _ := cfg.Build() - config.SetLogger(logger) + config.SetLogger(logger.Sugar()) return &config }