Skip to content

Commit

Permalink
fix(caddy): configuration parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
darkweak committed Oct 12, 2023
1 parent 156f609 commit 541dee7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
16 changes: 8 additions & 8 deletions pkg/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,9 @@ func (s *SouinBaseHandler) Store(
}

type singleflightValue struct {
body []byte
body []byte
headers http.Header
code int
code int
}

func (s *SouinBaseHandler) Upstream(
Expand All @@ -294,7 +294,7 @@ func (s *SouinBaseHandler) Upstream(
s.Configuration.GetLogger().Sugar().Debug("Request the upstream server")
prometheus.Increment(prometheus.RequestCounter)
shared := true

sfValue, err, _ := s.singleflightPool.Do(cachedKey, func() (interface{}, error) {
shared = false
if e := next(customWriter, rq); e != nil {
Expand Down Expand Up @@ -323,9 +323,9 @@ func (s *SouinBaseHandler) Upstream(
err := s.Store(customWriter, rq, requestCc, cachedKey)

return singleflightValue{
body: customWriter.Buf.Bytes(),
body: customWriter.Buf.Bytes(),
headers: customWriter.Headers,
code: customWriter.statusCode,
code: customWriter.statusCode,
}, err
}
})
Expand All @@ -349,7 +349,7 @@ func (s *SouinBaseHandler) Revalidate(validator *rfc.Revalidator, next handlerFu
prometheus.Increment(prometheus.RequestRevalidationCounter)

shared := true

sfValue, err, _ := s.singleflightPool.Do(cachedKey, func() (interface{}, error) {
shared = false
err := next(customWriter, rq)
Expand Down Expand Up @@ -384,9 +384,9 @@ func (s *SouinBaseHandler) Revalidate(validator *rfc.Revalidator, next handlerFu
)

return singleflightValue{
body: customWriter.Buf.Bytes(),
body: customWriter.Buf.Bytes(),
headers: customWriter.Headers,
code: customWriter.statusCode,
code: customWriter.statusCode,
}, err
})

Expand Down
50 changes: 26 additions & 24 deletions plugins/caddy/httpcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func init() {
type SouinCaddyMiddleware struct {
*middleware.SouinBaseHandler
logger *zap.Logger
cacheKeys configurationtypes.CacheKeys
Configuration Configuration
// Logger level, fallback on caddy's one when not redefined.
LogLevel string `json:"log_level,omitempty"`
Expand Down Expand Up @@ -86,30 +87,30 @@ func (s *SouinCaddyMiddleware) ServeHTTP(rw http.ResponseWriter, r *http.Request
}

func (s *SouinCaddyMiddleware) configurationPropertyMapper() error {
defaultCache := DefaultCache{
Badger: s.Badger,
Nuts: s.Nuts,
Key: s.Key,
DefaultCacheControl: s.DefaultCacheControl,
CacheName: s.CacheName,
Distributed: s.Olric.URL != "" || s.Olric.Path != "" || s.Olric.Configuration != nil || s.Etcd.Configuration != nil || s.Redis.URL != "" || s.Redis.Configuration != nil,
Headers: s.Headers,
Olric: s.Olric,
Etcd: s.Etcd,
Redis: s.Redis,
Timeout: s.Timeout,
TTL: s.TTL,
Stale: s.Stale,
Storers: s.Storers,
}
// if s.Configuration == nil {
// s.Configuration = Configuration{
// CacheKeys: s.cacheKeys,
// DefaultCache: defaultCache,
// LogLevel: s.LogLevel,
// }
// }
s.Configuration.DefaultCache = defaultCache
if s.Configuration.GetDefaultCache() == nil {
defaultCache := DefaultCache{
Badger: s.Badger,
Nuts: s.Nuts,
Key: s.Key,
DefaultCacheControl: s.DefaultCacheControl,
CacheName: s.CacheName,
Distributed: s.Olric.URL != "" || s.Olric.Path != "" || s.Olric.Configuration != nil || s.Etcd.Configuration != nil || s.Redis.URL != "" || s.Redis.Configuration != nil,
Headers: s.Headers,
Olric: s.Olric,
Etcd: s.Etcd,
Redis: s.Redis,
Timeout: s.Timeout,
TTL: s.TTL,
Stale: s.Stale,
Storers: s.Storers,
}
s.Configuration = Configuration{
CacheKeys: s.cacheKeys,
DefaultCache: defaultCache,
LogLevel: s.LogLevel,
}
}

return nil
}

Expand Down Expand Up @@ -327,6 +328,7 @@ func (s *SouinCaddyMiddleware) UnmarshalCaddyfile(h *caddyfile.Dispenser) error
s.Configuration = Configuration{
DefaultCache: dc,
}

return parseConfiguration(&s.Configuration, h, false)
}

Expand Down

0 comments on commit 541dee7

Please sign in to comment.