-
Notifications
You must be signed in to change notification settings - Fork 8
/
setup.go
285 lines (253 loc) · 7.46 KB
/
setup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright 2015-present, Cyrill @ Schumacher.fm and the CoreStore contributors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
package caddyesi
import (
"io"
"net/http"
"os"
"strings"
"time"
"github.com/corestoreio/caddy-esi/esicache"
"github.com/corestoreio/caddy-esi/esitag"
_ "github.com/corestoreio/caddy-esi/esitag/backend" // Let them register depending on the build tag
"github.com/corestoreio/caddy-esi/helper"
"github.com/corestoreio/errors"
"github.com/corestoreio/log"
"github.com/corestoreio/log/zapw"
"github.com/dustin/go-humanize"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func init() {
caddy.RegisterPlugin("esi", caddy.Plugin{
ServerType: "http",
Action: PluginSetup,
})
}
// PluginSetup used internally by Caddy to set up this middleware
func PluginSetup(c *caddy.Controller) error {
pcs, err := configEsiParse(c)
if err != nil {
return errors.Wrap(err, "[caddyesi] Failed to parse configuration")
}
cfg := httpserver.GetConfig(c)
mw := &Middleware{
Root: cfg.Root,
FileSys: http.Dir(cfg.Root),
PathConfigs: pcs,
}
cfg.AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
mw.Next = next
return mw
})
c.OnShutdown(func() error {
return errors.Wrap(esitag.CloseAllResourceHandler(), "[caddyesi] OnShutdown")
})
c.OnRestart(func() error {
// really necessary? investigate later
for _, pc := range pcs {
pc.purgeESICache()
}
return errors.Wrap(esitag.CloseAllResourceHandler(), "[caddyesi] OnRestart")
})
return nil
}
func configEsiParse(c *caddy.Controller) (PathConfigs, error) {
pcs := make(PathConfigs, 0, 2)
for c.Next() {
pc := NewPathConfig()
// Get the path scope
args := c.RemainingArgs()
switch len(args) {
case 0:
pc.Scope = "/"
case 1:
pc.Scope = args[0]
default:
return nil, c.ArgErr()
}
// Load any other configuration parameters
for c.NextBlock() {
if err := configLoadParams(c, pc); err != nil {
return nil, errors.Wrap(err, "[caddyesi] Failed to load params")
}
}
if err := setupLogger(pc); err != nil {
return nil, errors.Wrap(err, "[caddyesi] Failed to setup Logger")
}
if pc.MaxBodySize == 0 {
pc.MaxBodySize = DefaultMaxBodySize
}
if pc.Timeout == 0 {
pc.Timeout = DefaultTimeOut
}
if len(pc.OnError) == 0 {
pc.OnError = []byte(DefaultOnError)
}
pcs = append(pcs, pc)
}
return pcs, nil
}
// mocked out for testing
var osStdErr io.Writer = os.Stderr
var osStdOut io.Writer = os.Stdout
func setupLogger(pc *PathConfig) error {
pc.Log = log.BlackHole{}
const loggingDisabled zapcore.Level = -50
var lvl = loggingDisabled
switch pc.LogLevel {
case "debug":
lvl = zap.DebugLevel
case "info":
lvl = zap.InfoLevel
case "fatal":
lvl = zap.FatalLevel
}
if lvl == loggingDisabled {
// logging disabled
return nil
}
var w io.Writer
switch pc.LogFile {
case "stderr":
w = osStdErr
case "stdout":
w = osStdOut
case "":
// logging disabled
return nil
default:
var err error
w, err = os.OpenFile(pc.LogFile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
// maybe handle file close on server restart or shutdown
if err != nil {
return errors.Fatal.Newf("[caddyesi] Failed to open file %q with error: %s", pc.LogFile, err)
}
}
pc.Log = zapw.Wrap{
Level: lvl,
Zap: zap.New(
zapcore.NewCore(
zapcore.NewJSONEncoder(
zapcore.EncoderConfig{
MessageKey: "msg",
LevelKey: "level",
TimeKey: "ts",
NameKey: "name",
CallerKey: "caller",
StacktraceKey: "stacktrace",
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.NanosDurationEncoder,
}),
zapcore.AddSync(w),
lvl,
),
),
}
return nil
}
func configLoadParams(c *caddy.Controller, pc *PathConfig) error {
switch key := c.Val(); key {
case "timeout":
if !c.NextArg() {
return errors.NotValid.Newf("[caddyesi] timeout: %s", c.ArgErr())
}
d, err := time.ParseDuration(c.Val())
if err != nil {
return errors.NotValid.Newf("[caddyesi] Invalid duration in timeout configuration: %q Error: %s", c.Val(), err)
}
pc.Timeout = d
case "ttl":
if !c.NextArg() {
return errors.NotValid.Newf("[caddyesi] ttl: %s", c.ArgErr())
}
d, err := time.ParseDuration(c.Val())
if err != nil {
return errors.NotValid.Newf("[caddyesi] Invalid duration in ttl configuration: %q Error: %s", c.Val(), err)
}
pc.TTL = d
case "max_body_size":
if !c.NextArg() {
return errors.NotValid.Newf("[caddyesi] max_body_size: %s", c.ArgErr())
}
d, err := humanize.ParseBytes(c.Val())
if err != nil {
return errors.NotValid.Newf("[caddyesi] Invalid max body size value configuration: %q Error: %s", c.Val(), err)
}
pc.MaxBodySize = d
case "cache":
if !c.NextArg() {
return errors.NotValid.Newf("[caddyesi] cache: %s", c.ArgErr())
}
if err := esicache.MainRegistry.Register(pc.Scope, c.Val()); err != nil {
return errors.Wrapf(err, "[caddyesi] esicache.MainRegistry.Register Key %q with URL: %q", key, c.Val())
}
case "page_id_source":
if !c.NextArg() {
return errors.NotValid.Newf("[caddyesi] page_id_source: %s", c.ArgErr())
}
pc.PageIDSource = helper.CommaListToSlice(c.Val())
case "allowed_methods":
if !c.NextArg() {
return errors.NotValid.Newf("[caddyesi] allowed_methods: %s", c.ArgErr())
}
pc.AllowedMethods = helper.CommaListToSlice(strings.ToUpper(c.Val()))
case "cmd_header_name":
if !c.NextArg() {
return errors.NotValid.Newf("[caddyesi] cmd_header_name: %s", c.ArgErr())
}
pc.CmdHeaderName = http.CanonicalHeaderKey(c.Val())
case "on_error":
if !c.NextArg() {
return errors.NotValid.Newf("[caddyesi] allowed_methods: %s", c.ArgErr())
}
if err := pc.parseOnError(c.Val()); err != nil {
return errors.Wrap(err, "[caddyesi] PathConfig.parseOnError")
}
case "log_file":
if !c.NextArg() {
return errors.NotValid.Newf("[caddyesi] log_file: %s", c.ArgErr())
}
pc.LogFile = c.Val()
case "log_level":
if !c.NextArg() {
return errors.NotValid.Newf("[caddyesi] log_level: %s", c.ArgErr())
}
pc.LogLevel = strings.ToLower(c.Val())
case "resources":
if !c.NextArg() {
return errors.NotValid.Newf("[caddyesi] resources: %s", c.ArgErr())
}
// c.Val() contains the file name or raw-content ;-)
items, err := UnmarshalResourceItems(c.Val())
if err != nil {
return errors.Wrapf(err, "[caddyesi] Failed to unmarshal resource config %q", c.Val())
}
for _, item := range items {
f, err := esitag.NewResourceHandler(esitag.NewResourceOptions(item.URL, item.Alias, item.Query))
if err != nil {
// may disclose passwords which are stored in the URL
return errors.Wrapf(err, "[caddyesi] esikv Service init failed for URL %q in file %q", item.URL, c.Val())
}
esitag.RegisterResourceHandler(item.Alias, f)
}
default:
c.NextArg()
return errors.NotSupported.Newf("[caddyesi] Key %q with value %q not supported", key, c.Val())
}
return nil
}