Full project structure following upaas conventions: uber/fx DI, go-chi routing, slog logging, Viper config. State persisted as JSON file with per-nameserver record tracking for inconsistency detection. Stub implementations for resolver, portcheck, tlscheck, and watcher.
59 lines
1.2 KiB
Go
59 lines
1.2 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/globals"
|
|
"sneak.berlin/go/dnswatcher/internal/healthcheck"
|
|
"sneak.berlin/go/dnswatcher/internal/logger"
|
|
)
|
|
|
|
// Params contains dependencies for Handlers.
|
|
type Params struct {
|
|
fx.In
|
|
|
|
Logger *logger.Logger
|
|
Globals *globals.Globals
|
|
Healthcheck *healthcheck.Healthcheck
|
|
}
|
|
|
|
// Handlers provides HTTP request handlers.
|
|
type Handlers struct {
|
|
log *slog.Logger
|
|
params *Params
|
|
globals *globals.Globals
|
|
hc *healthcheck.Healthcheck
|
|
}
|
|
|
|
// 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,
|
|
}, nil
|
|
}
|
|
|
|
func (h *Handlers) respondJSON(
|
|
writer http.ResponseWriter,
|
|
_ *http.Request,
|
|
data any,
|
|
status int,
|
|
) {
|
|
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)
|
|
}
|
|
}
|
|
}
|