Skip to content

Commit 3c8632e

Browse files
authored
feat(dependencies): bump to Souin v1.7.7 (#129)
* feat(dependencies): bump to Souin v1.7.7 * feat(deps): bump golangci-lint CI version
1 parent 507cbf6 commit 3c8632e

File tree

9 files changed

+841
-279
lines changed

9 files changed

+841
-279
lines changed

.github/workflows/ci.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ jobs:
1717
# The Windows build currently fail because of https://github.com/golang/go/issues/40795, and because xcaddy isn't compatible with the known workaround
1818
#os: [ ubuntu-latest, macos-latest, windows-latest ]
1919
os: [ ubuntu-latest, macos-latest ]
20-
go: [ '1.21' ]
20+
go: [ '1.24' ]
2121

2222
runs-on: ${{ matrix.os }}
2323

2424
steps:
2525
- name: Install Go
26-
uses: actions/setup-go@v4
26+
uses: actions/setup-go@v5
2727
with:
2828
go-version: ${{ matrix.go }}
2929

3030
- name: Checkout code
31-
uses: actions/checkout@v3
31+
uses: actions/checkout@v4
3232

3333
- name: Print Go version and environment
3434
id: vars
@@ -44,7 +44,7 @@ jobs:
4444
echo "::set-output name=go_cache::$(go env GOCACHE)"
4545
4646
- name: Cache the build cache
47-
uses: actions/cache@v2
47+
uses: actions/cache@v4
4848
with:
4949
path: ${{ steps.vars.outputs.go_cache }}
5050
key: ${{ runner.os }}-${{ matrix.go }}-go-ci-${{ hashFiles('**/go.sum') }}
@@ -71,11 +71,11 @@ jobs:
7171
runs-on: ubuntu-latest
7272

7373
steps:
74-
- uses: actions/checkout@v3
75-
- uses: actions/setup-go@v4
74+
- uses: actions/checkout@v4
75+
- uses: actions/setup-go@v5
7676
with:
77-
go-version: '1.21'
77+
go-version: '1.24'
7878
- name: golangci-lint
79-
uses: golangci/golangci-lint-action@v3
79+
uses: golangci/golangci-lint-action@v8
8080
with:
8181
args: --timeout 5m

admin.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package httpcache
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"strings"
7+
"time"
8+
9+
"github.com/caddyserver/caddy/v2"
10+
"github.com/darkweak/souin/configurationtypes"
11+
"github.com/darkweak/souin/pkg/api"
12+
13+
"github.com/darkweak/storages/core"
14+
)
15+
16+
func init() {
17+
caddy.RegisterModule(new(adminAPI))
18+
}
19+
20+
// adminAPI is a module that serves PKI endpoints to retrieve
21+
// information about the CAs being managed by Caddy.
22+
type adminAPI struct {
23+
ctx caddy.Context
24+
logger core.Logger
25+
app *SouinApp
26+
InternalEndpointHandlers *api.MapHandler
27+
}
28+
29+
// CaddyModule returns the Caddy module information.
30+
func (adminAPI) CaddyModule() caddy.ModuleInfo {
31+
return caddy.ModuleInfo{
32+
ID: "admin.api.souin",
33+
New: func() caddy.Module { return new(adminAPI) },
34+
}
35+
}
36+
37+
func (a *adminAPI) handleAPIEndpoints(writer http.ResponseWriter, request *http.Request) error {
38+
if a.InternalEndpointHandlers != nil {
39+
for k, handler := range *a.InternalEndpointHandlers.Handlers {
40+
if strings.Contains(request.RequestURI, k) {
41+
handler(writer, request)
42+
return nil
43+
}
44+
}
45+
}
46+
47+
return caddy.APIError{
48+
HTTPStatus: http.StatusNotFound,
49+
Err: fmt.Errorf("resource not found: %v", request.URL.Path),
50+
}
51+
}
52+
53+
// Provision sets up the adminAPI module.
54+
func (a *adminAPI) Provision(ctx caddy.Context) error {
55+
a.ctx = ctx
56+
a.logger = ctx.Logger(a).Sugar()
57+
58+
app, err := ctx.App(moduleName)
59+
if err != nil {
60+
return err
61+
}
62+
63+
a.app = app.(*SouinApp)
64+
config := Configuration{
65+
API: a.app.API,
66+
DefaultCache: DefaultCache{
67+
TTL: configurationtypes.Duration{
68+
Duration: 120 * time.Second,
69+
},
70+
},
71+
}
72+
a.InternalEndpointHandlers = api.GenerateHandlerMap(&config, a.app.Storers, a.app.SurrogateStorage)
73+
74+
return nil
75+
}
76+
77+
// Routes returns the admin routes.
78+
func (a *adminAPI) Routes() []caddy.AdminRoute {
79+
return []caddy.AdminRoute{
80+
{
81+
Pattern: "/{params...}",
82+
Handler: caddy.AdminHandlerFunc(a.handleAPIEndpoints),
83+
},
84+
}
85+
}

app.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package httpcache
22

33
import (
4-
"errors"
5-
64
"github.com/caddyserver/caddy/v2"
75
"github.com/darkweak/souin/configurationtypes"
86
"github.com/darkweak/souin/pkg/storage/types"
@@ -15,8 +13,10 @@ type SouinApp struct {
1513
DefaultCache
1614
// The provider to use.
1715
Storers []types.Storer
18-
// Surrogate storage to support th econfiguration reload without surrogate-key data loss.
16+
// Surrogate storage to support the configuration reload without surrogate-key data loss.
1917
SurrogateStorage providers.SurrogateInterface
18+
// SurrogateKeyDisabled opt-out the Surrogate key system.
19+
SurrogateKeyDisabled bool
2020
// Cache-key tweaking.
2121
CacheKeys configurationtypes.CacheKeys `json:"cache_keys,omitempty"`
2222
// API endpoints enablers.
@@ -29,14 +29,17 @@ func init() {
2929
caddy.RegisterModule(SouinApp{})
3030
}
3131

32+
// Provision implements caddy.Provisioner
33+
func (s SouinApp) Provision(_ caddy.Context) error {
34+
return nil
35+
}
36+
3237
// Start will start the App
3338
func (s SouinApp) Start() error {
3439
core.ResetRegisteredStorages()
3540
_, _ = up.Delete(stored_providers_key)
3641
_, _ = up.LoadOrStore(stored_providers_key, newStorageProvider())
37-
if s.DefaultCache.GetTTL() == 0 {
38-
return errors.New("Invalid/Incomplete default cache declaration")
39-
}
42+
4043
return nil
4144
}
4245

@@ -54,6 +57,7 @@ func (s SouinApp) CaddyModule() caddy.ModuleInfo {
5457
}
5558

5659
var (
57-
_ caddy.App = (*SouinApp)(nil)
58-
_ caddy.Module = (*SouinApp)(nil)
60+
_ caddy.App = (*SouinApp)(nil)
61+
_ caddy.Module = (*SouinApp)(nil)
62+
_ caddy.Provisioner = (*SouinApp)(nil)
5963
)

cleaner.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ func newStorageProvider() *storage_providers {
2121
}
2222

2323
func (s *storage_providers) Add(key interface{}) {
24-
s.RWMutex.Lock()
25-
defer s.RWMutex.Unlock()
24+
s.Lock()
25+
defer s.Unlock()
2626

2727
s.list[key] = true
2828
}

configuration.go

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
type DefaultCache struct {
1616
// Allowed HTTP verbs to be cached by the system.
1717
AllowedHTTPVerbs []string `json:"allowed_http_verbs"`
18+
// Allowed additional status code to be cached by the system.
19+
AllowedAdditionalStatusCodes []int `json:"allowed_additional_status_codes"`
1820
// Badger provider configuration.
1921
Badger configurationtypes.CacheProvider `json:"badger"`
2022
// The cache name to use in the Cache-Status response header.
@@ -65,6 +67,11 @@ func (d *DefaultCache) GetAllowedHTTPVerbs() []string {
6567
return d.AllowedHTTPVerbs
6668
}
6769

70+
// GetAllowedAdditionalStatusCodes returns the allowed verbs to cache
71+
func (d *DefaultCache) GetAllowedAdditionalStatusCodes() []int {
72+
return d.AllowedAdditionalStatusCodes
73+
}
74+
6875
// GetBadger returns the Badger configuration
6976
func (d *DefaultCache) GetBadger() configurationtypes.CacheProvider {
7077
return d.Badger
@@ -189,15 +196,17 @@ type Configuration struct {
189196
LogLevel string
190197
// SurrogateKeys contains the surrogate keys to use with a predefined mapping
191198
SurrogateKeys map[string]configurationtypes.SurrogateKeys
192-
logger core.Logger
199+
// SurrogateKeyDisabled disables the surrogate keys system
200+
SurrogateKeyDisabled bool
201+
logger core.Logger
193202
}
194203

195204
// GetUrls get the urls list in the configuration
196205
func (c *Configuration) GetUrls() map[string]configurationtypes.URL {
197206
return c.URLs
198207
}
199208

200-
// GetDefaultCache get the default cache
209+
// GetPluginName get the plugin name
201210
func (c *Configuration) GetPluginName() string {
202211
return "caddy"
203212
}
@@ -237,6 +246,11 @@ func (c *Configuration) GetSurrogateKeys() map[string]configurationtypes.Surroga
237246
return nil
238247
}
239248

249+
// IsSurrogateDisabled disables the surrogate storage
250+
func (c *Configuration) IsSurrogateDisabled() bool {
251+
return c.SurrogateKeyDisabled
252+
}
253+
240254
// GetCacheKeys get the cache keys rules to override
241255
func (c *Configuration) GetCacheKeys() configurationtypes.CacheKeys {
242256
return c.CacheKeys
@@ -271,6 +285,12 @@ func parseBadgerConfiguration(c map[string]interface{}) map[string]interface{} {
271285
c[k] = v
272286
case "SyncWrites", "ReadOnly", "InMemory", "MetricsEnabled", "CompactL0OnClose", "LmaxCompaction", "VerifyValueChecksum", "BypassLockGuard", "DetectConflicts":
273287
c[k] = true
288+
if v != nil {
289+
val, ok := v.(string)
290+
if ok {
291+
c[k], _ = strconv.ParseBool(val)
292+
}
293+
}
274294
case "NumVersionsToKeep", "NumGoroutines", "MemTableSize", "BaseTableSize", "BaseLevelSize", "LevelSizeMultiplier", "TableSizeMultiplier", "MaxLevels", "ValueThreshold", "NumMemtables", "BlockSize", "BlockCacheSize", "IndexCacheSize", "NumLevelZeroTables", "NumLevelZeroTablesStall", "ValueLogFileSize", "NumCompactors", "ZSTDCompressionLevel", "ChecksumVerificationMode", "NamespaceOffset":
275295
c[k], _ = strconv.Atoi(v.(string))
276296
case "Compression", "ValueLogMaxEntries":
@@ -301,11 +321,12 @@ func parseRedisConfiguration(c map[string]interface{}) map[string]interface{} {
301321
case "SendToReplicas", "ShuffleInit", "ClientNoTouch", "DisableRetry", "DisableCache", "AlwaysPipelining", "AlwaysRESP2", "ForceSingleClient", "ReplicaOnly", "ClientNoEvict", "ContextTimeoutEnabled", "PoolFIFO", "ReadOnly", "RouteByLatency", "RouteRandomly", "DisableIndentity":
302322
c[k] = true
303323
case "SelectDB", "CacheSizeEachConn", "RingScaleEachConn", "ReadBufferEachConn", "WriteBufferEachConn", "BlockingPoolSize", "PipelineMultiplex", "DB", "Protocol", "MaxRetries", "PoolSize", "MinIdleConns", "MaxIdleConns", "MaxActiveConns", "MaxRedirects":
304-
if v == false {
324+
switch v {
325+
case false:
305326
c[k] = 0
306-
} else if v == true {
327+
case true:
307328
c[k] = 1
308-
} else {
329+
default:
309330
c[k], _ = strconv.Atoi(v.(string))
310331
}
311332
case "ConnWriteTimeout", "MaxFlushDelay", "MinRetryBackoff", "MaxRetryBackoff", "DialTimeout", "ReadTimeout", "WriteTimeout", "PoolTimeout", "ConnMaxIdleTime", "ConnMaxLifetime":
@@ -342,11 +363,21 @@ func parseSimpleFSConfiguration(c map[string]interface{}) map[string]interface{}
342363
case "path":
343364
c[k] = v
344365
case "size":
345-
if v == false {
366+
switch v {
367+
case false:
346368
c[k] = 0
347-
} else if v == true {
369+
case true:
348370
c[k] = 1
349-
} else {
371+
default:
372+
c[k], _ = strconv.Atoi(v.(string))
373+
}
374+
case "directory_size":
375+
switch v {
376+
case false:
377+
c[k] = 0
378+
case true:
379+
c[k] = 1
380+
default:
350381
c[k], _ = strconv.Atoi(v.(string))
351382
}
352383
}
@@ -364,6 +395,17 @@ func parseConfiguration(cfg *Configuration, h *caddyfile.Dispenser, isGlobal boo
364395
allowed := cfg.DefaultCache.AllowedHTTPVerbs
365396
allowed = append(allowed, h.RemainingArgs()...)
366397
cfg.DefaultCache.AllowedHTTPVerbs = allowed
398+
case "allowed_additional_status_codes":
399+
allowed := cfg.DefaultCache.AllowedAdditionalStatusCodes
400+
additional := h.RemainingArgs()
401+
codes := make([]int, 0)
402+
for _, code := range additional {
403+
if c, err := strconv.Atoi(code); err == nil {
404+
codes = append(codes, c)
405+
}
406+
}
407+
allowed = append(allowed, codes...)
408+
cfg.DefaultCache.AllowedAdditionalStatusCodes = allowed
367409
case "api":
368410
if !isGlobal {
369411
return h.Err("'api' block must be global")
@@ -453,6 +495,8 @@ func parseConfiguration(cfg *Configuration, h *caddyfile.Dispenser, isGlobal boo
453495
ck.DisableQuery = true
454496
case "disable_scheme":
455497
ck.DisableScheme = true
498+
case "disable_vary":
499+
ck.DisableVary = true
456500
case "template":
457501
ck.Template = h.RemainingArgs()[0]
458502
case "hash":
@@ -547,6 +591,8 @@ func parseConfiguration(cfg *Configuration, h *caddyfile.Dispenser, isGlobal boo
547591
config_key.DisableQuery = true
548592
case "disable_scheme":
549593
config_key.DisableScheme = true
594+
case "disable_vary":
595+
config_key.DisableVary = true
550596
case "template":
551597
config_key.Template = h.RemainingArgs()[0]
552598
case "hash":
@@ -720,6 +766,8 @@ func parseConfiguration(cfg *Configuration, h *caddyfile.Dispenser, isGlobal boo
720766
}
721767
case "disable_coalescing":
722768
cfg.DefaultCache.DisableCoalescing = true
769+
case "disable_surrogate_key":
770+
cfg.SurrogateKeyDisabled = true
723771
default:
724772
return h.Errf("unsupported root directive: %s", rootOption)
725773
}

0 commit comments

Comments
 (0)