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

@@ -1,42 +1,20 @@
# Nerd Monitor - Agent Guidelines # Nerd Monitor - Agent Guidelines
## Build & Test Commands ## Build & Test Commands
- **Generate templates**: `make templ`
- **Build**: `make build` (server + agent) or `make build-server`/`make build-agent`
- **Cross-platform**: `make build-all`
- **Clean**: `make clean`
- **Test all**: `go test ./...`
- **Test single**: `go test -run TestName ./package`
- **Format**: `gofmt -w ./cmd ./internal ./views`
```bash Note: Go 1.23+ project using Templ templating. Run `make templ` before building.
# Generate Templ templates (required before building)
make templ
# Build server and agent for current OS
make build
# Build individual components
make build-server
make build-agent
# Build for all platforms (Linux, macOS, Windows)
make build-all
# Clean build artifacts
make clean
```
Note: This Go 1.21 project uses Templ for templating. Always run `make templ` before building.
## Code Style Guidelines ## Code Style Guidelines
- **Imports**: stdlib → third-party → internal (alphabetical within groups)
**Imports**: Standard library first, then third-party packages, then internal modules. Organize alphabetically within each group. - **Formatting**: `gofmt` standards, explicit exported struct comments
- **Naming**: CamelCase exports, lowercase unexported, single-letter receivers (e.g., `(h *Handler)`)
**Formatting & Types**: Use `gofmt` standards. Explicit struct field comments for exported types. Prefer `error` return values (no panic for recoverable errors). - **Error Handling**: Explicit checks, `http.Error()` for HTTP, contextual wrapping
- **Concurrency**: `sync.RWMutex` for shared state, defer lock releases, small critical sections
**Naming**: CamelCase for exported symbols, lowercase for unexported. Receiver names as single letters (e.g., `(m *Manager)`). Constants use ALL_CAPS_SNAKE_CASE. - **HTTP**: Chi router, `func (h *Handler) Method(w http.ResponseWriter, r *http.Request)`, set Content-Type, validate early
**Error Handling**: Define sentinel errors in dedicated files (e.g., `auth/errors.go`). Check errors explicitly; use `http.Error(w, "message", status)` for HTTP responses. Wrap errors with context when needed.
**Project Structure**:
- `cmd/`: Executable entry points (server, agent)
- `internal/`: Packages (api, auth, stats, store, ui)
- `views/`: Templ templates (*.templ files)
**Concurrency**: Use `sync.RWMutex` for shared state. Always defer lock releases. Keep critical sections small.
**HTTP**: Use Chi router. Handlers follow `func (h *Handler) Name(w http.ResponseWriter, r *http.Request)`. Set Content-Type headers explicitly. Validate query parameters early and return 400/404 appropriately.

BIN
agent Executable file

Binary file not shown.

View File

@@ -5,7 +5,7 @@ import (
"encoding/json" "encoding/json"
"flag" "flag"
"fmt" "fmt"
"log" "log/slog"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@@ -25,8 +25,12 @@ func main() {
) )
flag.Parse() flag.Parse()
// Set up verbose logging
slog.SetLogLoggerLevel(slog.LevelDebug)
if *server == "" { if *server == "" {
log.Fatal("--server flag is required") slog.Error("Server flag is required")
os.Exit(1)
} }
// Normalize server URL (add http:// if missing) // Normalize server URL (add http:// if missing)
@@ -38,12 +42,13 @@ func main() {
var err error var err error
id, err = generateAgentID() id, err = generateAgentID()
if err != nil { if err != nil {
log.Fatalf("Failed to generate agent ID: %v", err) slog.Error("Failed to generate agent ID", "error", err)
os.Exit(1)
} }
} }
log.Printf("Starting agent with ID: %s\n", id) slog.Info("Starting agent", "id", id)
log.Printf("Reporting to: %s every %v\n", *server, *interval) slog.Info("Reporting configuration", "server", *server, "interval", *interval)
// Initialize stats collector // Initialize stats collector
collector := stats.NewCollector() collector := stats.NewCollector()
@@ -64,7 +69,7 @@ func main() {
case <-ticker.C: case <-ticker.C:
reportStats(id, *server, collector) reportStats(id, *server, collector)
case <-sigChan: case <-sigChan:
log.Println("Agent shutting down gracefully...") slog.Info("Agent shutting down gracefully")
os.Exit(0) os.Exit(0)
} }
} }
@@ -79,37 +84,39 @@ func reportStats(agentID, serverURL string, collector *stats.Collector) {
stat, err := collector.Collect(hostname) stat, err := collector.Collect(hostname)
if err != nil { if err != nil {
log.Printf("Error collecting stats: %v", err) slog.Error("Error collecting stats", "agentID", agentID, "hostname", hostname, "error", err)
return return
} }
// Marshal to JSON // Marshal to JSON
body, err := json.Marshal(stat) body, err := json.Marshal(stat)
if err != nil { if err != nil {
log.Printf("Error marshaling stats: %v", err) slog.Error("Error marshaling stats", "agentID", agentID, "error", err)
return return
} }
// Send to server // Send to server
reportURL := fmt.Sprintf("%s/api/report?id=%s", serverURL, agentID) reportURL := fmt.Sprintf("%s/api/report?id=%s", serverURL, agentID)
slog.Debug("Sending stats report", "agentID", agentID, "url", reportURL, "bodySize", len(body))
resp, err := http.Post(reportURL, "application/json", bytes.NewReader(body)) resp, err := http.Post(reportURL, "application/json", bytes.NewReader(body))
if err != nil { if err != nil {
log.Printf("Error reporting stats: %v", err) slog.Error("Error reporting stats", "agentID", agentID, "url", reportURL, "error", err)
return return
} }
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
log.Printf("Server returned status %d", resp.StatusCode) slog.Error("Server returned non-OK status", "agentID", agentID, "statusCode", resp.StatusCode, "url", reportURL)
return return
} }
log.Printf("Stats reported: CPU %.1f%% | RAM %s / %s | Disk %s / %s", slog.Debug("Stats reported successfully",
stat.CPUUsage, "agentID", agentID,
formatBytes(stat.RAMUsage), "cpu", stat.CPUUsage,
formatBytes(stat.RAMTotal), "ramUsage", formatBytes(stat.RAMUsage),
formatBytes(stat.DiskUsage), "ramTotal", formatBytes(stat.RAMTotal),
formatBytes(stat.DiskTotal), "diskUsage", formatBytes(stat.DiskUsage),
"diskTotal", formatBytes(stat.DiskTotal),
) )
} }

View File

@@ -2,7 +2,7 @@ package main
import ( import (
"flag" "flag"
"log" "log/slog"
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
@@ -24,6 +24,9 @@ func main() {
) )
flag.Parse() flag.Parse()
// Set up verbose logging
slog.SetLogLoggerLevel(slog.LevelDebug)
// Initialize dependencies // Initialize dependencies
s := store.New() s := store.New()
authMgr := auth.New(*username, *password) authMgr := auth.New(*username, *password)
@@ -63,14 +66,15 @@ func main() {
go func() { go func() {
<-sigChan <-sigChan
log.Println("Shutting down server...") slog.Info("Shutting down server...")
server.Close() server.Close()
os.Exit(0) os.Exit(0)
}() }()
log.Printf("Starting server on http://%s:%s\n", *addr, *port) slog.Info("Starting server", "addr", *addr+":"+*port, "url", "http://"+*addr+":"+*port)
log.Printf("Login with %s / %s\n", *username, *password) slog.Info("Login credentials", "username", *username, "password", *password)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Server error: %v", err) slog.Error("Server error", "error", err)
os.Exit(1)
} }
} }

View File

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

View File

@@ -3,6 +3,7 @@ package auth
import ( import (
"crypto/rand" "crypto/rand"
"encoding/hex" "encoding/hex"
"log/slog"
"net/http" "net/http"
"sync" "sync"
"time" "time"
@@ -36,11 +37,13 @@ func New(username, password string) *Manager {
// Login validates credentials and creates a session. // Login validates credentials and creates a session.
func (m *Manager) Login(username, password string) (string, error) { func (m *Manager) Login(username, password string) (string, error) {
if username != m.username || password != m.password { if username != m.username || password != m.password {
slog.Debug("Login failed - invalid credentials", "username", username)
return "", ErrInvalidCredentials return "", ErrInvalidCredentials
} }
token, err := generateToken() token, err := generateToken()
if err != nil { if err != nil {
slog.Error("Failed to generate session token", "error", err)
return "", err return "", err
} }
@@ -52,6 +55,7 @@ func (m *Manager) Login(username, password string) (string, error) {
ExpiresAt: time.Now().Add(m.expiryDur), ExpiresAt: time.Now().Add(m.expiryDur),
} }
slog.Debug("Login successful, session created", "username", username, "token", token[:8]+"...")
return token, nil return token, nil
} }
@@ -62,10 +66,17 @@ func (m *Manager) Validate(token string) bool {
session, ok := m.sessions[token] session, ok := m.sessions[token]
if !ok { if !ok {
slog.Debug("Session validation failed - token not found", "token", token[:8]+"...")
return false return false
} }
return session.ExpiresAt.After(time.Now()) if !session.ExpiresAt.After(time.Now()) {
slog.Debug("Session validation failed - token expired", "token", token[:8]+"...", "expiredAt", session.ExpiresAt)
return false
}
slog.Debug("Session validation successful", "token", token[:8]+"...")
return true
} }
// Logout invalidates a session. // Logout invalidates a session.
@@ -74,16 +85,24 @@ func (m *Manager) Logout(token string) {
defer m.mu.Unlock() defer m.mu.Unlock()
delete(m.sessions, token) delete(m.sessions, token)
slog.Debug("Session logged out", "token", token[:8]+"...")
} }
// Middleware returns a Chi middleware for authentication. // Middleware returns a Chi middleware for authentication.
func (m *Manager) Middleware(next http.Handler) http.Handler { func (m *Manager) Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("session_token") cookie, err := r.Cookie("session_token")
if err != nil || !m.Validate(cookie.Value) { if err != nil {
slog.Debug("Authentication failed - no session cookie", "path", r.URL.Path, "remoteAddr", r.RemoteAddr)
http.Redirect(w, r, "/login", http.StatusSeeOther) http.Redirect(w, r, "/login", http.StatusSeeOther)
return return
} }
if !m.Validate(cookie.Value) {
slog.Debug("Authentication failed - invalid session", "path", r.URL.Path, "remoteAddr", r.RemoteAddr)
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
slog.Debug("Authentication successful", "path", r.URL.Path, "remoteAddr", r.RemoteAddr)
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}) })
} }

View File

@@ -2,6 +2,7 @@ package store
import ( import (
"errors" "errors"
"log/slog"
"sync" "sync"
"time" "time"
@@ -53,6 +54,7 @@ func (s *Store) UpdateAgent(id string, stat *stats.Stats) {
DiskTotal: stat.DiskTotal, DiskTotal: stat.DiskTotal,
LastSeen: time.Now(), LastSeen: time.Now(),
} }
slog.Debug("Agent stats updated", "agentID", id, "hostname", stat.Hostname, "cpu", stat.CPUUsage)
} }
// GetAgent retrieves agent stats by ID. // GetAgent retrieves agent stats by ID.
@@ -60,7 +62,13 @@ func (s *Store) GetAgent(id string) *AgentStats {
s.mu.RLock() s.mu.RLock()
defer s.mu.RUnlock() defer s.mu.RUnlock()
return s.agents[id] agent := s.agents[id]
if agent != nil {
slog.Debug("Agent retrieved", "agentID", id, "hostname", agent.Hostname)
} else {
slog.Debug("Agent not found", "agentID", id)
}
return agent
} }
// GetAllAgents returns all agents. // GetAllAgents returns all agents.
@@ -72,6 +80,7 @@ func (s *Store) GetAllAgents() []*AgentStats {
for _, agent := range s.agents { for _, agent := range s.agents {
agents = append(agents, agent) agents = append(agents, agent)
} }
slog.Debug("All agents retrieved", "count", len(agents))
return agents return agents
} }
@@ -81,6 +90,7 @@ func (s *Store) DeleteAgent(id string) {
defer s.mu.Unlock() defer s.mu.Unlock()
delete(s.agents, id) delete(s.agents, id)
slog.Debug("Agent deleted", "agentID", id)
} }
// UpdateHostname updates the hostname for an agent. // UpdateHostname updates the hostname for an agent.
@@ -90,9 +100,11 @@ func (s *Store) UpdateHostname(id string, hostname string) error {
agent, exists := s.agents[id] agent, exists := s.agents[id]
if !exists { if !exists {
slog.Debug("Agent not found for hostname update", "agentID", id)
return ErrAgentNotFound return ErrAgentNotFound
} }
agent.Hostname = hostname agent.Hostname = hostname
slog.Debug("Agent hostname updated", "agentID", id, "hostname", hostname)
return nil return nil
} }

View File

@@ -2,9 +2,11 @@ package ui
import ( import (
"context" "context"
"log/slog"
"net/http" "net/http"
"time" "time"
"github.com/go-chi/chi/v5"
"nerd-monitor/internal/auth" "nerd-monitor/internal/auth"
"nerd-monitor/internal/store" "nerd-monitor/internal/store"
"nerd-monitor/views" "nerd-monitor/views"
@@ -26,30 +28,42 @@ func New(s *store.Store, a *auth.Manager) *Handler {
// Dashboard renders the dashboard page. // Dashboard renders the dashboard page.
func (h *Handler) Dashboard(w http.ResponseWriter, r *http.Request) { func (h *Handler) Dashboard(w http.ResponseWriter, r *http.Request) {
slog.Debug("Incoming dashboard request", "method", r.Method, "path", r.URL.Path, "remoteAddr", r.RemoteAddr)
slog.Debug("Rendering dashboard", "agentCount", len(h.store.GetAllAgents()), "staleAgentCount", len(h.getStaleAgents()))
agents := h.store.GetAllAgents() agents := h.store.GetAllAgents()
staleAgents := h.getStaleAgents() staleAgents := h.getStaleAgents()
component := views.Dashboard(agents, staleAgents) component := views.Dashboard(agents, staleAgents)
component.Render(context.Background(), w) component.Render(context.Background(), w)
slog.Debug("Dashboard rendered successfully")
} }
// AgentDetail renders the agent detail page. // AgentDetail renders the agent detail page.
func (h *Handler) AgentDetail(w http.ResponseWriter, r *http.Request) { func (h *Handler) AgentDetail(w http.ResponseWriter, r *http.Request) {
agentID := r.PathValue("id") slog.Debug("Incoming agent detail request", "method", r.Method, "path", r.URL.Path, "remoteAddr", r.RemoteAddr)
agentID := chi.URLParam(r, "id")
slog.Debug("Retrieving agent detail", "agentID", agentID)
agent := h.store.GetAgent(agentID) agent := h.store.GetAgent(agentID)
if agent == nil { if agent == nil {
slog.Warn("Agent not found for detail view", "agentID", agentID)
http.NotFound(w, r) http.NotFound(w, r)
return return
} }
component := views.AgentDetail(agent) component := views.AgentDetail(agent)
component.Render(context.Background(), w) component.Render(context.Background(), w)
slog.Debug("Agent detail rendered successfully", "agentID", agentID)
} }
// Login renders the login page. // Login renders the login page.
func (h *Handler) Login(w http.ResponseWriter, r *http.Request) { func (h *Handler) Login(w http.ResponseWriter, r *http.Request) {
slog.Debug("Incoming login request", "method", r.Method, "path", r.URL.Path, "remoteAddr", r.RemoteAddr)
if r.Method == http.MethodGet { if r.Method == http.MethodGet {
slog.Debug("Rendering login page")
component := views.LoginPage("") component := views.LoginPage("")
component.Render(context.Background(), w) component.Render(context.Background(), w)
return return
@@ -59,13 +73,16 @@ func (h *Handler) Login(w http.ResponseWriter, r *http.Request) {
username := r.FormValue("username") username := r.FormValue("username")
password := r.FormValue("password") password := r.FormValue("password")
slog.Debug("Attempting login", "username", username, "remoteAddr", r.RemoteAddr)
token, err := h.auth.Login(username, password) token, err := h.auth.Login(username, password)
if err != nil { if err != nil {
slog.Warn("Login failed - invalid credentials", "username", username, "remoteAddr", r.RemoteAddr, "error", err)
component := views.LoginPage("Invalid credentials") component := views.LoginPage("Invalid credentials")
component.Render(context.Background(), w) component.Render(context.Background(), w)
return return
} }
slog.Info("Login successful", "username", username, "remoteAddr", r.RemoteAddr)
http.SetCookie(w, &http.Cookie{ http.SetCookie(w, &http.Cookie{
Name: "session_token", Name: "session_token",
Value: token, Value: token,
@@ -79,19 +96,26 @@ func (h *Handler) Login(w http.ResponseWriter, r *http.Request) {
return return
} }
slog.Warn("Invalid method for login", "method", r.Method)
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
} }
// Logout handles logout. // Logout handles logout.
func (h *Handler) Logout(w http.ResponseWriter, r *http.Request) { func (h *Handler) Logout(w http.ResponseWriter, r *http.Request) {
slog.Debug("Incoming logout request", "method", r.Method, "path", r.URL.Path, "remoteAddr", r.RemoteAddr)
if r.Method != http.MethodPost { if r.Method != http.MethodPost {
slog.Warn("Invalid method for logout", "method", r.Method)
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return return
} }
cookie, err := r.Cookie("session_token") cookie, err := r.Cookie("session_token")
if err == nil { if err == nil {
slog.Info("Logging out user", "remoteAddr", r.RemoteAddr)
h.auth.Logout(cookie.Value) h.auth.Logout(cookie.Value)
} else {
slog.Debug("Logout attempted without valid session", "remoteAddr", r.RemoteAddr)
} }
http.SetCookie(w, &http.Cookie{ http.SetCookie(w, &http.Cookie{
@@ -107,53 +131,70 @@ func (h *Handler) Logout(w http.ResponseWriter, r *http.Request) {
// RemoveStaleAgents handles bulk removal of stale agents. // RemoveStaleAgents handles bulk removal of stale agents.
func (h *Handler) RemoveStaleAgents(w http.ResponseWriter, r *http.Request) { func (h *Handler) RemoveStaleAgents(w http.ResponseWriter, r *http.Request) {
slog.Debug("Incoming remove stale agents request", "method", r.Method, "path", r.URL.Path, "remoteAddr", r.RemoteAddr)
if r.Method != http.MethodPost { if r.Method != http.MethodPost {
slog.Warn("Invalid method for remove stale agents", "method", r.Method)
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return return
} }
agentIDs := r.Form["agent_ids"] agentIDs := r.Form["agent_ids"]
slog.Info("Removing stale agents", "count", len(agentIDs), "agentIDs", agentIDs)
for _, id := range agentIDs { for _, id := range agentIDs {
h.store.DeleteAgent(id) h.store.DeleteAgent(id)
} }
slog.Info("Stale agents removed successfully", "count", len(agentIDs))
http.Redirect(w, r, "/", http.StatusSeeOther) http.Redirect(w, r, "/", http.StatusSeeOther)
} }
// DeleteAgent handles single agent deletion. // DeleteAgent handles single agent deletion.
func (h *Handler) DeleteAgent(w http.ResponseWriter, r *http.Request) { 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)
if r.Method != http.MethodPost { if r.Method != http.MethodPost {
slog.Warn("Invalid method for delete agent", "method", r.Method)
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return return
} }
agentID := r.PathValue("id") agentID := chi.URLParam(r, "id")
slog.Info("Deleting agent", "agentID", agentID)
h.store.DeleteAgent(agentID) h.store.DeleteAgent(agentID)
slog.Info("Agent deleted successfully", "agentID", agentID)
http.Redirect(w, r, "/", http.StatusSeeOther) http.Redirect(w, r, "/", http.StatusSeeOther)
} }
// UpdateAgentHostname handles hostname updates for agents. // UpdateAgentHostname handles hostname updates for agents.
func (h *Handler) UpdateAgentHostname(w http.ResponseWriter, r *http.Request) { func (h *Handler) UpdateAgentHostname(w http.ResponseWriter, r *http.Request) {
slog.Debug("Incoming update agent hostname request", "method", r.Method, "path", r.URL.Path, "remoteAddr", r.RemoteAddr)
if r.Method != http.MethodPost { if r.Method != http.MethodPost {
slog.Warn("Invalid method for update agent hostname", "method", r.Method)
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return return
} }
agentID := r.PathValue("id") agentID := chi.URLParam(r, "id")
hostname := r.FormValue("hostname") hostname := r.FormValue("hostname")
if hostname == "" { if hostname == "" {
slog.Warn("Empty hostname provided for update", "agentID", agentID)
http.Error(w, "Hostname cannot be empty", http.StatusBadRequest) http.Error(w, "Hostname cannot be empty", http.StatusBadRequest)
return return
} }
slog.Info("Updating agent hostname", "agentID", agentID, "newHostname", hostname)
err := h.store.UpdateHostname(agentID, hostname) err := h.store.UpdateHostname(agentID, hostname)
if err != nil { if err != nil {
slog.Warn("Failed to update hostname - agent not found", "agentID", agentID, "error", err)
http.NotFound(w, r) http.NotFound(w, r)
return return
} }
slog.Info("Agent hostname updated successfully", "agentID", agentID, "hostname", hostname)
http.Redirect(w, r, "/agents/"+agentID, http.StatusSeeOther) http.Redirect(w, r, "/agents/"+agentID, http.StatusSeeOther)
} }

BIN
server Executable file

Binary file not shown.