forked from caddyserver/caddy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
on.go
68 lines (52 loc) · 1.23 KB
/
on.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
package onevent
import (
"strings"
"github.com/google/uuid"
"github.com/mholt/caddy"
"github.com/mholt/caddy/onevent/hook"
)
func init() {
// Register Directive.
caddy.RegisterPlugin("on", caddy.Plugin{Action: setup})
}
func setup(c *caddy.Controller) error {
config, err := onParse(c)
if err != nil {
return err
}
// Register Event Hooks.
c.OncePerServerBlock(func() error {
for _, cfg := range config {
caddy.RegisterEventHook("on-"+cfg.ID, cfg.Hook)
}
return nil
})
return nil
}
func onParse(c *caddy.Controller) ([]*hook.Config, error) {
var config []*hook.Config
for c.Next() {
cfg := new(hook.Config)
if !c.NextArg() {
return config, c.ArgErr()
}
// Configure Event.
event, ok := hook.SupportedEvents[strings.ToLower(c.Val())]
if !ok {
return config, c.Errf("Wrong event name or event not supported: '%s'", c.Val())
}
cfg.Event = event
// Assign an unique ID.
cfg.ID = uuid.New().String()
args := c.RemainingArgs()
// Extract command and arguments.
command, args, err := caddy.SplitCommandAndArgs(strings.Join(args, " "))
if err != nil {
return config, c.Err(err.Error())
}
cfg.Command = command
cfg.Args = args
config = append(config, cfg)
}
return config, nil
}