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
67 lines
1.5 KiB
Go
67 lines
1.5 KiB
Go
// Package handlers provides HTTP request handlers.
|
|
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log/slog"
|
|
"net/http"
|
|
|
|
"go.uber.org/fx"
|
|
|
|
"sneak.berlin/go/dnswatcher/internal/config"
|
|
"sneak.berlin/go/dnswatcher/internal/globals"
|
|
"sneak.berlin/go/dnswatcher/internal/healthcheck"
|
|
"sneak.berlin/go/dnswatcher/internal/logger"
|
|
"sneak.berlin/go/dnswatcher/internal/state"
|
|
)
|
|
|
|
// Params contains dependencies for Handlers.
|
|
type Params struct {
|
|
fx.In
|
|
|
|
Logger *logger.Logger
|
|
Globals *globals.Globals
|
|
Healthcheck *healthcheck.Healthcheck
|
|
State *state.State
|
|
Config *config.Config
|
|
}
|
|
|
|
// Handlers provides HTTP request handlers.
|
|
type Handlers struct {
|
|
log *slog.Logger
|
|
params *Params
|
|
globals *globals.Globals
|
|
hc *healthcheck.Healthcheck
|
|
state *state.State
|
|
config *config.Config
|
|
}
|
|
|
|
// New creates a new Handlers instance.
|
|
func New(_ fx.Lifecycle, params Params) (*Handlers, error) {
|
|
return &Handlers{
|
|
log: params.Logger.Get(),
|
|
params: ¶ms,
|
|
globals: params.Globals,
|
|
hc: params.Healthcheck,
|
|
state: params.State,
|
|
config: params.Config,
|
|
}, nil
|
|
}
|
|
|
|
func (h *Handlers) respondJSON(
|
|
writer http.ResponseWriter,
|
|
_ *http.Request,
|
|
data any,
|
|
status int, //nolint:unparam // general-purpose utility; status varies in future use
|
|
) {
|
|
writer.Header().Set("Content-Type", "application/json")
|
|
writer.WriteHeader(status)
|
|
|
|
if data != nil {
|
|
err := json.NewEncoder(writer).Encode(data)
|
|
if err != nil {
|
|
h.log.Error("json encode error", "error", err)
|
|
}
|
|
}
|
|
}
|