From 9ab2da8deb09fe7e9e3dd19713c167a95ac575c6 Mon Sep 17 00:00:00 2001 From: Tom Wilkie Date: Mon, 20 Jul 2015 10:12:31 +0000 Subject: [PATCH] Add GET /name handler to return all dns entries (including tombstones) for debugging. Also, make Address marshall into json as an IP string, not a number. --- nameserver/http.go | 9 +++++++++ net/address/address.go | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/nameserver/http.go b/nameserver/http.go index d85b9970..7b3eb352 100644 --- a/nameserver/http.go +++ b/nameserver/http.go @@ -1,6 +1,7 @@ package nameserver import ( + "encoding/json" "fmt" "net/http" @@ -82,4 +83,12 @@ func (n *Nameserver) HandleHTTP(router *mux.Router) { router.Methods("DELETE").Path("/name/{container}/{ip}").HandlerFunc(deleteHandler) router.Methods("DELETE").Path("/name/{container}").HandlerFunc(deleteHandler) router.Methods("DELETE").Path("/name").HandlerFunc(deleteHandler) + + router.Methods("GET").Path("/name").Headers("Accept", "application/json").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + n.RLock() + defer n.RUnlock() + if err := json.NewEncoder(w).Encode(n.entries); err != nil { + badRequest(w, fmt.Errorf("Error marshalling response: %v", err)) + } + }) } diff --git a/net/address/address.go b/net/address/address.go index b5ffb265..291a3227 100644 --- a/net/address/address.go +++ b/net/address/address.go @@ -90,6 +90,10 @@ func (addr Address) IP4() (r net.IP) { return } +func (addr Address) MarshalJSON() ([]byte, error) { + return []byte(fmt.Sprintf("%q", addr.String())), nil +} + func (addr Address) String() string { return addr.IP4().String() }