mirror of
				https://github.com/coredns/coredns.git
				synced 2025-11-04 03:03:14 -05:00 
			
		
		
		
	Abstract the caddy call and make it simpler. See #3261 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>
		
			
				
	
	
		
			47 lines
		
	
	
		
			984 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			47 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
 | 
						|
}
 |