Something went wrong. Try again.
Simple homelab monitoring service
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138package web
import ( "encoding/json" "log/slog" "net/http" "strconv"
"tangled.org/chrismwalker.net/mon/models")
// health is a basic health endpoint, callable via monitoring scripts.// This is a very basic implementation which simply returns 204 (No// Content) to the caller.func (a *app) health(w http.ResponseWriter, r *http.Request) { // TODO. w.WriteHeader(http.StatusNoContent)}
// index returns a graphical overview of service statuses.func (a *app) index(w http.ResponseWriter, r *http.Request) { // TODO. w.WriteHeader(http.StatusNoContent)}
func (a *app) createServices(w http.ResponseWriter, r *http.Request) { var services []models.Service err := json.NewDecoder(r.Body).Decode(&services) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return }
if err := a.svc.CreateServices(services); err != nil { a.serverError(w, r, err) return }
w.WriteHeader(http.StatusNoContent)
}
// listServices returns a list of defined services.func (a *app) listServices(w http.ResponseWriter, r *http.Request) { list, err := a.svc.ListServices() if err != nil { a.serverError(w, r, err) return }
w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) if err := json.NewEncoder(w).Encode(list); err != nil { a.serverError(w, r, err) return }}
// getService gets an individual service by ID.func (a *app) getService(w http.ResponseWriter, r *http.Request) { val := r.PathValue("id") id, err := strconv.Atoi(val) if err != nil { a.logger.Error("unable to parse path ID", slog.String("id", val), slog.Any("error", err)) a.serverError(w, r, err) return } svc, err := a.svc.GetService(id) if err != nil { a.serverError(w, r, err) return }
w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) if err := json.NewEncoder(w).Encode(svc); err != nil { a.serverError(w, r, err) return }}
func (a *app) updateService(w http.ResponseWriter, r *http.Request) { a.logger.Info("updateService called") val := r.PathValue("id") id, err := strconv.Atoi(val) if err != nil { a.logger.Error("unable to parse path ID", slog.String("id", val), slog.Any("error", err)) a.serverError(w, r, err) return }
var service models.Service err = json.NewDecoder(r.Body).Decode(&service) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } service.ID = id a.logger.Debug("Parsed service", slog.Any("service", service))
if err := a.svc.UpdateService(service); err != nil { a.serverError(w, r, err) return }
w.WriteHeader(http.StatusNoContent)
}
func (a *app) getStatusList(w http.ResponseWriter, r *http.Request) { val := r.URL.Query().Get("limit") limit, err := strconv.Atoi(val) if err != nil { a.logger.Error("unable to limit query parameter", slog.String("limit", val), slog.Any("error", err)) a.serverError(w, r, err) return }
statuses, err := a.svc.GetStatusList(limit) if err != nil { a.serverError(w, r, err) return }
w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) if err := json.NewEncoder(w).Encode(statuses); err != nil { a.serverError(w, r, err) return }}