refactor: harden diagnostics and consolidate runtime code
This commit is contained in:
@@ -348,6 +348,7 @@ func NewHandler(opts HandlerOptions) http.Handler {
|
||||
mux.HandleFunc("POST /api/system/install-to-ram", h.handleAPIInstallToRAM)
|
||||
mux.HandleFunc("POST /api/system/reboot", h.handleAPISystemReboot)
|
||||
mux.HandleFunc("POST /api/system/shutdown", h.handleAPISystemShutdown)
|
||||
mux.HandleFunc("GET /api/system/time", h.handleAPISystemTime)
|
||||
mux.HandleFunc("POST /api/system/time-sync", h.handleAPISystemTimeSync)
|
||||
|
||||
// Preflight
|
||||
@@ -813,3 +814,19 @@ func writeError(w http.ResponseWriter, status int, msg string) {
|
||||
w.WriteHeader(status)
|
||||
_ = json.NewEncoder(w).Encode(map[string]string{"error": msg})
|
||||
}
|
||||
|
||||
func writeAPIListResponse[T any](w http.ResponseWriter, configured bool, load func() ([]T, error)) {
|
||||
if !configured {
|
||||
writeError(w, http.StatusServiceUnavailable, "app not configured")
|
||||
return
|
||||
}
|
||||
items, err := load()
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
if items == nil {
|
||||
items = []T{}
|
||||
}
|
||||
writeJSON(w, items)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user