This repository has been archived by the owner on Nov 22, 2018. It is now read-only.
forked from nicolasazrak/caddy-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
127 lines (109 loc) · 2.94 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
package cache
import (
"time"
"os"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver"
)
var (
defaultStatusHeader = "X-Cache-Status"
defaultLockTimeout = time.Duration(5) * time.Minute
defaultMaxAge = time.Duration(5) * time.Minute
defaultPath = ""
)
type Config struct {
StatusHeader string
DefaultMaxAge time.Duration
LockTimeout time.Duration
CacheRules []CacheRule
Path string
}
func init() {
// httpserver.RegisterDevDirective("cache", "root")
caddy.RegisterPlugin("cache", caddy.Plugin{
ServerType: "http",
Action: Setup,
})
}
// Setup creates a new cache middleware
func Setup(c *caddy.Controller) error {
config, err := cacheParse(c)
if err != nil {
return err
}
httpserver.GetConfig(c).AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
return NewHandler(next, config)
})
c.OnStartup(func() error {
if config.Path == "" {
return nil
}
return os.MkdirAll(config.Path, 0600)
})
return nil
}
func emptyConfig() *Config {
return &Config{
StatusHeader: defaultStatusHeader,
DefaultMaxAge: defaultMaxAge,
LockTimeout: defaultLockTimeout,
CacheRules: []CacheRule{},
Path: defaultPath,
}
}
func cacheParse(c *caddy.Controller) (*Config, error) {
config := emptyConfig()
c.Next() // Skip "cache" literal
if len(c.RemainingArgs()) > 1 {
return nil, c.Err("Unexpected value " + c.Val())
}
for c.NextBlock() {
parameter := c.Val()
args := c.RemainingArgs()
switch parameter {
case "status_header":
if len(args) != 1 {
return nil, c.Err("Invalid usage of status_header in cache config.")
}
config.StatusHeader = args[0]
case "lock_timeout":
if len(args) != 1 {
return nil, c.Err("Invalid usage of lock_timeout in cache config.")
}
duration, err := time.ParseDuration(c.Val())
if err != nil {
return nil, c.Err("lock_timeout: Invalid duration " + c.Val())
}
config.LockTimeout = duration
case "default_max_age":
if len(args) != 1 {
return nil, c.Err("Invalid usage of default_max_age in cache config.")
}
duration, err := time.ParseDuration(c.Val())
if err != nil {
return nil, c.Err("default_max_age: Invalid duration " + c.Val())
}
config.DefaultMaxAge = duration
case "path":
if len(args) != 1 {
return nil, c.Err("Invalid usage of path in cache config.")
}
config.Path = args[0]
case "match_header":
if len(args) < 2 {
return nil, c.Err("Invalid usage of match_header in cache config.")
}
cacheRule := &HeaderCacheRule{Header: args[0], Value: args[1:]}
config.CacheRules = append(config.CacheRules, cacheRule)
case "match_path":
if len(args) != 1 {
return nil, c.Err("Invalid usage of match_path in cache config.")
}
cacheRule := &PathCacheRule{Path: args[0]}
config.CacheRules = append(config.CacheRules, cacheRule)
default:
return nil, c.Err("Unknown cache parameter: " + parameter)
}
}
return config, nil
}