mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-28 05:46:36 +08:00
129 lines
2.5 KiB
Go
129 lines
2.5 KiB
Go
package controller
|
|
|
|
import (
|
|
"encoding/json"
|
|
"gin-template/service"
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
)
|
|
|
|
func AgentRegister(c *gin.Context) {
|
|
var payload service.AgentNodePayload
|
|
if err := json.NewDecoder(c.Request.Body).Decode(&payload); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"success": false,
|
|
"message": "无效的参数",
|
|
})
|
|
return
|
|
}
|
|
node, err := service.RegisterNode(payload)
|
|
if err != nil {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": false,
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": true,
|
|
"message": "",
|
|
"data": node,
|
|
})
|
|
}
|
|
|
|
func AgentHeartbeat(c *gin.Context) {
|
|
var payload service.AgentNodePayload
|
|
if err := json.NewDecoder(c.Request.Body).Decode(&payload); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"success": false,
|
|
"message": "无效的参数",
|
|
})
|
|
return
|
|
}
|
|
node, err := service.HeartbeatNode(payload)
|
|
if err != nil {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": false,
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": true,
|
|
"message": "",
|
|
"data": node,
|
|
})
|
|
}
|
|
|
|
func AgentGetActiveConfig(c *gin.Context) {
|
|
config, err := service.GetActiveConfigForAgent()
|
|
if err != nil {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": false,
|
|
"message": "当前没有激活版本",
|
|
})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": true,
|
|
"message": "",
|
|
"data": config,
|
|
})
|
|
}
|
|
|
|
func AgentReportApplyLog(c *gin.Context) {
|
|
var payload service.ApplyLogPayload
|
|
if err := json.NewDecoder(c.Request.Body).Decode(&payload); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"success": false,
|
|
"message": "无效的参数",
|
|
})
|
|
return
|
|
}
|
|
log, err := service.ReportApplyLog(payload)
|
|
if err != nil {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": false,
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": true,
|
|
"message": "",
|
|
"data": log,
|
|
})
|
|
}
|
|
|
|
func GetNodes(c *gin.Context) {
|
|
nodes, err := service.ListNodeViews()
|
|
if err != nil {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": false,
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": true,
|
|
"message": "",
|
|
"data": nodes,
|
|
})
|
|
}
|
|
|
|
func GetApplyLogs(c *gin.Context) {
|
|
logs, err := service.ListApplyLogs(c.Query("node_id"))
|
|
if err != nil {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": false,
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": true,
|
|
"message": "",
|
|
"data": logs,
|
|
})
|
|
}
|