4
4
"bytes"
5
5
"context"
6
6
"github.com/caddyserver/caddy/v2"
7
+ "github.com/caddyserver/caddy/v2/caddyconfig"
7
8
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
8
9
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
9
10
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
@@ -15,7 +16,6 @@ import (
15
16
"io"
16
17
"net/http"
17
18
"net/http/httptest"
18
- "strconv"
19
19
"sync"
20
20
)
21
21
@@ -31,29 +31,20 @@ func init() {
31
31
httpcaddyfile .RegisterHandlerDirective (moduleName , parseCaddyfileHandlerDirective )
32
32
}
33
33
34
- var (
35
- staticConfig Configuration
36
- appCounter = 0
37
- appConfigs * caddy.UsagePool
38
- )
39
-
40
34
// SouinCaddyPlugin declaration.
41
35
type SouinCaddyPlugin struct {
42
36
plugins.SouinBasePlugin
43
37
Configuration * Configuration
44
38
logger * zap.Logger
45
39
LogLevel string `json:"log_level,omitempty"`
46
40
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"`
50
44
}
51
45
52
46
// CaddyModule returns the Caddy module information.
53
47
func (SouinCaddyPlugin ) CaddyModule () caddy.ModuleInfo {
54
- if appConfigs == nil {
55
- appConfigs = caddy .NewUsagePool ()
56
- }
57
48
return caddy.ModuleInfo {
58
49
ID : moduleID ,
59
50
New : func () caddy.Module { return new (SouinCaddyPlugin ) },
@@ -67,7 +58,7 @@ type getterContext struct {
67
58
}
68
59
69
60
// 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 {
71
62
getterCtx := getterContext {rw , req , next }
72
63
ctx := context .WithValue (req .Context (), getterContextCtxKey , getterCtx )
73
64
req = req .WithContext (ctx )
@@ -122,39 +113,66 @@ func (s *SouinCaddyPlugin) Validate() error {
122
113
return nil
123
114
}
124
115
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
+
125
150
// Provision to do the provisioning part.
126
151
func (s * SouinCaddyPlugin ) Provision (ctx caddy.Context ) error {
127
152
s .logger = ctx .Logger (s )
153
+
128
154
if err := s .configurationPropertyMapper (); err != nil {
129
155
return err
130
156
}
131
157
158
+ app , _ := ctx .App (moduleName )
159
+
160
+ if err := s .FromApp (app .(* SouinApp )); err != nil {
161
+ return err
162
+ }
163
+
132
164
s .bufPool = sync.Pool {
133
165
New : func () interface {} {
134
166
return new (bytes.Buffer )
135
167
},
136
168
}
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
- }
151
169
s .Retriever = plugins .DefaultSouinPluginInitializerFromConfiguration (s .Configuration )
152
170
s .RequestCoalescing = coalescing .Initialize ()
153
171
return nil
154
172
}
155
173
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 )
158
176
cfg := & Configuration {
159
177
DefaultCache : & DefaultCache {
160
178
Distributed : false ,
@@ -167,10 +185,6 @@ func parseCaddyfileGlobalOption(h *caddyfile.Dispenser) (interface{}, error) {
167
185
for nesting := h .Nesting (); h .NextBlock (nesting ); {
168
186
rootOption := h .Val ()
169
187
switch rootOption {
170
- case "distributed" :
171
- args := h .RemainingArgs ()
172
- distributed , _ := strconv .ParseBool (args [0 ])
173
- cfg .DefaultCache .Distributed = distributed
174
188
case "headers" :
175
189
args := h .RemainingArgs ()
176
190
cfg .DefaultCache .Headers = append (cfg .DefaultCache .Headers , args ... )
@@ -197,24 +211,18 @@ func parseCaddyfileGlobalOption(h *caddyfile.Dispenser) (interface{}, error) {
197
211
}
198
212
}
199
213
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
203
220
}
204
221
205
222
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 {}
213
224
sc := Configuration {
214
225
DefaultCache : & dc ,
215
- URLs : staticConfig .URLs ,
216
- LogLevel : staticConfig .LogLevel ,
217
- logger : staticConfig .logger ,
218
226
}
219
227
220
228
for h .Next () {
@@ -227,11 +235,6 @@ func parseCaddyfileHandlerDirective(h httpcaddyfile.Helper) (caddyhttp.Middlewar
227
235
}
228
236
}
229
237
230
- appConfigs .LoadOrStore (appCounter , & sc )
231
- _ , _ = appConfigs .Delete ("counter" )
232
- appConfigs .LoadOrStore ("counter" , appCounter )
233
- appCounter ++
234
-
235
238
return & SouinCaddyPlugin {
236
239
Configuration : & sc ,
237
240
}, nil
0 commit comments