Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Simplify the arguments passed to nameserver/http.go:ServeHTTP() (by u…
Browse files Browse the repository at this point in the history
…sing information found in some other objects we are already passing, like the server)
  • Loading branch information
inercia committed Jun 8, 2015
1 parent abe6eb3 commit 0ff4fc3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
13 changes: 7 additions & 6 deletions nameserver/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ func extractFQDN(r *http.Request) (string, string) {
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.Methods("GET").Path("/status").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "weave DNS", version)
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) {
Expand All @@ -55,9 +55,10 @@ func ServeHTTP(listener net.Listener, version string, server *DNSServer, domain
return
}

domain := server.Zone.Domain()
if dns.IsSubDomain(domain, name) {
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 {
httpErrorAndLog(
Error, w, "Internal error", http.StatusInternalServerError,
Expand Down Expand Up @@ -86,7 +87,7 @@ func ServeHTTP(listener net.Listener, version string, server *DNSServer, domain
fqdnStr, fqdn := extractFQDN(r)

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) {
Expand All @@ -96,14 +97,14 @@ func ServeHTTP(listener net.Listener, version string, server *DNSServer, domain
fqdnStr, fqdn := extractFQDN(r)

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) {
fqdnStr, fqdn := extractFQDN(r)

Info.Printf("[http] Deleting ID *, IP *, FQDN %s", fqdnStr)
db.DeleteRecords("", fqdn, net.IP{})
server.Zone.DeleteRecords("", fqdn, net.IP{})
})

http.Handle("/", muxRouter)
Expand Down
7 changes: 5 additions & 2 deletions nameserver/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestHttp(t *testing.T) {
dockerIP = "9.8.7.6"
)

zone, err := NewZoneDb(ZoneConfig{})
zone, err := NewZoneDb(ZoneConfig{Domain: testDomain})
wt.AssertNoErr(t, err)
err = zone.Start()
wt.AssertNoErr(t, err)
Expand All @@ -38,8 +38,11 @@ func TestHttp(t *testing.T) {
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
go ServeHTTP(httpListener, "", nil, testDomain, zone)
go ServeHTTP(httpListener, "", srv)

// Ask the http server to add our test address into the database
addrParts := strings.Split(testAddr1, "/")
Expand Down
2 changes: 1 addition & 1 deletion prog/weavedns/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func main() {
}

go SignalHandlerLoop(srv)
go weavedns.ServeHTTP(httpListener, version, srv, domain, zone)
go weavedns.ServeHTTP(httpListener, version, srv)

err = srv.Start()
if err != nil {
Expand Down

0 comments on commit 0ff4fc3

Please sign in to comment.