Add logging and fix /agents/ route error
All checks were successful
Build and Release / build (push) Successful in 35s

This commit is contained in:
Ducky SSH User
2025-12-20 07:34:02 +00:00
parent 761b91b031
commit 50dcfcdc83
9 changed files with 158 additions and 65 deletions

View File

@@ -2,9 +2,12 @@ package api
import (
"encoding/json"
"log/slog"
"net/http"
"github.com/go-chi/chi/v5"
"nerd-monitor/internal/stats"
"nerd-monitor/internal/store"
"net/http"
)
// Handler manages HTTP requests.
@@ -19,86 +22,115 @@ func New(s *store.Store) *Handler {
// ReportStats handles agent stats reports.
func (h *Handler) ReportStats(w http.ResponseWriter, r *http.Request) {
slog.Debug("Incoming stats report request", "method", r.Method, "path", r.URL.Path, "remoteAddr", r.RemoteAddr)
agentID := r.URL.Query().Get("id")
if agentID == "" {
slog.Warn("Missing agent ID in stats report", "remoteAddr", r.RemoteAddr)
http.Error(w, "missing agent id", http.StatusBadRequest)
return
}
var stat stats.Stats
if err := json.NewDecoder(r.Body).Decode(&stat); err != nil {
slog.Error("Invalid request body in stats report", "agentID", agentID, "error", err)
http.Error(w, "invalid request body", http.StatusBadRequest)
return
}
slog.Debug("Updating agent stats", "agentID", agentID, "hostname", stat.Hostname, "cpu", stat.CPUUsage)
h.store.UpdateAgent(agentID, &stat)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
slog.Debug("Stats report processed successfully", "agentID", agentID)
}
// GetAgent returns stats for a single agent.
func (h *Handler) GetAgent(w http.ResponseWriter, r *http.Request) {
slog.Debug("Incoming get agent request", "method", r.Method, "path", r.URL.Path, "remoteAddr", r.RemoteAddr)
agentID := r.URL.Query().Get("id")
if agentID == "" {
slog.Warn("Missing agent ID in get agent request", "remoteAddr", r.RemoteAddr)
http.Error(w, "missing agent id", http.StatusBadRequest)
return
}
slog.Debug("Retrieving agent stats", "agentID", agentID)
agent := h.store.GetAgent(agentID)
if agent == nil {
slog.Warn("Agent not found", "agentID", agentID)
http.Error(w, "agent not found", http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(agent)
slog.Debug("Agent stats retrieved successfully", "agentID", agentID)
}
// ListAgents returns all agents.
func (h *Handler) ListAgents(w http.ResponseWriter, r *http.Request) {
slog.Debug("Incoming list agents request", "method", r.Method, "path", r.URL.Path, "remoteAddr", r.RemoteAddr)
slog.Debug("Retrieving all agents")
agents := h.store.GetAllAgents()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(agents)
slog.Debug("All agents listed successfully", "count", len(agents))
}
// DeleteAgent removes an agent.
func (h *Handler) DeleteAgent(w http.ResponseWriter, r *http.Request) {
slog.Debug("Incoming delete agent request", "method", r.Method, "path", r.URL.Path, "remoteAddr", r.RemoteAddr)
agentID := r.URL.Query().Get("id")
if agentID == "" {
slog.Warn("Missing agent ID in delete agent request", "remoteAddr", r.RemoteAddr)
http.Error(w, "missing agent id", http.StatusBadRequest)
return
}
slog.Info("Deleting agent", "agentID", agentID)
h.store.DeleteAgent(agentID)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"status": "deleted"})
slog.Info("Agent deleted successfully", "agentID", agentID)
}
// UpdateHostname updates the hostname for an agent.
func (h *Handler) UpdateHostname(w http.ResponseWriter, r *http.Request) {
slog.Debug("Incoming update hostname request", "method", r.Method, "path", r.URL.Path, "remoteAddr", r.RemoteAddr)
if r.Method != http.MethodPost {
slog.Warn("Invalid method for update hostname", "method", r.Method)
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
agentID := r.PathValue("id")
agentID := chi.URLParam(r, "id")
if agentID == "" {
slog.Warn("Missing agent ID in update hostname request", "remoteAddr", r.RemoteAddr)
http.Error(w, "missing agent id", http.StatusBadRequest)
return
}
hostname := r.FormValue("hostname")
if hostname == "" {
slog.Warn("Missing hostname in update request", "agentID", agentID)
http.Error(w, "missing hostname", http.StatusBadRequest)
return
}
slog.Info("Updating agent hostname", "agentID", agentID, "newHostname", hostname)
err := h.store.UpdateHostname(agentID, hostname)
if err != nil {
slog.Warn("Failed to update hostname - agent not found", "agentID", agentID, "error", err)
http.Error(w, "agent not found", http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"status": "updated"})
slog.Info("Agent hostname updated successfully", "agentID", agentID, "hostname", hostname)
}