Files
cloud-ip-validator/internal/httpapi/handlers_registry.go
T

59 lines
1.8 KiB
Go
Raw Normal View History

package httpapi
import (
"net/http"
"cloudipvalidator/internal/db"
)
// handleAdminRegistry lists every address ever submitted to the check
// queue, each with a summary of its accumulated check history — the
// durable record that survives an address being deleted from ip_queue and
// later re-added. See migrations/0007_ip_registry.sql.
func (s *Server) handleAdminRegistry(w http.ResponseWriter, r *http.Request) {
items, err := s.DB.ListRegistry(r.Context())
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
out := make([]registryDTO, len(items))
for i, it := range items {
out[i] = registrySummaryToDTO(it)
}
writeJSON(w, http.StatusOK, out)
}
// handleAdminRegistryHistory returns one address's registry record plus its
// full retained check history (across every cycle still kept — see
// db.PruneRegistryHistory / settings.history_retention_cycles), newest
// cycle first.
func (s *Server) handleAdminRegistryHistory(w http.ResponseWriter, r *http.Request) {
address := r.PathValue("ip")
summary, err := s.DB.GetRegistryByAddress(r.Context(), address)
if err != nil {
writeDBError(w, err)
return
}
checks, err := s.DB.ListChecksForRegistry(r.Context(), summary.ID, nil)
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
writeJSON(w, http.StatusOK, struct {
Registry registryDTO `json:"registry"`
Checks []db.Check `json:"checks"`
}{registrySummaryToDTO(*summary), checks})
}
func registrySummaryToDTO(s db.RegistrySummary) registryDTO {
return registryDTO{
IPAddress: s.IPAddress,
FirstSeenAt: s.FirstSeenAt,
LastSeenAt: s.LastSeenAt,
TotalCycles: s.TotalCycles,
LastResult: s.LastResult,
LastCheckedAt: s.LastCheckedAt,
InQueue: s.InQueue,
CurrentState: s.CurrentState,
}
}