mirror of
				https://github.com/coredns/coredns.git
				synced 2025-11-03 10:43:20 -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
		
	
	
		
			808 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			47 lines
		
	
	
		
			808 B
		
	
	
	
		
			Go
		
	
	
	
	
	
package nsid
 | 
						|
 | 
						|
import (
 | 
						|
	"os"
 | 
						|
	"strings"
 | 
						|
 | 
						|
	"github.com/coredns/coredns/core/dnsserver"
 | 
						|
	"github.com/coredns/coredns/plugin"
 | 
						|
 | 
						|
	"github.com/caddyserver/caddy"
 | 
						|
)
 | 
						|
 | 
						|
func init() { plugin.Register("nsid", setup) }
 | 
						|
 | 
						|
func setup(c *caddy.Controller) error {
 | 
						|
	nsid, err := nsidParse(c)
 | 
						|
	if err != nil {
 | 
						|
		return plugin.Error("nsid", err)
 | 
						|
	}
 | 
						|
 | 
						|
	dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
 | 
						|
		return Nsid{Next: next, Data: nsid}
 | 
						|
	})
 | 
						|
 | 
						|
	return nil
 | 
						|
}
 | 
						|
 | 
						|
func nsidParse(c *caddy.Controller) (string, error) {
 | 
						|
	// Use hostname as the default
 | 
						|
	nsid, err := os.Hostname()
 | 
						|
	if err != nil {
 | 
						|
		nsid = "localhost"
 | 
						|
	}
 | 
						|
	i := 0
 | 
						|
	for c.Next() {
 | 
						|
		if i > 0 {
 | 
						|
			return nsid, plugin.ErrOnce
 | 
						|
		}
 | 
						|
		i++
 | 
						|
		args := c.RemainingArgs()
 | 
						|
		if len(args) > 0 {
 | 
						|
			nsid = strings.Join(args, " ")
 | 
						|
		}
 | 
						|
	}
 | 
						|
	return nsid, nil
 | 
						|
}
 |