2017-07-29 08:17:24 +00:00
|
|
|
package example
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/coredns/coredns/core/dnsserver"
|
2017-09-15 20:29:47 +00:00
|
|
|
"github.com/coredns/coredns/plugin"
|
2018-04-01 11:56:41 +00:00
|
|
|
"github.com/coredns/coredns/plugin/metrics"
|
2017-07-29 08:17:24 +00:00
|
|
|
|
2019-07-10 07:39:00 +00:00
|
|
|
"github.com/caddyserver/caddy"
|
2017-07-29 08:17:24 +00:00
|
|
|
)
|
|
|
|
|
2018-02-25 08:52:52 +00:00
|
|
|
// init registers this plugin within the Caddy plugin framework. It uses "example" as the
|
|
|
|
// name, and couples it to the Action "setup".
|
2017-07-29 08:17:24 +00:00
|
|
|
func init() {
|
|
|
|
caddy.RegisterPlugin("example", caddy.Plugin{
|
|
|
|
ServerType: "dns",
|
|
|
|
Action: setup,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-02-25 08:52:52 +00:00
|
|
|
// setup is the function that gets called when the config parser see the token "example". Setup is responsible
|
|
|
|
// for parsing any extra options the example plugin may have. The first token this function sees is "example".
|
2017-07-29 08:17:24 +00:00
|
|
|
func setup(c *caddy.Controller) error {
|
2018-02-25 08:52:52 +00:00
|
|
|
c.Next() // Ignore "example" and give us the next token.
|
2017-07-29 08:17:24 +00:00
|
|
|
if c.NextArg() {
|
2018-02-25 08:52:52 +00:00
|
|
|
// If there was another token, return an error, because we don't have any configuration.
|
|
|
|
// Any errors returned from this setup function should be wrapped with plugin.Error, so we
|
|
|
|
// can present a slightly nicer error message to the user.
|
2017-09-15 20:29:47 +00:00
|
|
|
return plugin.Error("example", c.ArgErr())
|
2017-07-29 08:17:24 +00:00
|
|
|
}
|
|
|
|
|
2018-04-01 11:56:41 +00:00
|
|
|
// Add a startup function that will -- after all plugins have been loaded -- check if the
|
|
|
|
// prometheus plugin has been used - if so we will export metrics. We can only register
|
|
|
|
// this metric once, hence the "once.Do".
|
|
|
|
c.OnStartup(func() error {
|
2018-04-01 12:28:37 +00:00
|
|
|
once.Do(func() { metrics.MustRegister(c, requestCount) })
|
2018-04-01 11:56:41 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2018-02-25 08:52:52 +00:00
|
|
|
// Add the Plugin to CoreDNS, so Servers can use it in their plugin chain.
|
2017-09-15 20:29:47 +00:00
|
|
|
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
|
2017-07-29 08:17:24 +00:00
|
|
|
return Example{Next: next}
|
|
|
|
})
|
|
|
|
|
2018-02-25 08:52:52 +00:00
|
|
|
// All OK, return a nil error.
|
2017-07-29 08:17:24 +00:00
|
|
|
return nil
|
|
|
|
}
|