feat: init pc-monitor project
- Client: Go-based Windows hardware monitoring (CPU, GPU, memory, disk, network, power) - Server: Go + Gin + SQLite backend with REST API - Frontend: Vue 3 + Element Plus dashboard - Docker deployment support - Windows service installation script
This commit is contained in:
89
server/api/handler/alert.go
Normal file
89
server/api/handler/alert.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"pc-monitor-server/model"
|
||||
"pc-monitor-server/service"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type AlertHandler struct {
|
||||
service *service.AlertService
|
||||
}
|
||||
|
||||
func NewAlertHandler(service *service.AlertService) *AlertHandler {
|
||||
return &AlertHandler{service: service}
|
||||
}
|
||||
|
||||
type CreateRuleRequest struct {
|
||||
DeviceID string `json:"device_id" binding:"required"`
|
||||
Metric string `json:"metric" binding:"required"`
|
||||
Operator string `json:"operator" binding:"required"`
|
||||
Threshold float64 `json:"threshold" binding:"required"`
|
||||
Duration int `json:"duration"`
|
||||
}
|
||||
|
||||
func (h *AlertHandler) CreateRule(c *gin.Context) {
|
||||
var req CreateRuleRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
rule := &model.AlertRule{
|
||||
ID: uuid.New().String(),
|
||||
DeviceID: req.DeviceID,
|
||||
Metric: req.Metric,
|
||||
Operator: req.Operator,
|
||||
Threshold: req.Threshold,
|
||||
Duration: req.Duration,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
if err := h.service.CreateRule(rule); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"rule": rule})
|
||||
}
|
||||
|
||||
func (h *AlertHandler) GetRulesByDevice(c *gin.Context) {
|
||||
deviceID := c.Param("id")
|
||||
rules, err := h.service.GetRulesByDevice(deviceID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"rules": rules})
|
||||
}
|
||||
|
||||
func (h *AlertHandler) DeleteRule(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
if err := h.service.DeleteRule(id); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Rule deleted"})
|
||||
}
|
||||
|
||||
func (h *AlertHandler) GetActiveAlerts(c *gin.Context) {
|
||||
alerts, err := h.service.GetActiveAlerts()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"alerts": alerts})
|
||||
}
|
||||
|
||||
func (h *AlertHandler) ResolveAlert(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
if err := h.service.ResolveAlert(id); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Alert resolved"})
|
||||
}
|
||||
75
server/api/handler/device.go
Normal file
75
server/api/handler/device.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"pc-monitor-server/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type DeviceHandler struct {
|
||||
service *service.DeviceService
|
||||
}
|
||||
|
||||
func NewDeviceHandler(service *service.DeviceService) *DeviceHandler {
|
||||
return &DeviceHandler{service: service}
|
||||
}
|
||||
|
||||
type RegisterRequest struct {
|
||||
Hostname string `json:"hostname" binding:"required"`
|
||||
OS string `json:"os" binding:"required"`
|
||||
IP string `json:"ip" binding:"required"`
|
||||
}
|
||||
|
||||
func (h *DeviceHandler) Register(c *gin.Context) {
|
||||
var req RegisterRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
device, err := h.service.Register(req.Hostname, req.OS, req.IP)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"device": device})
|
||||
}
|
||||
|
||||
func (h *DeviceHandler) GetAll(c *gin.Context) {
|
||||
devices, err := h.service.GetAll()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"devices": devices})
|
||||
}
|
||||
|
||||
func (h *DeviceHandler) GetByID(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
device, err := h.service.GetByID(id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Device not found"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"device": device})
|
||||
}
|
||||
|
||||
func (h *DeviceHandler) Delete(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
if err := h.service.Delete(id); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Device deleted"})
|
||||
}
|
||||
|
||||
func (h *DeviceHandler) Heartbeat(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
if err := h.service.Heartbeat(id); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"message": "OK"})
|
||||
}
|
||||
80
server/api/handler/metrics.go
Normal file
80
server/api/handler/metrics.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"pc-monitor-server/model"
|
||||
"pc-monitor-server/service"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type MetricsHandler struct {
|
||||
service *service.MetricsService
|
||||
}
|
||||
|
||||
func NewMetricsHandler(service *service.MetricsService) *MetricsHandler {
|
||||
return &MetricsHandler{service: service}
|
||||
}
|
||||
|
||||
type ReportRequest struct {
|
||||
DeviceID string `json:"device_id" binding:"required"`
|
||||
model.Metrics
|
||||
}
|
||||
|
||||
func (h *MetricsHandler) Report(c *gin.Context) {
|
||||
var req ReportRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
metrics := req.Metrics
|
||||
metrics.DeviceID = req.DeviceID
|
||||
metrics.Timestamp = time.Now()
|
||||
|
||||
if err := h.service.Save(&metrics); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Metrics saved"})
|
||||
}
|
||||
|
||||
func (h *MetricsHandler) GetLatest(c *gin.Context) {
|
||||
deviceID := c.Param("id")
|
||||
metrics, err := h.service.GetLatest(deviceID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "No metrics found"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"metrics": metrics})
|
||||
}
|
||||
|
||||
func (h *MetricsHandler) GetHistory(c *gin.Context) {
|
||||
deviceID := c.Param("id")
|
||||
|
||||
startStr := c.Query("start")
|
||||
endStr := c.Query("end")
|
||||
|
||||
start := time.Now().Add(-1 * time.Hour)
|
||||
end := time.Now()
|
||||
|
||||
if startStr != "" {
|
||||
if t, err := time.Parse(time.RFC3339, startStr); err == nil {
|
||||
start = t
|
||||
}
|
||||
}
|
||||
if endStr != "" {
|
||||
if t, err := time.Parse(time.RFC3339, endStr); err == nil {
|
||||
end = t
|
||||
}
|
||||
}
|
||||
|
||||
metrics, err := h.service.GetHistory(deviceID, start, end, 1000)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"metrics": metrics})
|
||||
}
|
||||
57
server/api/router.go
Normal file
57
server/api/router.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"pc-monitor-server/api/handler"
|
||||
"pc-monitor-server/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func SetupRouter(deviceService *service.DeviceService, metricsService *service.MetricsService, alertService *service.AlertService) *gin.Engine {
|
||||
r := gin.Default()
|
||||
|
||||
r.Use(corsMiddleware())
|
||||
|
||||
api := r.Group("/api/v1")
|
||||
{
|
||||
deviceHandler := handler.NewDeviceHandler(deviceService)
|
||||
metricsHandler := handler.NewMetricsHandler(metricsService)
|
||||
alertHandler := handler.NewAlertHandler(alertService)
|
||||
|
||||
// Device endpoints
|
||||
api.POST("/register", deviceHandler.Register)
|
||||
api.GET("/devices", deviceHandler.GetAll)
|
||||
api.GET("/devices/:id", deviceHandler.GetByID)
|
||||
api.DELETE("/devices/:id", deviceHandler.Delete)
|
||||
api.POST("/devices/:id/heartbeat", deviceHandler.Heartbeat)
|
||||
|
||||
// Metrics endpoints
|
||||
api.POST("/report", metricsHandler.Report)
|
||||
api.GET("/devices/:id/metrics/latest", metricsHandler.GetLatest)
|
||||
api.GET("/devices/:id/metrics/history", metricsHandler.GetHistory)
|
||||
|
||||
// Alert endpoints
|
||||
api.GET("/alerts", alertHandler.GetActiveAlerts)
|
||||
api.POST("/alerts/rules", alertHandler.CreateRule)
|
||||
api.GET("/devices/:id/alerts/rules", alertHandler.GetRulesByDevice)
|
||||
api.DELETE("/alerts/rules/:id", alertHandler.DeleteRule)
|
||||
api.POST("/alerts/:id/resolve", alertHandler.ResolveAlert)
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func corsMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
|
||||
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
|
||||
|
||||
if c.Request.Method == "OPTIONS" {
|
||||
c.AbortWithStatus(204)
|
||||
return
|
||||
}
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user