-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
55 lines (45 loc) · 1.23 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
package recorder
import (
"fmt"
"strings"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
clog "github.com/coredns/coredns/plugin/pkg/log"
)
func setup(c *caddy.Controller) error {
var out output = &defaultOutput{}
c.Next()
if c.NextArg() {
switch c.Val() {
case "nats":
args := c.RemainingArgs()
if len(args) != 2 {
err := fmt.Errorf("expected <topic> <endpoint>, got %s: %w", strings.Join(args, " "), c.ArgErr())
return plugin.Error(PluginName, err)
}
out = &natsOutput{Topic: args[0], Endpoint: args[1]}
if err := out.(*natsOutput).Validate(); err != nil {
return plugin.Error(PluginName, fmt.Errorf("invalid nats config: %v: %w", err, c.ArgErr()))
}
default:
return plugin.Error(PluginName, c.ArgErr())
}
}
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
return Plugin{
Next: next,
out: out,
log: clog.NewWithPlugin(PluginName),
}
})
c.OnStartup(func() error {
if err := out.Connect(); err != nil {
clog.Warningf("Connecting recorder: %v", err)
}
return nil
})
c.OnShutdown(func() error { return out.Flush() })
return nil
}
func init() { plugin.Register(PluginName, setup) }