mirror of
https://github.com/coredns/coredns.git
synced 2025-11-09 13:32:16 -05:00
Make CoreDNS a server type plugin for Caddy (#220)
* Make CoreDNS a server type plugin for Caddy Remove code we don't need and port all middleware over. Fix all tests and rework the documentation. Also make `go generate` build a caddy binary which we then copy into our directory. This means `go build`-builds remain working as-is. And new etc instances in each etcd test for better isolation. Fix more tests and rework test.Server with the newer support Caddy offers. Fix Makefile to support new mode of operation.
This commit is contained in:
@@ -12,7 +12,7 @@ type Handler struct {
|
||||
mux *http.ServeMux
|
||||
}
|
||||
|
||||
func (h *Handler) Start() error {
|
||||
func (h *Handler) Startup() error {
|
||||
if ln, err := net.Listen("tcp", addr); err != nil {
|
||||
log.Printf("[ERROR] Failed to start pprof handler: %s", err)
|
||||
return err
|
||||
|
||||
40
middleware/pprof/setup.go
Normal file
40
middleware/pprof/setup.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package pprof
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/mholt/caddy"
|
||||
)
|
||||
|
||||
func init() {
|
||||
caddy.RegisterPlugin("pprof", caddy.Plugin{
|
||||
ServerType: "dns",
|
||||
Action: setup,
|
||||
})
|
||||
}
|
||||
|
||||
func setup(c *caddy.Controller) error {
|
||||
found := false
|
||||
for c.Next() {
|
||||
if found {
|
||||
return c.Err("pprof can only be specified once")
|
||||
}
|
||||
if len(c.RemainingArgs()) != 0 {
|
||||
return c.ArgErr()
|
||||
}
|
||||
if c.NextBlock() {
|
||||
return c.ArgErr()
|
||||
}
|
||||
found = true
|
||||
}
|
||||
|
||||
handler := &Handler{}
|
||||
pprofOnce.Do(func() {
|
||||
c.OnStartup(handler.Startup)
|
||||
c.OnShutdown(handler.Shutdown)
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var pprofOnce sync.Once
|
||||
32
middleware/pprof/setup_test.go
Normal file
32
middleware/pprof/setup_test.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package pprof
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/mholt/caddy"
|
||||
)
|
||||
|
||||
func TestPProf(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
shouldErr bool
|
||||
}{
|
||||
{`pprof`, false},
|
||||
{`pprof {}`, true},
|
||||
{`pprof /foo`, true},
|
||||
{`pprof {
|
||||
a b
|
||||
}`, true},
|
||||
{`pprof
|
||||
pprof`, true},
|
||||
}
|
||||
for i, test := range tests {
|
||||
c := caddy.NewTestController("dns", test.input)
|
||||
err := setup(c)
|
||||
if test.shouldErr && err == nil {
|
||||
t.Errorf("Test %v: Expected error but found nil", i)
|
||||
} else if !test.shouldErr && err != nil {
|
||||
t.Errorf("Test %v: Expected no error but found error: %v", i, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user