setup.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. package example
  2. import (
  3. "github.com/coredns/coredns/core/dnsserver"
  4. "github.com/coredns/coredns/plugin"
  5. "github.com/mholt/caddy"
  6. )
  7. // init registers this plugin within the Caddy plugin framework. It uses "example" as the
  8. // name, and couples it to the Action "setup".
  9. func init() {
  10. caddy.RegisterPlugin("example", caddy.Plugin{
  11. ServerType: "dns",
  12. Action: setup,
  13. })
  14. }
  15. // setup is the function that gets called when the config parser see the token "example". Setup is responsible
  16. // for parsing any extra options the example plugin may have. The first token this function sees is "example".
  17. func setup(c *caddy.Controller) error {
  18. c.Next() // Ignore "example" and give us the next token.
  19. if c.NextArg() {
  20. // If there was another token, return an error, because we don't have any configuration.
  21. // Any errors returned from this setup function should be wrapped with plugin.Error, so we
  22. // can present a slightly nicer error message to the user.
  23. return plugin.Error("example", c.ArgErr())
  24. }
  25. // Add the Plugin to CoreDNS, so Servers can use it in their plugin chain.
  26. dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
  27. return Example{Next: next}
  28. })
  29. // All OK, return a nil error.
  30. return nil
  31. }