coredns/plugin/header/setup.go
Qasim Sarfraz 00128bda4e
plugin/header: Introduce header plugin (#4752)
* Add header plugin

Signed-off-by: MQasimSarfraz <syed.qasim.sarfraz@gmail.com>

* fix import format

* improve README.md

* Add codeowners for header plugin
2021-07-15 09:32:39 +02:00

50 lines
887 B
Go

package header
import (
"fmt"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"github.com/coredns/caddy"
)
func init() { plugin.Register("header", setup) }
func setup(c *caddy.Controller) error {
rules, err := parse(c)
if err != nil {
return plugin.Error("header", err)
}
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
return Header{
Rules: rules,
Next: next,
}
})
return nil
}
func parse(c *caddy.Controller) ([]Rule, error) {
for c.Next() {
var all []Rule
for c.NextBlock() {
v := c.Val()
args := c.RemainingArgs()
// set up rules
rules, err := newRules(v, args)
if err != nil {
return nil, fmt.Errorf("seting up rule: %w", err)
}
all = append(all, rules...)
}
// return combined rules
if len(all) > 0 {
return all, nil
}
}
return nil, c.ArgErr()
}