-
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
setup.go
93 lines (76 loc) · 1.83 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
package status
import (
"strconv"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver"
)
// init registers Status plugin
func init() {
caddy.RegisterPlugin("status", caddy.Plugin{
ServerType: "http",
Action: setup,
})
}
// setup configures new Status middleware instance.
func setup(c *caddy.Controller) error {
rules, err := statusParse(c)
if err != nil {
return err
}
cfg := httpserver.GetConfig(c)
mid := func(next httpserver.Handler) httpserver.Handler {
return Status{Rules: rules, Next: next}
}
cfg.AddMiddleware(mid)
return nil
}
// statusParse parses status directive
func statusParse(c *caddy.Controller) ([]httpserver.HandlerConfig, error) {
var rules []httpserver.HandlerConfig
for c.Next() {
hadBlock := false
args := c.RemainingArgs()
switch len(args) {
case 1:
status, err := strconv.Atoi(args[0])
if err != nil {
return rules, c.Errf("Expecting a numeric status code, got '%s'", args[0])
}
for c.NextBlock() {
hadBlock = true
basePath := c.Val()
for _, cfg := range rules {
rule := cfg.(*Rule)
if rule.Base == basePath {
return rules, c.Errf("Duplicate path: '%s'", basePath)
}
}
rule := NewRule(basePath, status)
rules = append(rules, rule)
if c.NextArg() {
return rules, c.ArgErr()
}
}
if !hadBlock {
return rules, c.ArgErr()
}
case 2:
status, err := strconv.Atoi(args[0])
if err != nil {
return rules, c.Errf("Expecting a numeric status code, got '%s'", args[0])
}
basePath := args[1]
for _, cfg := range rules {
rule := cfg.(*Rule)
if rule.Base == basePath {
return rules, c.Errf("Duplicate path: '%s'", basePath)
}
}
rule := NewRule(basePath, status)
rules = append(rules, rule)
default:
return rules, c.ArgErr()
}
}
return rules, nil
}