diff --git a/http_server.go b/http_server.go index 623170c..d9d8920 100644 --- a/http_server.go +++ b/http_server.go @@ -9,24 +9,27 @@ import ( "time" ) -// HTTPServer owns the health endpoint listener and server. +// HealthServer owns the `/health` listener and server. // // server, err := service.ServeHTTPHealth("127.0.0.1", 5554) // defer func() { _ = server.Close() }() // fmt.Println("health at", server.Address()) -type HTTPServer struct { +type HealthServer struct { listener net.Listener server *http.Server } -func (server *HTTPServer) Address() string { +// HTTPServer is kept as a compatibility alias for HealthServer. +type HTTPServer = HealthServer + +func (server *HealthServer) Address() string { if server == nil || server.listener == nil { return "" } return server.listener.Addr().String() } -func (server *HTTPServer) Close() error { +func (server *HealthServer) Close() error { if server == nil { return nil } @@ -52,7 +55,7 @@ func (server *HTTPServer) Close() error { // server, err := service.ServeHTTPHealth("127.0.0.1", 5554) // defer func() { _ = server.Close() }() // resp, _ := http.Get("http://" + server.Address() + "/health") -func (service *Service) ServeHTTPHealth(bind string, port int) (*HTTPServer, error) { +func (service *Service) ServeHTTPHealth(bind string, port int) (*HealthServer, error) { if bind == "" { bind = "127.0.0.1" } @@ -78,7 +81,7 @@ func (service *Service) ServeHTTPHealth(bind string, port int) (*HTTPServer, err Handler: mux, } - httpServer := &HTTPServer{ + httpServer := &HealthServer{ listener: listener, server: server, } diff --git a/serve.go b/serve.go index 0bdcc9c..c21d294 100644 --- a/serve.go +++ b/serve.go @@ -23,14 +23,14 @@ type DNSServer struct { tcpServer *dnsprotocol.Server } -// ServiceRuntime owns the DNS and HTTP listeners created by ServeAll. +// ServiceRuntime owns the DNS and health listeners created by ServeAll. // // runtime, err := service.ServeAll("127.0.0.1", 53, 5554) // defer func() { _ = runtime.Close() }() -// fmt.Println(runtime.DNSAddress(), runtime.HTTPAddress()) +// fmt.Println(runtime.DNSAddress(), runtime.HealthAddress()) type ServiceRuntime struct { DNS *DNSServer - HTTP *HTTPServer + HTTP *HealthServer } func (runtime *ServiceRuntime) DNSAddress() string { @@ -40,13 +40,18 @@ func (runtime *ServiceRuntime) DNSAddress() string { return runtime.DNS.Address() } -func (runtime *ServiceRuntime) HTTPAddress() string { +func (runtime *ServiceRuntime) HealthAddress() string { if runtime == nil || runtime.HTTP == nil { return "" } return runtime.HTTP.Address() } +// HTTPAddress is retained for compatibility with older call sites. +func (runtime *ServiceRuntime) HTTPAddress() string { + return runtime.HealthAddress() +} + func (runtime *ServiceRuntime) Close() error { if runtime == nil { return nil @@ -140,7 +145,7 @@ func (service *Service) Serve(bind string, port int) (*DNSServer, error) { // // runtime, err := service.ServeAll("127.0.0.1", 53, 5554) // defer func() { _ = runtime.Close() }() -// fmt.Println("dns:", runtime.DNSAddress(), "health:", runtime.HTTPAddress()) +// fmt.Println("dns:", runtime.DNSAddress(), "health:", runtime.HealthAddress()) func (service *Service) ServeAll(bind string, dnsPort int, httpPort int) (*ServiceRuntime, error) { if dnsPort == 0 { dnsPort = service.dnsPort diff --git a/service_test.go b/service_test.go index 54c1ca2..ba55488 100644 --- a/service_test.go +++ b/service_test.go @@ -461,11 +461,14 @@ func TestServiceServeAllStartsDNSAndHTTPTogether(t *testing.T) { if runtime.DNSAddress() == "" { t.Fatal("expected DNS address from combined runtime") } - if runtime.HTTPAddress() == "" { - t.Fatal("expected HTTP address from combined runtime") + if runtime.HealthAddress() == "" { + t.Fatal("expected health address from combined runtime") + } + if runtime.HTTPAddress() != runtime.HealthAddress() { + t.Fatalf("expected HTTPAddress and HealthAddress to match, got %q and %q", runtime.HTTPAddress(), runtime.HealthAddress()) } - response, err := http.Get("http://" + runtime.HTTPAddress() + "/health") + response, err := http.Get("http://" + runtime.HealthAddress() + "/health") if err != nil { t.Fatalf("expected combined HTTP health endpoint to respond: %v", err) } @@ -519,7 +522,7 @@ func TestServiceServeConfiguredUsesPortsFromServiceOptions(t *testing.T) { t.Fatalf("expected configured DNS port %d, got %s", dnsPort, dnsActualPort) } - _, httpActualPort, err := net.SplitHostPort(runtime.HTTPAddress()) + _, httpActualPort, err := net.SplitHostPort(runtime.HealthAddress()) if err != nil { t.Fatalf("expected HTTP address to parse: %v", err) }