Simplify the arguments passed to nameserver/http.go:ServeHTTP() (by using information found in some other objects we are already passing, like the server)

This commit is contained in:
Alvaro Saurin committed 2015-06-10 22:27:49 +02:00
1 parent 5cb2347f77
commit 1889a4a87f
3 files changed
+13 -9

No files matched your search

+7 -6
View File
@@ -24,14 +24,14 @@ func extractFQDN(r *http.Request) (string, string) {
return "*", "" return "*", ""
} }
func ServeHTTP(listener net.Listener, version string, server *DNSServer, domain string, db Zone) { func ServeHTTP(listener net.Listener, version string, server *DNSServer) {
muxRouter := mux.NewRouter() muxRouter := mux.NewRouter()
muxRouter.Methods("GET").Path("/status").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { muxRouter.Methods("GET").Path("/status").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "weave DNS", version) fmt.Fprintln(w, "weave DNS", version)
fmt.Fprintln(w, server.Status()) fmt.Fprintln(w, server.Status())
fmt.Fprintln(w, db.Status()) fmt.Fprintln(w, server.Zone.Status())
}) })
muxRouter.Methods("PUT").Path("/name/{id:.+}/{ip:.+}").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { muxRouter.Methods("PUT").Path("/name/{id:.+}/{ip:.+}").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -55,9 +55,10 @@ func ServeHTTP(listener net.Listener, version string, server *DNSServer, domain
return return
} }
domain := server.Zone.Domain()
if dns.IsSubDomain(domain, name) { if dns.IsSubDomain(domain, name) {
Info.Printf("[http] Adding %s -> %s", name, ipStr) Info.Printf("[http] Adding %s -> %s", name, ipStr)
if err := db.AddRecord(idStr, name, ip); err != nil { if err := server.Zone.AddRecord(idStr, name, ip); err != nil {
if _, ok := err.(DuplicateError); !ok { if _, ok := err.(DuplicateError); !ok {
httpErrorAndLog( httpErrorAndLog(
Error, w, "Internal error", http.StatusInternalServerError, Error, w, "Internal error", http.StatusInternalServerError,
@@ -86,7 +87,7 @@ func ServeHTTP(listener net.Listener, version string, server *DNSServer, domain
fqdnStr, fqdn := extractFQDN(r) fqdnStr, fqdn := extractFQDN(r)
Info.Printf("[http] Deleting ID %s, IP %s, FQDN %s", idStr, ipStr, fqdnStr) Info.Printf("[http] Deleting ID %s, IP %s, FQDN %s", idStr, ipStr, fqdnStr)
db.DeleteRecords(idStr, fqdn, ip) server.Zone.DeleteRecords(idStr, fqdn, ip)
}) })
muxRouter.Methods("DELETE").Path("/name/{id:.+}").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { muxRouter.Methods("DELETE").Path("/name/{id:.+}").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -96,14 +97,14 @@ func ServeHTTP(listener net.Listener, version string, server *DNSServer, domain
fqdnStr, fqdn := extractFQDN(r) fqdnStr, fqdn := extractFQDN(r)
Info.Printf("[http] Deleting ID %s, IP *, FQDN %s", idStr, fqdnStr) Info.Printf("[http] Deleting ID %s, IP *, FQDN %s", idStr, fqdnStr)
db.DeleteRecords(idStr, fqdn, net.IP{}) server.Zone.DeleteRecords(idStr, fqdn, net.IP{})
}) })
muxRouter.Methods("DELETE").Path("/name").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { muxRouter.Methods("DELETE").Path("/name").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fqdnStr, fqdn := extractFQDN(r) fqdnStr, fqdn := extractFQDN(r)
Info.Printf("[http] Deleting ID *, IP *, FQDN %s", fqdnStr) Info.Printf("[http] Deleting ID *, IP *, FQDN %s", fqdnStr)
db.DeleteRecords("", fqdn, net.IP{}) server.Zone.DeleteRecords("", fqdn, net.IP{})
}) })
http.Handle("/", muxRouter) http.Handle("/", muxRouter)
+5 -2
View File
@@ -27,7 +27,7 @@ func TestHttp(t *testing.T) {
dockerIP = "9.8.7.6" dockerIP = "9.8.7.6"
) )
zone, err := NewZoneDb(ZoneConfig{}) zone, err := NewZoneDb(ZoneConfig{Domain: testDomain})
wt.AssertNoErr(t, err) wt.AssertNoErr(t, err)
err = zone.Start() err = zone.Start()
wt.AssertNoErr(t, err) wt.AssertNoErr(t, err)
@@ -38,8 +38,11 @@ func TestHttp(t *testing.T) {
t.Fatal("Unable to create http listener: ", err) t.Fatal("Unable to create http listener: ", err)
} }
// Create a useless server... it will not even be started
srv, _ := NewDNSServer(DNSServerConfig{Zone: zone})
port := httpListener.Addr().(*net.TCPAddr).Port port := httpListener.Addr().(*net.TCPAddr).Port
go ServeHTTP(httpListener, "", nil, testDomain, zone) go ServeHTTP(httpListener, "", srv)
// Ask the http server to add our test address into the database // Ask the http server to add our test address into the database
addrParts := strings.Split(testAddr1, "/") addrParts := strings.Split(testAddr1, "/")
+1 -1
View File
@@ -145,7 +145,7 @@ func main() {
} }
go SignalHandlerLoop(srv) go SignalHandlerLoop(srv)
go weavedns.ServeHTTP(httpListener, version, srv, domain, zone) go weavedns.ServeHTTP(httpListener, version, srv)
err = srv.Start() err = srv.Start()
if err != nil { if err != nil {