-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcaddyfile.go
More file actions
144 lines (125 loc) · 3.27 KB
/
caddyfile.go
File metadata and controls
144 lines (125 loc) · 3.27 KB
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
package httpcache
import (
"regexp"
"strconv"
"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"
"github.com/golevi/cache-handler/config"
"github.com/golevi/cache-handler/stores"
"github.com/golevi/cache-handler/validators"
)
var (
cfg *config.Config
)
func init() {
caddy.RegisterModule(Cache{})
httpcaddyfile.RegisterGlobalOption("cache", parseCaddyfileGlobalOption)
httpcaddyfile.RegisterHandlerDirective("cache", parseCaddyfileHandlerDirective)
}
// CaddyModule returns the Caddy module information.
func (Cache) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.cache",
New: func() caddy.Module { return new(Cache) },
}
}
func parseCaddyfileGlobalOption(d *caddyfile.Dispenser) (interface{}, error) {
cfg = &config.Config{}
for d.Next() {
for d.NextBlock(0) {
switch d.Val() {
case "host":
if !d.NextArg() {
return nil, d.ArgErr()
}
cfg.Host = d.Val()
case "type":
if !d.NextArg() {
return nil, d.ArgErr()
}
cfg.Type = d.Val()
}
}
}
return nil, nil
}
func parseCaddyfileHandlerDirective(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
c := &Cache{}
if cfg != nil {
c.Config = *cfg
}
err := c.UnmarshalCaddyfile(h.Dispenser)
if err != nil {
return nil, err
}
return c, nil
}
// UnmarshalCaddyfile parses plugin settings from Caddyfile.
//
// {
// order cache first
// cache {
// type <redis>|<file>
// host localhost:6379
// }
// }
//
// cache {
// expire 120 # Cache expiration in seconds
// bypass {
// paths wp-admin wp-login.php system # WordPress and ExpressionEngine
// methods post # Don't typically cache POST
// cookies wordpress_logged_in_.* # WordPress
// # cookie exp_sessionid # ExpressionEngine
// }
// }
//
// This may change.
func (c *Cache) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() {
for d.NextBlock(0) {
switch d.Val() {
case "bypass":
for nesting := d.Nesting(); d.NextBlock(nesting); {
switch d.Val() {
case "paths":
c.Config.Bypass.Paths = d.RemainingArgs()
case "methods":
c.Config.Bypass.Methods = d.RemainingArgs()
case "cookies":
c.Config.Bypass.Cookies = d.RemainingArgs()
}
}
case "expire":
expire, _ := strconv.Atoi(d.RemainingArgs()[0])
c.Config.Expire = expire
}
}
}
return nil
}
// Provision _
func (c *Cache) Provision(ctx caddy.Context) error {
// Make sure regular expressions are valid
for _, cc := range c.Config.Bypass.Cookies {
c.Config.CookieRegexp = append(c.Config.CookieRegexp, regexp.MustCompile(cc))
}
c.logger = ctx.Logger(c)
switch c.Config.Type {
case "redis":
c.Store = stores.NewRedisStore(c.Config.Host)
case "file":
c.Store = stores.NewFileStore()
}
c.Validators = append(c.Validators, validators.URI)
c.Validators = append(c.Validators, validators.ShouldBypassHTTPMethod)
c.Validators = append(c.Validators, validators.Cookie)
return nil
}
var (
_ caddy.Provisioner = (*Cache)(nil)
_ caddyhttp.MiddlewareHandler = (*Cache)(nil)
_ caddyfile.Unmarshaler = (*Cache)(nil)
)