Add hostname editing feature to agent detail page

- Create enhanced agent detail template with all stats and hostname edit form
- Add UpdateHostname method to store for agent hostname updates
- Add UpdateAgentHostname handler in UI for web form submissions
- Add UpdateHostname endpoint in API handler for JSON requests
- Register new POST /agents/{id}/hostname route for hostname updates
- Improve agent detail page layout with Settings and Danger Zone sections
- Add error handling for missing agents and empty hostname values
This commit is contained in:
Ducky SSH User
2025-12-20 06:51:27 +00:00
parent 0a37b04506
commit 761b91b031
5 changed files with 107 additions and 6 deletions

View File

@@ -73,3 +73,32 @@ func (h *Handler) DeleteAgent(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"status": "deleted"})
}
// UpdateHostname updates the hostname for an agent.
func (h *Handler) UpdateHostname(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
agentID := r.PathValue("id")
if agentID == "" {
http.Error(w, "missing agent id", http.StatusBadRequest)
return
}
hostname := r.FormValue("hostname")
if hostname == "" {
http.Error(w, "missing hostname", http.StatusBadRequest)
return
}
err := h.store.UpdateHostname(agentID, hostname)
if err != nil {
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"})
}

View File

@@ -1,12 +1,18 @@
package store
import (
"errors"
"sync"
"time"
"nerd-monitor/internal/stats"
)
var (
// ErrAgentNotFound is returned when an agent is not found.
ErrAgentNotFound = errors.New("agent not found")
)
// AgentStats represents the latest stats for an agent.
type AgentStats struct {
ID string
@@ -76,3 +82,17 @@ func (s *Store) DeleteAgent(id string) {
delete(s.agents, id)
}
// UpdateHostname updates the hostname for an agent.
func (s *Store) UpdateHostname(id string, hostname string) error {
s.mu.Lock()
defer s.mu.Unlock()
agent, exists := s.agents[id]
if !exists {
return ErrAgentNotFound
}
agent.Hostname = hostname
return nil
}

View File

@@ -133,6 +133,30 @@ func (h *Handler) DeleteAgent(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", http.StatusSeeOther)
}
// UpdateAgentHostname handles hostname updates for agents.
func (h *Handler) UpdateAgentHostname(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
agentID := r.PathValue("id")
hostname := r.FormValue("hostname")
if hostname == "" {
http.Error(w, "Hostname cannot be empty", http.StatusBadRequest)
return
}
err := h.store.UpdateHostname(agentID, hostname)
if err != nil {
http.NotFound(w, r)
return
}
http.Redirect(w, r, "/agents/"+agentID, http.StatusSeeOther)
}
// getStaleAgents returns agents that haven't reported in 6 months.
func (h *Handler) getStaleAgents() []*store.AgentStats {
const staleThreshold = 6 * 30 * 24 * time.Hour