Files
X-Agents/server/internal/handler/system_handler.go
DESKTOP-72TV0V4\caoxiaozhu fdd6b2c17d fix: 优化后端各模块 handler
- database_handler, knowledge_handler, model_handler
- neo4j_handler, sub_table_handler
- system_handler, upload_handler
- knowledge_service, upload_service

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-11 14:26:04 +08:00

70 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package handler
import (
"net/http"
"x-agents/server/internal/model"
"github.com/gin-gonic/gin"
)
type SystemHandler struct{}
func NewSystemHandler() *SystemHandler {
return &SystemHandler{}
}
// @Summary 获取系统信息
// @Description 获取服务器系统信息CPU、内存等
// @Tags 系统
// @Accept json
// @Produce json
// @Success 200 {object} model.SystemInfo
// @Failure 500 {object} map[string]string
// @Router /system/info [get]
func (h *SystemHandler) GetSystemInfo(c *gin.Context) {
info, err := getSystemInfo()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, info)
}
// getSystemInfo 获取系统信息
func getSystemInfo() (*model.SystemInfo, error) {
// 获取CPU使用率
cpuPercent, err := getCPUPercent()
if err != nil {
return nil, err
}
// 获取CPU核心数
coreCount, err := getCPUCoreCount()
if err != nil {
coreCount = 0
}
// 获取CPU型号
modelName, err := getCPUModelName()
if err != nil {
modelName = "Unknown"
}
// 获取内存信息
memoryInfo, err := getMemoryInfo()
if err != nil {
return nil, err
}
return &model.SystemInfo{
CPU: model.CPUInfo{
Percent: cpuPercent,
CoreCount: coreCount,
ModelName: modelName,
},
Memory: *memoryInfo,
}, nil
}