coredns/plugin/rewrite/setup.go
Miek Gieben 004c5fca9d
all: simply registering plugins ()
Abstract the caddy call and make it simpler.

See  for some part of the discussion.

Go from:

~~~ go
func init() {
       caddy.RegisterPlugin("any", caddy.Plugin{
               ServerType: "dns",
               Action:     setup,
       })
}
~~~

To:

~~~ go
func init() { plugin.Register("any", setup) }
~~~

This requires some external documents in coredns.io to be updated as
well; the old way still works, so it's backwards compatible.

Signed-off-by: Miek Gieben <miek@miek.nl>
2019-09-20 08:02:30 +01:00

46 lines
984 B
Go

package rewrite
import (
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/caddyserver/caddy"
)
var log = clog.NewWithPlugin("rewrite")
func init() { plugin.Register("rewrite", setup) }
func setup(c *caddy.Controller) error {
rewrites, err := rewriteParse(c)
if err != nil {
return plugin.Error("rewrite", err)
}
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
return Rewrite{Next: next, Rules: rewrites}
})
return nil
}
func rewriteParse(c *caddy.Controller) ([]Rule, error) {
var rules []Rule
for c.Next() {
args := c.RemainingArgs()
if len(args) < 2 {
// Handles rules out of nested instructions, i.e. the ones enclosed in curly brackets
for c.NextBlock() {
args = append(args, c.Val())
}
}
rule, err := newRule(args...)
if err != nil {
return nil, err
}
rules = append(rules, rule)
}
return rules, nil
}