Document and cleanup

This commit is contained in:
Miek Gieben 2018-02-25 08:52:52 +00:00
parent d9282030ca
commit 932108f489
4 changed files with 43 additions and 13 deletions

View File

@ -16,31 +16,44 @@ type Example struct {
Next plugin.Handler Next plugin.Handler
} }
// ServeDNS implements the plugin.Handler interface. // ServeDNS implements the plugin.Handler interface. This method gets called when example is used
// in a Server.
func (e Example) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { func (e Example) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
// Somewhat convoluted, as we could have printed "example" here and just call // This function could be simpler. I.e. just fmt.Println("example") here, but we want to show
// the next plugin - but as an example, show how to wrap a ResponseWriter might be // a slightly more complex example as to make this more interesting.
// educational. // Here we wrap the dns.ResponseWriter in a new ResponseWriter and call the next plugin, when the
// answer comes back, it will print "example".
// Wrap.
pw := NewResponsePrinter(w) pw := NewResponsePrinter(w)
// Call next plugin (if any).
return plugin.NextOrFailure(e.Name(), e.Next, ctx, pw, r) return plugin.NextOrFailure(e.Name(), e.Next, ctx, pw, r)
} }
// Name implements the Handler interface. // Name implements the Handler interface.
func (e Example) Name() string { return "example" } func (e Example) Name() string { return "example" }
// ResponsePrinter wrap a dns.ResponseWriter and will write example to standard output when
// WriteMsg is called.
type ResponsePrinter struct { type ResponsePrinter struct {
dns.ResponseWriter dns.ResponseWriter
} }
// NewResponsePrinter returns a dns.Msg modifier that print example when a query is received. // NewResponsePrinter returns ResponseWriter.
func NewResponsePrinter(w dns.ResponseWriter) *ResponsePrinter { func NewResponsePrinter(w dns.ResponseWriter) *ResponsePrinter {
return &ResponsePrinter{ResponseWriter: w} return &ResponsePrinter{ResponseWriter: w}
} }
// WriteMsg records the status code and calls the underlying ResponseWriter's WriteMsg method. // WriteMsg calls the underlying ResponseWriter's WriteMsg method and prints "example" to standard
// output.
func (r *ResponsePrinter) WriteMsg(res *dns.Msg) error { func (r *ResponsePrinter) WriteMsg(res *dns.Msg) error {
fmt.Fprintf(out, "example") fmt.Fprintln(out, ex)
return r.ResponseWriter.WriteMsg(res) return r.ResponseWriter.WriteMsg(res)
} }
// Make out a reference to os.Stdout so we can easily overwrite it for testing.
var out io.Writer = os.Stdout var out io.Writer = os.Stdout
// What we will print.
const ex = "example"

View File

@ -12,18 +12,24 @@ import (
) )
func TestExample(t *testing.T) { func TestExample(t *testing.T) {
ex := Example{Next: test.ErrorHandler()} // Create a new Example Plugin. Use the test.ErrorHandler as the next plugin.
x := Example{Next: test.ErrorHandler()}
// Setup a new output buffer that is *not* standard output, so we can check if
// example is really being printed.
b := &bytes.Buffer{} b := &bytes.Buffer{}
out = b out = b
ctx := context.TODO() ctx := context.TODO()
r := new(dns.Msg) r := new(dns.Msg)
r.SetQuestion("example.org.", dns.TypeA) r.SetQuestion("example.org.", dns.TypeA)
// Create a new Recorder that captures the result, this isn't actually used in this test
// as it just serves as something that implements the dns.ResponseWriter interface.
rec := dnstest.NewRecorder(&test.ResponseWriter{}) rec := dnstest.NewRecorder(&test.ResponseWriter{})
ex.ServeDNS(ctx, rec, r) // Call our plugin directly, and check the result.
if x := b.String(); x != "example" { x.ServeDNS(ctx, rec, r)
t.Errorf("Failed to print 'example', got %s", x) if a := b.String(); a != ex+"\n" {
t.Errorf("Failed to print '%s', got %s", ex, a)
} }
} }

View File

@ -7,6 +7,8 @@ import (
"github.com/mholt/caddy" "github.com/mholt/caddy"
) )
// init registers this plugin within the Caddy plugin framework. It uses "example" as the
// name, and couples it to the Action "setup".
func init() { func init() {
caddy.RegisterPlugin("example", caddy.Plugin{ caddy.RegisterPlugin("example", caddy.Plugin{
ServerType: "dns", ServerType: "dns",
@ -14,15 +16,22 @@ func init() {
}) })
} }
// setup is the function that gets called when the config parser see the token "example". Setup is responsible
// for parsing any extra options the example plugin may have. The first token this function sees is "example".
func setup(c *caddy.Controller) error { func setup(c *caddy.Controller) error {
c.Next() c.Next() // Ignore "example" and give us the next token.
if c.NextArg() { if c.NextArg() {
// If there was another token, return an error, because we don't have any configuration.
// Any errors returned from this setup function should be wrapped with plugin.Error, so we
// can present a slightly nicer error message to the user.
return plugin.Error("example", c.ArgErr()) return plugin.Error("example", c.ArgErr())
} }
// Add the Plugin to CoreDNS, so Servers can use it in their plugin chain.
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler { dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
return Example{Next: next} return Example{Next: next}
}) })
// All OK, return a nil error.
return nil return nil
} }

View File

@ -6,7 +6,9 @@ import (
"github.com/mholt/caddy" "github.com/mholt/caddy"
) )
func TestSetupWhoami(t *testing.T) { // TestSetup tests the various things that should be parsed by setup.
// Make sure you also test for parse errors.
func TestSetup(t *testing.T) {
c := caddy.NewTestController("dns", `example`) c := caddy.NewTestController("dns", `example`)
if err := setup(c); err != nil { if err := setup(c); err != nil {
t.Fatalf("Expected no errors, but got: %v", err) t.Fatalf("Expected no errors, but got: %v", err)