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
76 lines
1.9 KiB
Go
76 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"nerd-monitor/internal/api"
|
|
"nerd-monitor/internal/auth"
|
|
"nerd-monitor/internal/store"
|
|
"nerd-monitor/internal/ui"
|
|
)
|
|
|
|
func main() {
|
|
var (
|
|
addr = flag.String("addr", "0.0.0.0", "server address")
|
|
port = flag.String("port", "8080", "server port")
|
|
username = flag.String("username", "admin", "admin username")
|
|
password = flag.String("password", "admin", "admin password")
|
|
)
|
|
flag.Parse()
|
|
|
|
// Initialize dependencies
|
|
s := store.New()
|
|
authMgr := auth.New(*username, *password)
|
|
apiHandler := api.New(s)
|
|
uiHandler := ui.New(s, authMgr)
|
|
|
|
// Setup router
|
|
r := chi.NewRouter()
|
|
|
|
// Public routes (no auth required)
|
|
r.Post("/api/report", apiHandler.ReportStats)
|
|
r.Get("/api/agents", apiHandler.ListAgents)
|
|
r.Get("/api/agents/{id}", apiHandler.GetAgent)
|
|
r.Get("/login", uiHandler.Login)
|
|
r.Post("/login", uiHandler.Login)
|
|
|
|
// Protected routes (auth required)
|
|
r.Group(func(protectedRoutes chi.Router) {
|
|
protectedRoutes.Use(authMgr.Middleware)
|
|
protectedRoutes.Get("/", uiHandler.Dashboard)
|
|
protectedRoutes.Get("/agents/{id}", uiHandler.AgentDetail)
|
|
protectedRoutes.Post("/logout", uiHandler.Logout)
|
|
protectedRoutes.Post("/api/agents/remove-stale", uiHandler.RemoveStaleAgents)
|
|
protectedRoutes.Post("/api/agents/{id}/delete", uiHandler.DeleteAgent)
|
|
})
|
|
|
|
// Setup server
|
|
server := &http.Server{
|
|
Addr: *addr + ":" + *port,
|
|
Handler: r,
|
|
}
|
|
|
|
// Graceful shutdown
|
|
sigChan := make(chan os.Signal, 1)
|
|
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
|
|
|
|
go func() {
|
|
<-sigChan
|
|
log.Println("Shutting down server...")
|
|
server.Close()
|
|
os.Exit(0)
|
|
}()
|
|
|
|
log.Printf("Starting server on http://%s:%s\n", *addr, *port)
|
|
log.Printf("Login with %s / %s\n", *username, *password)
|
|
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
log.Fatalf("Server error: %v", err)
|
|
}
|
|
}
|