|
@@ -16,31 +16,44 @@ type Example struct {
|
|
|
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) {
|
|
|
- // Somewhat convoluted, as we could have printed "example" here and just call
|
|
|
- // the next plugin - but as an example, show how to wrap a ResponseWriter might be
|
|
|
- // educational.
|
|
|
+ // This function could be simpler. I.e. just fmt.Println("example") here, but we want to show
|
|
|
+ // a slightly more complex example as to make this more interesting.
|
|
|
+ // 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)
|
|
|
+
|
|
|
+ // Call next plugin (if any).
|
|
|
return plugin.NextOrFailure(e.Name(), e.Next, ctx, pw, r)
|
|
|
}
|
|
|
|
|
|
// Name implements the Handler interface.
|
|
|
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 {
|
|
|
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 {
|
|
|
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 {
|
|
|
- fmt.Fprintf(out, "example")
|
|
|
+ fmt.Fprintln(out, ex)
|
|
|
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
|
|
|
+
|
|
|
+// What we will print.
|
|
|
+const ex = "example"
|