feat(dns): auto-start health server when HTTPPort is configured

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-03 23:58:59 +00:00
parent 50b2394fdd
commit 8807fee752
2 changed files with 56 additions and 0 deletions

View file

@ -310,6 +310,13 @@ func (service *Service) handleServe(ctx context.Context, values map[string]any)
if err != nil {
return nil, false, err
}
if !healthPortProvided && service.httpPort > 0 {
runtime, err := service.ServeAll(bind, port, service.httpPort)
if err != nil {
return nil, false, err
}
return runtime, true, nil
}
if healthPortProvided {
runtime, err := service.ServeAll(bind, port, healthPort)
if err != nil {

View file

@ -2718,6 +2718,55 @@ func TestServiceHandleActionServeHealthPortStartsRuntime(t *testing.T) {
}
}
func TestServiceHandleActionServeDefaultsToConfiguredHTTPPort(t *testing.T) {
httpPort := pickFreeTCPPort(t)
dnsPort := pickFreeTCPPort(t)
service := NewService(ServiceOptions{
DNSPort: dnsPort,
HTTPPort: httpPort,
Records: map[string]NameRecords{
"gateway.charon.lthn": {
A: []string{"10.10.10.10"},
},
},
})
payload, ok, err := service.HandleAction(ActionServe, map[string]any{
"bind": "127.0.0.1",
})
if err != nil {
t.Fatalf("expected serve action to start runtime with default http port: %v", err)
}
if !ok {
t.Fatal("expected serve action to succeed")
}
runtime, ok := payload.(*ServiceRuntime)
if !ok {
t.Fatalf("expected ServiceRuntime payload, got %T", payload)
}
if runtime == nil {
t.Fatal("expected runtime from serve action")
}
defer func() {
_ = runtime.Close()
}()
_, dnsPortStr, err := net.SplitHostPort(runtime.DNSAddress())
if err != nil {
t.Fatalf("expected dns address to include port: %v", err)
}
if dnsPortStr != strconv.Itoa(dnsPort) {
t.Fatalf("expected configured DNS port %d, got %q", dnsPort, dnsPortStr)
}
_, healthPortStr, err := net.SplitHostPort(runtime.HealthAddress())
if err != nil {
t.Fatalf("expected health address to include port: %v", err)
}
if healthPortStr != strconv.Itoa(httpPort) {
t.Fatalf("expected configured health port %d, got %q", httpPort, healthPortStr)
}
}
func TestServiceResolveServePortDefaultsToStandardDNSPort(t *testing.T) {
service := NewService(ServiceOptions{})
if service.ResolveDNSPort() != DefaultDNSPort {