Files
komari/api/admin/client.go
T
Akizon77 2c2c81953e 移除 远程配置下发。
修改 客户端基本属性逻辑。
修改 最新上报key改为uuid
新增 客户端命名
新增 历史记录数据库压缩。
新增 在线节点信息。
新增 公开API /api/nodes 显示客户端基础属性
2025-04-27 18:57:37 +08:00

113 lines
2.6 KiB
Go

package admin
import (
"net/http"
"time"
"github.com/akizon77/komari/database/clients"
"github.com/akizon77/komari/database/dbcore"
"github.com/akizon77/komari/database/history"
"github.com/akizon77/komari/database/models"
"github.com/gin-gonic/gin"
)
func AddClient(c *gin.Context) {
uuid, token, err := clients.CreateClient()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"status": "success", "uuid": uuid, "token": token})
}
func EditClient(c *gin.Context) {
var req struct {
UUID string `json:"uuid" binding:"required"`
ClientName string `json:"name,omitempty"`
Token string `json:"token,omitempty"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"status": "error", "error": err.Error()})
return
}
updates := make(map[string]interface{})
updates["updated_at"] = time.Now()
if req.ClientName != "" {
updates["client_name"] = req.ClientName
}
if req.Token != "" {
updates["token"] = req.Token
}
db := dbcore.GetDBInstance()
if err := db.Model(&models.Client{}).Where("uuid = ?", req.UUID).Updates(updates).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"status": "success"})
}
func RemoveClient(c *gin.Context) {
var req struct {
UUID string `json:"uuid" binding:"required"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(400, gin.H{
"status": "error",
"error": "Invalid or missing UUID",
})
}
err := clients.DeleteClientConfig(req.UUID)
if err != nil {
c.JSON(500, gin.H{
"status": "error",
"error": "Failed to delete client" + err.Error(),
})
return
}
c.JSON(200, gin.H{"status": "success"})
}
func ClearHistory(c *gin.Context) {
if err := history.DeleteAll(); err != nil {
c.JSON(500, gin.H{
"status": "error",
"error": "Failed to delete history" + err.Error(),
})
return
}
c.JSON(200, gin.H{"status": "success"})
}
func GetClient(c *gin.Context) {
uuid := c.Query("uuid")
if uuid == "" {
c.JSON(400, gin.H{
"status": "error",
"error": "Invalid or missing UUID",
})
return
}
result, err := clients.GetClientByUUID(uuid)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "error": err.Error()})
return
}
c.JSON(http.StatusOK, result)
}
func ListClients(c *gin.Context) {
cls, err := clients.GetAllClientBasicInfo()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "error": err.Error()})
return
}
c.JSON(http.StatusOK, cls)
}