- 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
76 lines
1.8 KiB
Go
76 lines
1.8 KiB
Go
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"})
|
|
}
|