Files
nerd-monitor/internal/api/api.go
Ducky SSH User 765590a1a8 Initial commit: Nerd Monitor - Cross-platform system monitoring application
Features:
- Multi-platform agents (Linux, macOS, Windows - AMD64 & ARM64)
- Real-time CPU, RAM, and disk usage monitoring
- Responsive web dashboard with live status indicators
- Session-based authentication with secure credentials
- Stale agent detection and removal (6+ months inactive)
- Auto-refresh dashboard (5 second intervals)
- 15-second agent reporting intervals
- Auto-generated agent IDs from hostnames
- In-memory storage (zero database setup)
- Minimal dependencies (Chi router + Templ templating)

Project Structure:
- cmd/: Agent and Server executables
- internal/: API, Auth, Stats, Storage, and UI packages
- views/: Templ templates for dashboard UI
- Makefile: Build automation for all platforms

Ready for deployment with comprehensive documentation:
- README.md: Full project documentation
- QUICKSTART.md: Getting started guide
- AGENTS.md: Development guidelines
2025-12-20 04:51:12 +00:00

76 lines
1.9 KiB
Go

package api
import (
"encoding/json"
"nerd-monitor/internal/stats"
"nerd-monitor/internal/store"
"net/http"
)
// Handler manages HTTP requests.
type Handler struct {
store *store.Store
}
// New creates a new API handler.
func New(s *store.Store) *Handler {
return &Handler{store: s}
}
// ReportStats handles agent stats reports.
func (h *Handler) ReportStats(w http.ResponseWriter, r *http.Request) {
agentID := r.URL.Query().Get("id")
if agentID == "" {
http.Error(w, "missing agent id", http.StatusBadRequest)
return
}
var stat stats.Stats
if err := json.NewDecoder(r.Body).Decode(&stat); err != nil {
http.Error(w, "invalid request body", http.StatusBadRequest)
return
}
h.store.UpdateAgent(agentID, &stat)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
}
// GetAgent returns stats for a single agent.
func (h *Handler) GetAgent(w http.ResponseWriter, r *http.Request) {
agentID := r.URL.Query().Get("id")
if agentID == "" {
http.Error(w, "missing agent id", http.StatusBadRequest)
return
}
agent := h.store.GetAgent(agentID)
if agent == nil {
http.Error(w, "agent not found", http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(agent)
}
// ListAgents returns all agents.
func (h *Handler) ListAgents(w http.ResponseWriter, r *http.Request) {
agents := h.store.GetAllAgents()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(agents)
}
// DeleteAgent removes an agent.
func (h *Handler) DeleteAgent(w http.ResponseWriter, r *http.Request) {
agentID := r.URL.Query().Get("id")
if agentID == "" {
http.Error(w, "missing agent id", http.StatusBadRequest)
return
}
h.store.DeleteAgent(agentID)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"status": "deleted"})
}