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 "*", ""
}
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) {
@@ -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,
@@ -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) {
@@ -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)