All checks were successful
check / check (push) Successful in 46s
Implement the two API endpoints documented in the README that were previously returning 404: - GET /api/v1/domains: Returns configured domains with their discovered nameservers, last check time, and status (ok/pending). - GET /api/v1/hostnames: Returns configured hostnames with per-nameserver DNS records, status, and last check time. Both endpoints read from the existing state store and config, requiring no new dependencies. Nameserver results in the hostnames endpoint are sorted for deterministic output. Closes #67
61 lines
1.2 KiB
Go
61 lines
1.2 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
// domainResponse represents a single domain in the API response.
|
|
type domainResponse struct {
|
|
Domain string `json:"domain"`
|
|
Nameservers []string `json:"nameservers,omitempty"`
|
|
LastChecked string `json:"lastChecked,omitempty"`
|
|
Status string `json:"status"`
|
|
}
|
|
|
|
// domainsResponse is the top-level response for GET /api/v1/domains.
|
|
type domainsResponse struct {
|
|
Domains []domainResponse `json:"domains"`
|
|
}
|
|
|
|
// HandleDomains returns the configured domains and their status.
|
|
func (h *Handlers) HandleDomains() http.HandlerFunc {
|
|
return func(
|
|
writer http.ResponseWriter,
|
|
request *http.Request,
|
|
) {
|
|
configured := h.config.Domains
|
|
snapshot := h.state.GetSnapshot()
|
|
|
|
domains := make(
|
|
[]domainResponse, 0, len(configured),
|
|
)
|
|
|
|
for _, domain := range configured {
|
|
dr := domainResponse{
|
|
Domain: domain,
|
|
Status: "pending",
|
|
}
|
|
|
|
ds, ok := snapshot.Domains[domain]
|
|
if ok {
|
|
dr.Nameservers = ds.Nameservers
|
|
dr.Status = "ok"
|
|
|
|
if !ds.LastChecked.IsZero() {
|
|
dr.LastChecked = ds.LastChecked.
|
|
Format(time.RFC3339)
|
|
}
|
|
}
|
|
|
|
domains = append(domains, dr)
|
|
}
|
|
|
|
h.respondJSON(
|
|
writer, request,
|
|
&domainsResponse{Domains: domains},
|
|
http.StatusOK,
|
|
)
|
|
}
|
|
}
|