diff --git a/action.go b/action.go index 7599d9b..f5a26be 100644 --- a/action.go +++ b/action.go @@ -292,7 +292,7 @@ func (service *Service) handleServe(ctx context.Context, values map[string]any) return nil, false, err } if !portProvided { - port = service.resolveServePort() + port = service.ResolveDNSPort() } result, err := service.Serve(bind, port) if err != nil { @@ -301,13 +301,6 @@ func (service *Service) handleServe(ctx context.Context, values map[string]any) return result, true, nil } -func (service *Service) resolveServePort() int { - if service == nil || service.dnsPort <= 0 { - return DefaultDNSPort - } - return service.dnsPort -} - func stringActionValue(values map[string]any, key string) (string, error) { if values == nil { return "", errActionMissingValue diff --git a/serve.go b/serve.go index 65c2ddb..cb75643 100644 --- a/serve.go +++ b/serve.go @@ -114,13 +114,53 @@ func (server *DNSServer) Close() error { return err } -func (service *Service) resolveHTTPPort() int { +// ResolveDNSPort returns the DNS port used for `dns.serve` and `Serve`. +// +// port := service.ResolveDNSPort() +// server, err := service.Serve("127.0.0.1", port) +func (service *Service) ResolveDNSPort() int { + if service == nil || service.dnsPort <= 0 { + return DefaultDNSPort + } + return service.dnsPort +} + +// DNSPort is an explicit alias for ResolveDNSPort. +// +// port := service.DNSPort() +// server, err := service.Serve("127.0.0.1", port) +func (service *Service) DNSPort() int { + return service.ResolveDNSPort() +} + +// resolveServePort keeps internal callers aligned with existing behavior. +func (service *Service) resolveServePort() int { + return service.ResolveDNSPort() +} + +// ResolveHTTPPort returns the HTTP health port used by `ServeHTTPHealth`. +// +// port := service.ResolveHTTPPort() +// healthServer, err := service.ServeHTTPHealth("127.0.0.1", port) +func (service *Service) ResolveHTTPPort() int { if service == nil || service.httpPort <= 0 { return DefaultHTTPPort } return service.httpPort } +// HTTPPort is an explicit alias for ResolveHTTPPort. +// +// port := service.HTTPPort() +// healthServer, err := service.ServeHTTPHealth("127.0.0.1", port) +func (service *Service) HTTPPort() int { + return service.ResolveHTTPPort() +} + +func (service *Service) resolveHTTPPort() int { + return service.ResolveHTTPPort() +} + // Serve starts DNS over UDP and TCP. // // srv, err := service.Serve("0.0.0.0", 53) diff --git a/service_test.go b/service_test.go index 4f79651..5845e30 100644 --- a/service_test.go +++ b/service_test.go @@ -2399,31 +2399,55 @@ func TestServiceHandleActionServeDefaultsPortFromServiceConfiguration(t *testing func TestServiceResolveServePortDefaultsToStandardDNSPort(t *testing.T) { service := NewService(ServiceOptions{}) + if service.ResolveDNSPort() != DefaultDNSPort { + t.Fatalf("expected ResolveDNSPort to default to standard DNS port %d, got %d", DefaultDNSPort, service.ResolveDNSPort()) + } + if service.DNSPort() != DefaultDNSPort { + t.Fatalf("expected DNSPort alias to return the standard DNS port %d, got %d", DefaultDNSPort, service.DNSPort()) + } if service.resolveServePort() != DefaultDNSPort { - t.Fatalf("expected serve to default to standard DNS port %d, got %d", DefaultDNSPort, service.resolveServePort()) + t.Fatalf("expected internal resolveServePort helper to return the standard DNS port %d, got %d", DefaultDNSPort, service.resolveServePort()) } customPort := 1053 customService := NewService(ServiceOptions{ DNSPort: customPort, }) + if customService.ResolveDNSPort() != customPort { + t.Fatalf("expected ResolveDNSPort to honor configured DNSPort, got %d", customService.ResolveDNSPort()) + } + if customService.DNSPort() != customPort { + t.Fatalf("expected DNSPort alias to honor configured DNSPort, got %d", customService.DNSPort()) + } if customService.resolveServePort() != customPort { - t.Fatalf("expected resolveServePort to honor configured DNSPort, got %d", customService.resolveServePort()) + t.Fatalf("expected resolveServePort helper to honor configured DNSPort, got %d", customService.resolveServePort()) } } func TestServiceResolveHTTPPortDefaultsToStandardHTTPPort(t *testing.T) { service := NewService(ServiceOptions{}) + if service.ResolveHTTPPort() != DefaultHTTPPort { + t.Fatalf("expected ResolveHTTPPort to default to %d, got %d", DefaultHTTPPort, service.ResolveHTTPPort()) + } + if service.HTTPPort() != DefaultHTTPPort { + t.Fatalf("expected HTTPPort alias to return default %d, got %d", DefaultHTTPPort, service.HTTPPort()) + } if service.resolveHTTPPort() != DefaultHTTPPort { - t.Fatalf("expected resolve HTTP port to default to %d, got %d", DefaultHTTPPort, service.resolveHTTPPort()) + t.Fatalf("expected resolveHTTPPort helper to return default %d, got %d", DefaultHTTPPort, service.resolveHTTPPort()) } customPort := 5555 customService := NewService(ServiceOptions{ HTTPPort: customPort, }) + if customService.ResolveHTTPPort() != customPort { + t.Fatalf("expected ResolveHTTPPort to honor configured HTTPPort, got %d", customService.ResolveHTTPPort()) + } + if customService.HTTPPort() != customPort { + t.Fatalf("expected HTTPPort alias to honor configured HTTPPort, got %d", customService.HTTPPort()) + } if customService.resolveHTTPPort() != customPort { - t.Fatalf("expected resolveHTTPPort to honor configured HTTPPort, got %d", customService.resolveHTTPPort()) + t.Fatalf("expected resolveHTTPPort helper to honor configured HTTPPort, got %d", customService.resolveHTTPPort()) } }