Skip to content

Commit 0c89772

Browse files
authored
Supports caddy App (#72)
* Supports caddy App * Fix linter * Remove unused code * Remove unused usagePool
1 parent 4d633c7 commit 0c89772

File tree

6 files changed

+140
-63
lines changed

6 files changed

+140
-63
lines changed

plugins/caddy/Caddyfile

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
{
2-
experimental_http3
32
order souin_cache before rewrite
43
souin_cache {
54
headers Content-Type Authorization
65
log_level info
7-
ttl 1000
6+
ttl 1000s
87
}
98
}
109

@@ -13,7 +12,6 @@
1312
@match path /test1*
1413
@match2 path /test2*
1514
@matchdefault path /default
16-
@matchall path /*
1715

1816
souin_cache @match {
1917
ttl 30s

plugins/caddy/app.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package caddy
2+
3+
import (
4+
"github.com/caddyserver/caddy/v2"
5+
)
6+
7+
type SouinApp struct {
8+
*DefaultCache
9+
LogLevel string `json:"log_level,omitempty"`
10+
}
11+
12+
func init() {
13+
caddy.RegisterModule(SouinApp{})
14+
}
15+
16+
// Provision implements caddy.Provisioner
17+
func (s *SouinApp) Provision(_ caddy.Context) error {
18+
return nil
19+
}
20+
21+
// Start will start the App
22+
func (s SouinApp) Start() error {
23+
if s.DefaultCache != nil && s.DefaultCache.TTL == "" {
24+
return new(defaultCacheError)
25+
}
26+
return nil
27+
}
28+
29+
// Stop will stop the App
30+
func (s SouinApp) Stop() error {
31+
return nil
32+
}
33+
34+
// CaddyModule implements caddy.ModuleInfo
35+
func (a SouinApp) CaddyModule() caddy.ModuleInfo {
36+
return caddy.ModuleInfo{
37+
ID: moduleName,
38+
New: func() caddy.Module { return new(SouinApp) },
39+
}
40+
}
41+
42+
var (
43+
_ caddy.App = (*SouinApp)(nil)
44+
_ caddy.Module = (*SouinApp)(nil)
45+
_ caddy.Provisioner = (*SouinApp)(nil)
46+
)

plugins/caddy/configuration.json

+30-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
{
22
"apps": {
3+
"souin_cache": {
4+
"headers": [
5+
"Content-Type",
6+
"Authorization"
7+
],
8+
"log_level": "info",
9+
"olric": {
10+
"url": "olric:3320"
11+
},
12+
"ttl": "1000s"
13+
},
314
"http": {
415
"servers": {
516
"": {
@@ -30,19 +41,31 @@
3041
"Content-Type": ["*"]
3142
},
3243
"path": [
33-
"*"
44+
"/b*"
3445
]
3546
}
3647
],
3748
"handle": [
3849
{
3950
"handler": "souin_cache",
40-
"headers": ["Content-Type", "Authorization"],
41-
"log_level": "info",
42-
"ttl": "1000",
43-
"olric": {
44-
"url": "olric:3320"
45-
}
51+
"headers": []
52+
}
53+
]
54+
},
55+
{
56+
"match": [
57+
{
58+
"header": {
59+
"Content-Type": ["*"]
60+
},
61+
"path": [
62+
"*"
63+
]
64+
}
65+
],
66+
"handle": [
67+
{
68+
"handler": "souin_cache"
4669
}
4770
]
4871
}

plugins/caddy/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/darkweak/souin/plugins/caddy
33
go 1.15
44

55
require (
6-
github.com/caddyserver/caddy/v2 v2.3.0
6+
github.com/caddyserver/caddy/v2 v2.4.0-beta.2
77
github.com/darkweak/souin v1.5.0
88
github.com/dgraph-io/ristretto v0.0.3 // indirect
99
github.com/pquerna/cachecontrol v0.1.0 // indirect

plugins/caddy/go.sum

+7
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,10 @@ github.com/buraksezer/pool v3.0.0+incompatible h1:MXcI3YkBnElnbJ8ZPIYqa0dia3qqHD
103103
github.com/buraksezer/pool v3.0.0+incompatible/go.mod h1:zxtCo9iWArq14Yd5u1nvAr95d4wyp0YSkKGOF89BGr8=
104104
github.com/caddyserver/caddy/v2 v2.3.0 h1:fnrqJLa3G5vfxcxmOH/+kJOcunPLhSBnjgIvjXV/QTA=
105105
github.com/caddyserver/caddy/v2 v2.3.0/go.mod h1:0NN57HuypYfGgYdesiERpCtgwWpQzEZQFMKYKZjGkOw=
106+
github.com/caddyserver/caddy/v2 v2.4.0-beta.2/go.mod h1:R8rCCbfUmWSjHcQ/AS357+kPUWoMISrlo7pdO15N3tI=
106107
github.com/caddyserver/certmagic v0.12.1-0.20201215190346-201f83a06067 h1:gpjCX6/8hHRgVXxy1v2AQdoAX6XRXIA8fBUZtEpnVg0=
107108
github.com/caddyserver/certmagic v0.12.1-0.20201215190346-201f83a06067/go.mod h1:tr26xh+9fY5dN0J6IPAlMj07qpog22PJKa7Nw7j835U=
109+
github.com/caddyserver/certmagic v0.13.0/go.mod h1:dNOzF4iOB7H9E51xTooMB90vs+2XNVtpnx0liQNsQY4=
108110
github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
109111
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
110112
github.com/census-instrumentation/opencensus-proto v0.2.0/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
@@ -311,6 +313,7 @@ github.com/google/trillian-examples v0.0.0-20190603134952-4e75ba15216c/go.mod h1
311313
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
312314
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
313315
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
316+
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
314317
github.com/googleapis/gax-go v2.0.0+incompatible h1:j0GKcs05QVmm7yesiZq2+9cxHkNK9YM6zKx4D2qucQU=
315318
github.com/googleapis/gax-go v2.0.0+incompatible/go.mod h1:SFVmujtThgffbyetf+mdk2eWhX2bMyUtNHzFKcPA9HY=
316319
github.com/googleapis/gax-go/v2 v2.0.3/go.mod h1:LLvjysVCY1JZeum8Z6l8qUty8fiNwE08qbEPm1M08qg=
@@ -433,6 +436,7 @@ github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
433436
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
434437
github.com/libdns/libdns v0.1.0 h1:0ctCOrVJsVzj53mop1angHp/pE3hmAhP7KiHvR0HD04=
435438
github.com/libdns/libdns v0.1.0/go.mod h1:yQCXzk1lEZmmCPa857bnk4TsOiqYasqpyOEeSObbb40=
439+
github.com/libdns/libdns v0.2.0/go.mod h1:yQCXzk1lEZmmCPa857bnk4TsOiqYasqpyOEeSObbb40=
436440
github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM=
437441
github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4=
438442
github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
@@ -473,6 +477,7 @@ github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0j
473477
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
474478
github.com/mholt/acmez v0.1.1 h1:KQODCqk+hBn3O7qfCRPj6L96uG65T5BSS95FKNEqtdA=
475479
github.com/mholt/acmez v0.1.1/go.mod h1:8qnn8QA/Ewx8E3ZSsmscqsIjhhpxuy9vqdgbX2ceceM=
480+
github.com/mholt/acmez v0.1.3/go.mod h1:8qnn8QA/Ewx8E3ZSsmscqsIjhhpxuy9vqdgbX2ceceM=
476481
github.com/microcosm-cc/bluemonday v1.0.1/go.mod h1:hsXNsILzKxV+sX77C5b8FSuKF00vh2OMYv+xgHpAMF4=
477482
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
478483
github.com/miekg/dns v1.1.30 h1:Qww6FseFn8PRfw07jueqIXqodm0JKiiKuK0DeXSqfyo=
@@ -956,9 +961,11 @@ golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7w
956961
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
957962
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
958963
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
964+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
959965
golang.org/x/sys v0.0.0-20201214210602-f9fddec55a1e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
960966
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2 h1:46ULzRKLh1CwgRq2dC5SlBzEqqNCi8rreOZnNrbqcIY=
961967
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
968+
golang.org/x/term v0.0.0-20210317153231-de623e64d2a6/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
962969
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
963970
golang.org/x/text v0.0.0-20170915090833-1cbadb444a80/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
964971
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

plugins/caddy/main.go

+55-52
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"github.com/caddyserver/caddy/v2"
7+
"github.com/caddyserver/caddy/v2/caddyconfig"
78
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
89
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
910
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
@@ -15,7 +16,6 @@ import (
1516
"io"
1617
"net/http"
1718
"net/http/httptest"
18-
"strconv"
1919
"sync"
2020
)
2121

@@ -31,29 +31,20 @@ func init() {
3131
httpcaddyfile.RegisterHandlerDirective(moduleName, parseCaddyfileHandlerDirective)
3232
}
3333

34-
var (
35-
staticConfig Configuration
36-
appCounter = 0
37-
appConfigs *caddy.UsagePool
38-
)
39-
4034
// SouinCaddyPlugin declaration.
4135
type SouinCaddyPlugin struct {
4236
plugins.SouinBasePlugin
4337
Configuration *Configuration
4438
logger *zap.Logger
4539
LogLevel string `json:"log_level,omitempty"`
4640
bufPool sync.Pool
47-
Headers []string
48-
Olric configurationtypes.CacheProvider
49-
TTL string
41+
Headers []string `json:"headers,omitempty"`
42+
Olric configurationtypes.CacheProvider `json:"olric,omitempty"`
43+
TTL string `json:"ttl,omitempty"`
5044
}
5145

5246
// CaddyModule returns the Caddy module information.
5347
func (SouinCaddyPlugin) CaddyModule() caddy.ModuleInfo {
54-
if appConfigs == nil {
55-
appConfigs = caddy.NewUsagePool()
56-
}
5748
return caddy.ModuleInfo{
5849
ID: moduleID,
5950
New: func() caddy.Module { return new(SouinCaddyPlugin) },
@@ -67,7 +58,7 @@ type getterContext struct {
6758
}
6859

6960
// ServeHTTP implements caddyhttp.MiddlewareHandler.
70-
func (s SouinCaddyPlugin) ServeHTTP(rw http.ResponseWriter, req *http.Request, next caddyhttp.Handler) error {
61+
func (s *SouinCaddyPlugin) ServeHTTP(rw http.ResponseWriter, req *http.Request, next caddyhttp.Handler) error {
7162
getterCtx := getterContext{rw, req, next}
7263
ctx := context.WithValue(req.Context(), getterContextCtxKey, getterCtx)
7364
req = req.WithContext(ctx)
@@ -122,39 +113,66 @@ func (s *SouinCaddyPlugin) Validate() error {
122113
return nil
123114
}
124115

116+
// FromApp to initialize configuration from App structure.
117+
func (s *SouinCaddyPlugin) FromApp(app *SouinApp) error {
118+
if s.Configuration == nil {
119+
s.Configuration = &Configuration{
120+
URLs: make(map[string]configurationtypes.URL),
121+
}
122+
}
123+
124+
if app.DefaultCache == nil {
125+
return nil
126+
}
127+
128+
if s.Configuration.DefaultCache == nil {
129+
s.Configuration.DefaultCache = &DefaultCache{
130+
Headers: app.Headers,
131+
TTL: app.TTL,
132+
}
133+
} else {
134+
dc := s.Configuration.DefaultCache
135+
appDc := app.DefaultCache
136+
if dc.Headers == nil {
137+
s.Configuration.DefaultCache.Headers = appDc.Headers
138+
}
139+
if dc.TTL == "" {
140+
s.Configuration.DefaultCache.TTL = appDc.TTL
141+
}
142+
if dc.Olric.URL == "" {
143+
s.Configuration.DefaultCache.Olric = appDc.Olric
144+
}
145+
}
146+
147+
return nil
148+
}
149+
125150
// Provision to do the provisioning part.
126151
func (s *SouinCaddyPlugin) Provision(ctx caddy.Context) error {
127152
s.logger = ctx.Logger(s)
153+
128154
if err := s.configurationPropertyMapper(); err != nil {
129155
return err
130156
}
131157

158+
app, _ := ctx.App(moduleName)
159+
160+
if err := s.FromApp(app.(*SouinApp)); err != nil {
161+
return err
162+
}
163+
132164
s.bufPool = sync.Pool{
133165
New: func() interface{} {
134166
return new(bytes.Buffer)
135167
},
136168
}
137-
if s.Configuration == nil {
138-
c, _, _ := appConfigs.LoadOrNew("counter", nil)
139-
if c != nil {
140-
counter := c.(int)
141-
config, _, _ := appConfigs.LoadOrNew(appCounter - counter, nil)
142-
s.Configuration, _ = config.(*Configuration)
143-
_, _ = appConfigs.Delete("counter")
144-
counter--
145-
appConfigs.LoadOrStore("counter", counter)
146-
} else {
147-
sc := staticConfig
148-
s.Configuration = &sc
149-
}
150-
}
151169
s.Retriever = plugins.DefaultSouinPluginInitializerFromConfiguration(s.Configuration)
152170
s.RequestCoalescing = coalescing.Initialize()
153171
return nil
154172
}
155173

156-
func parseCaddyfileGlobalOption(h *caddyfile.Dispenser) (interface{}, error) {
157-
var souin SouinCaddyPlugin
174+
func parseCaddyfileGlobalOption(h *caddyfile.Dispenser, _ interface{}) (interface{}, error) {
175+
souinApp := new(SouinApp)
158176
cfg := &Configuration{
159177
DefaultCache: &DefaultCache{
160178
Distributed: false,
@@ -167,10 +185,6 @@ func parseCaddyfileGlobalOption(h *caddyfile.Dispenser) (interface{}, error) {
167185
for nesting := h.Nesting(); h.NextBlock(nesting); {
168186
rootOption := h.Val()
169187
switch rootOption {
170-
case "distributed":
171-
args := h.RemainingArgs()
172-
distributed, _ := strconv.ParseBool(args[0])
173-
cfg.DefaultCache.Distributed = distributed
174188
case "headers":
175189
args := h.RemainingArgs()
176190
cfg.DefaultCache.Headers = append(cfg.DefaultCache.Headers, args...)
@@ -197,24 +211,18 @@ func parseCaddyfileGlobalOption(h *caddyfile.Dispenser) (interface{}, error) {
197211
}
198212
}
199213

200-
souin.Configuration = cfg
201-
staticConfig = *cfg
202-
return nil, nil
214+
souinApp.DefaultCache = cfg.DefaultCache
215+
216+
return httpcaddyfile.App{
217+
Name: moduleName,
218+
Value: caddyconfig.JSON(souinApp, nil),
219+
}, nil
203220
}
204221

205222
func parseCaddyfileHandlerDirective(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
206-
dc := DefaultCache{
207-
Distributed: staticConfig.DefaultCache.Distributed,
208-
Headers: staticConfig.DefaultCache.Headers,
209-
Olric: staticConfig.DefaultCache.Olric,
210-
Regex: staticConfig.DefaultCache.Regex,
211-
TTL: staticConfig.DefaultCache.TTL,
212-
}
223+
dc := DefaultCache{}
213224
sc := Configuration{
214225
DefaultCache: &dc,
215-
URLs: staticConfig.URLs,
216-
LogLevel: staticConfig.LogLevel,
217-
logger: staticConfig.logger,
218226
}
219227

220228
for h.Next() {
@@ -227,11 +235,6 @@ func parseCaddyfileHandlerDirective(h httpcaddyfile.Helper) (caddyhttp.Middlewar
227235
}
228236
}
229237

230-
appConfigs.LoadOrStore(appCounter, &sc)
231-
_, _ = appConfigs.Delete("counter")
232-
appConfigs.LoadOrStore("counter", appCounter)
233-
appCounter++
234-
235238
return &SouinCaddyPlugin{
236239
Configuration: &sc,
237240
}, nil

0 commit comments

Comments
 (0)