-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
caddyfile.go
102 lines (94 loc) · 2.17 KB
/
caddyfile.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
package mwcache
import (
"fmt"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
)
func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
h.config = Config{
Backend: "ristretto",
PurgeAcl: []string{"127.0.0.1"},
}
config = &h.config
for d.Next() {
if len(d.RemainingArgs()) == 1 {
switch d.Val() {
case "ristretto":
// Use default
default:
return d.ArgErr()
}
}
for d.NextBlock(0) {
switch d.Val() {
case "ristretto":
// Use default backend
// Unmarshal block
if len(d.RemainingArgs()) != 1 {
config.RistrettoConfig = map[string]string{}
for d.NextBlock(1) {
k := d.Val()
if !d.Next() {
return d.ArgErr()
}
config.RistrettoConfig[k] = d.Val()
}
}
case "purge_acl":
// TODO throw error when an empty block is given
config.PurgeAcl = nil
if len(d.RemainingArgs()) == 1 && !d.NextBlock(1) {
config.PurgeAcl = []string{d.Val()}
} else {
for d.NextBlock(1) {
config.PurgeAcl = append(config.PurgeAcl, d.Val())
}
}
default:
return d.ArgErr()
}
}
}
return nil
}
// Validate implements caddy.Validator.
func (h *Handler) Validate() error {
h.config = *config
if config.Backend == "" {
return fmt.Errorf("no backend")
}
if config.PurgeAcl == nil {
return fmt.Errorf("no purge acl")
}
if config.RistrettoConfig != nil {
if err := ValidateRistrettoConfig(config.RistrettoConfig); err != nil {
return err
}
}
return nil
}
// Provision implements caddy.Provisioner.
func (h *Handler) Provision(ctx caddy.Context) error {
h.logger = ctx.Logger(h)
h.logger.Info("logger is created")
switch config.Backend {
case "ristretto":
b, err := newRistrettoBackend(config.RistrettoConfig)
if err != nil {
return err
}
backend = b
}
return nil
}
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
var m Handler
err := m.UnmarshalCaddyfile(h.Dispenser)
return m, err
}
// Interface guards
var (
_ caddyfile.Unmarshaler = (*Handler)(nil)
)