aboutsummaryrefslogtreecommitdiff
path: root/plugin/ready/setup.go
diff options
context:
space:
mode:
Diffstat (limited to 'plugin/ready/setup.go')
-rw-r--r--plugin/ready/setup.go75
1 files changed, 75 insertions, 0 deletions
diff --git a/plugin/ready/setup.go b/plugin/ready/setup.go
new file mode 100644
index 000000000..cbdb9583d
--- /dev/null
+++ b/plugin/ready/setup.go
@@ -0,0 +1,75 @@
+package ready
+
+import (
+ "net"
+
+ "github.com/coredns/coredns/core/dnsserver"
+ "github.com/coredns/coredns/plugin"
+
+ "github.com/mholt/caddy"
+)
+
+func init() {
+ caddy.RegisterPlugin("ready", caddy.Plugin{
+ ServerType: "dns",
+ Action: setup,
+ })
+}
+
+func setup(c *caddy.Controller) error {
+ addr, err := parse(c)
+ if err != nil {
+ return plugin.Error("ready", err)
+ }
+
+ rd := &ready{Addr: addr}
+
+ uniqAddr.Set(addr, rd.onStartup, rd)
+
+ c.OncePerServerBlock(func() error {
+ c.OnStartup(func() error {
+ return uniqAddr.ForEach()
+ })
+ return nil
+ })
+
+ c.OnStartup(func() error {
+ // Each plugin in this server block will (if they support it) report readiness.
+ plugs := dnsserver.GetConfig(c).Handlers()
+ for _, p := range plugs {
+ if r, ok := p.(Readiness); ok {
+ plugins.Append(r, p.Name())
+ }
+ }
+ return nil
+ })
+
+ c.OnRestart(rd.onRestart)
+ c.OnFinalShutdown(rd.onFinalShutdown)
+
+ return nil
+}
+
+func parse(c *caddy.Controller) (string, error) {
+ addr := ""
+ i := 0
+ for c.Next() {
+ if i > 0 {
+ return "", plugin.ErrOnce
+ }
+ i++
+ args := c.RemainingArgs()
+
+ switch len(args) {
+ case 0:
+ case 1:
+ addr = args[0]
+ if _, _, e := net.SplitHostPort(addr); e != nil {
+ return "", e
+ }
+ default:
+ return "", c.ArgErr()
+ }
+ }
+ return addr, nil
+}