102 lines
3.3 KiB
Go
102 lines
3.3 KiB
Go
package handler
|
|
|
|
import (
|
|
"runtime"
|
|
"syscall"
|
|
"time"
|
|
|
|
"kra/internal/server/httpx"
|
|
"kra/internal/service"
|
|
"kra/internal/service/dto"
|
|
"kra/internal/worker"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/shirou/gopsutil/v4/cpu"
|
|
"github.com/shirou/gopsutil/v4/mem"
|
|
)
|
|
|
|
type SystemConfig struct {
|
|
system *service.SystemService
|
|
settings *service.SettingsService
|
|
scheduler *worker.TaskScheduler
|
|
}
|
|
|
|
func NewSystemConfig(system *service.SystemService, settings *service.SettingsService, scheduler *worker.TaskScheduler) *SystemConfig {
|
|
return &SystemConfig{system: system, settings: settings, scheduler: scheduler}
|
|
}
|
|
|
|
func (h *SystemConfig) GetSecurity(c *gin.Context) {
|
|
value, err := h.settings.Security(c.Request.Context())
|
|
if err != nil {
|
|
httpx.Fail(c, "获取安全配置失败")
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, value, "获取成功")
|
|
}
|
|
|
|
func (h *SystemConfig) SetSecurity(c *gin.Context) {
|
|
var req dto.SecurityConfigRequest
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
httpx.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
result, err := h.settings.SaveSecurityRequest(c.Request.Context(), &req)
|
|
if err != nil {
|
|
httpx.Fail(c, err.Error())
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, result, "设置成功")
|
|
}
|
|
|
|
func (h *SystemConfig) Get(c *gin.Context) {
|
|
httpx.Write(c, httpx.CodeSuccess, h.system.SystemConfig(), "获取成功")
|
|
}
|
|
|
|
func (h *SystemConfig) Set(c *gin.Context) {
|
|
var req dto.SetSystemConfigRequest
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
httpx.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := h.system.SaveSystemConfig(c.Request.Context(), &req); err != nil {
|
|
httpx.Fail(c, "配置保存失败: "+err.Error())
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "设置成功")
|
|
}
|
|
|
|
func (h *SystemConfig) Reload(c *gin.Context) {
|
|
if err := h.system.ReloadConfig(c.Request.Context()); err != nil {
|
|
httpx.Fail(c, "重载系统失败:"+err.Error())
|
|
return
|
|
}
|
|
if err := h.scheduler.Reload(c.Request.Context()); err != nil {
|
|
httpx.Fail(c, "系统配置已重载,但定时任务重载失败:"+err.Error())
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "重载系统成功")
|
|
}
|
|
|
|
func (h *SystemConfig) ServerInfo(c *gin.Context) {
|
|
cpuPercent, _ := cpu.Percent(200*time.Millisecond, false)
|
|
memory, _ := mem.VirtualMemory()
|
|
disk := []gin.H{}
|
|
var stat syscall.Statfs_t
|
|
if syscall.Statfs(".", &stat) == nil {
|
|
total := uint64(stat.Blocks) * uint64(stat.Bsize)
|
|
free := uint64(stat.Bavail) * uint64(stat.Bsize)
|
|
used := total - free
|
|
percent := 0
|
|
if total > 0 {
|
|
percent = int(used * 100 / total)
|
|
}
|
|
disk = append(disk, gin.H{"mountPoint": ".", "usedMb": used / 1024 / 1024, "usedGb": used / 1024 / 1024 / 1024, "totalMb": total / 1024 / 1024, "totalGb": total / 1024 / 1024 / 1024, "usedPercent": percent})
|
|
}
|
|
usedMB, totalMB, memoryPercent := uint64(0), uint64(0), float64(0)
|
|
if memory != nil {
|
|
usedMB, totalMB, memoryPercent = memory.Used/1024/1024, memory.Total/1024/1024, memory.UsedPercent
|
|
}
|
|
server := gin.H{"os": gin.H{"goos": runtime.GOOS, "numCpu": runtime.NumCPU(), "compiler": runtime.Compiler, "goVersion": runtime.Version(), "numGoroutine": runtime.NumGoroutine()}, "cpu": gin.H{"cpus": cpuPercent, "cores": runtime.NumCPU()}, "ram": gin.H{"usedMb": usedMB, "totalMb": totalMB, "usedPercent": memoryPercent}, "disk": disk}
|
|
httpx.Write(c, httpx.CodeSuccess, gin.H{"server": server}, "获取成功")
|
|
}
|