2025-02-22 14:44:26 +08:00
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2025-03-11 15:14:38 +08:00
|
|
|
"log"
|
2025-02-22 14:44:26 +08:00
|
|
|
"videoplayer/proto"
|
|
|
|
|
"videoplayer/service"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ShellHandler struct {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type CreateShellReq struct {
|
2025-03-11 14:24:38 +08:00
|
|
|
ShellName string `json:"shell_name" form:"shell_name" binding:"required"`
|
|
|
|
|
ShellContent string `json:"shell_content" form:"shell_content" binding:"required"`
|
|
|
|
|
Server string `json:"server" form:"server" binding:"required"`
|
2025-02-22 14:44:26 +08:00
|
|
|
}
|
|
|
|
|
type UpdateShellReq struct {
|
2025-03-11 14:24:38 +08:00
|
|
|
ID uint `json:"id" form:"id" binding:"required"`
|
|
|
|
|
ShellName string `json:"shell_name" form:"shell_name"`
|
|
|
|
|
ShellContent string `json:"shell_content" form:"shell_content"`
|
|
|
|
|
Server string `json:"server" form:"server"`
|
|
|
|
|
Status int `json:"status" form:"status"`
|
|
|
|
|
ShellResult string `json:"shell_result" form:"shell_result"`
|
2025-02-22 14:44:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type UpdateShellReqV2 struct {
|
2025-03-11 14:24:38 +08:00
|
|
|
Shells []UpdateShellReq `json:"shells" form:"shells"`
|
2025-02-22 14:44:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type UpdateShellResp struct {
|
|
|
|
|
ID uint `json:"id" form:"id"`
|
|
|
|
|
Status int `json:"status" form:"status"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func SetUpShellGroup(router *gin.Engine) {
|
|
|
|
|
shellGroup := router.Group("/shell") //持续集成、部署
|
|
|
|
|
shellHandler := ShellHandler{}
|
|
|
|
|
shellGroup.POST("/create", shellHandler.CreateShell)
|
|
|
|
|
shellGroup.POST("/list", shellHandler.ListShell)
|
|
|
|
|
shellGroup.POST("/update", shellHandler.UpdateShell)
|
|
|
|
|
shellGroup.POST("/server_will_run_list", shellHandler.ServerWillRun)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *ShellHandler) CreateShell(c *gin.Context) {
|
|
|
|
|
user_id, _ := c.Get("id")
|
|
|
|
|
uid := int(user_id.(float64))
|
|
|
|
|
var req CreateShellReq
|
2025-03-11 14:29:26 +08:00
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
2025-02-22 14:44:26 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.ShellCreateFailed, "message": "参数错误", "data": err.Error()})
|
|
|
|
|
} else {
|
|
|
|
|
id := service.CreateShell(req.ShellName, req.ShellContent, req.Server, uid)
|
|
|
|
|
if id == 0 {
|
|
|
|
|
c.JSON(200, gin.H{"code": proto.ShellCreateFailed, "message": "创建失败,id is 0", "data": ""})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "创建成功", "data": id})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *ShellHandler) ListShell(c *gin.Context) {
|
2025-03-03 23:49:44 +08:00
|
|
|
userId, _ := c.Get("id")
|
|
|
|
|
id := int(userId.(float64))
|
2025-02-22 14:44:26 +08:00
|
|
|
shells := service.FindShellByAuthID(id)
|
|
|
|
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "获取成功", "data": shells})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *ShellHandler) UpdateShell(c *gin.Context) {
|
|
|
|
|
var req UpdateShellReqV2
|
2025-03-03 23:49:44 +08:00
|
|
|
userId, _ := c.Get("id")
|
|
|
|
|
id := int(userId.(float64))
|
2025-03-11 14:29:26 +08:00
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
2025-02-22 14:44:26 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.ShellUpdateFailed, "message": "参数错误", "data": err.Error()})
|
|
|
|
|
} else {
|
|
|
|
|
var resp []UpdateShellResp
|
2025-03-11 15:30:32 +08:00
|
|
|
log.Println("UpdateShellReqData:", req.Shells)
|
2025-02-22 14:44:26 +08:00
|
|
|
for _, v := range req.Shells {
|
|
|
|
|
if service.UpdateShellByID(v.ID, uint(id), v.ShellName, v.ShellContent, v.Server, v.Status, v.ShellResult) {
|
|
|
|
|
resp = append(resp, UpdateShellResp{ID: v.ID, Status: v.Status})
|
|
|
|
|
} else {
|
|
|
|
|
resp = append(resp, UpdateShellResp{ID: v.ID, Status: -1})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "更新成功", "data": resp})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ServerWillRunReq struct {
|
|
|
|
|
Server string `json:"server"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *ShellHandler) ServerWillRun(c *gin.Context) {
|
2025-03-03 23:49:44 +08:00
|
|
|
userId, _ := c.Get("id")
|
|
|
|
|
id := int(userId.(float64))
|
2025-02-22 14:44:26 +08:00
|
|
|
var req ServerWillRunReq
|
2025-03-11 14:29:26 +08:00
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
2025-02-22 14:44:26 +08:00
|
|
|
c.JSON(200, gin.H{"code": proto.ShellUpdateFailed, "message": "参数错误", "data": err.Error()})
|
|
|
|
|
} else {
|
2025-03-11 15:14:38 +08:00
|
|
|
log.Printf("ServerWillRunReq:%s,id:%d", req.Server, id)
|
2025-02-22 14:44:26 +08:00
|
|
|
willRunShells, err2 := service.FindShellWillRunByServer(req.Server, id)
|
|
|
|
|
if err2 != nil {
|
|
|
|
|
c.JSON(200, gin.H{"code": proto.ShellUpdateFailed, "message": "获取失败", "data": err2.Error()})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "获取成功", "data": willRunShells})
|
|
|
|
|
}
|
|
|
|
|
}
|