153 lines
5.0 KiB
Go
153 lines
5.0 KiB
Go
package handler
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
"videoplayer/proto"
|
|
"videoplayer/service"
|
|
)
|
|
|
|
type ShellHandler struct {
|
|
}
|
|
|
|
type CreateShellReq struct {
|
|
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"`
|
|
}
|
|
type UpdateShellReq struct {
|
|
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"`
|
|
ShellRuntime float64 `json:"shell_runtime" form:"shell_runtime"` // 执行时间,单位秒
|
|
ShellDuration float64 `json:"shell_duration" form:"shell_duration"` // 执行时长,单位秒
|
|
}
|
|
|
|
type UpdateShellReqV2 struct {
|
|
Shells []UpdateShellReq `json:"shells" form:"shells"`
|
|
}
|
|
|
|
type UpdateShellResp struct {
|
|
ID uint `json:"id" form:"id"`
|
|
Status int `json:"status" form:"status"`
|
|
}
|
|
|
|
type DeleteShellRequestID struct {
|
|
ID uint `json:"id" form:"id" binding:"required"`
|
|
}
|
|
type DeleteShellRequest struct {
|
|
Shells []DeleteShellRequestID `json:"shells" form:"shells" binding:"required"`
|
|
}
|
|
|
|
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("/delete", shellHandler.DeleteShell)
|
|
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
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
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) {
|
|
userId, _ := c.Get("id")
|
|
id := int(userId.(float64))
|
|
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
|
|
userId, _ := c.Get("id")
|
|
id := int(userId.(float64))
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
c.JSON(200, gin.H{"code": proto.ShellUpdateFailed, "message": "参数错误", "data": err.Error()})
|
|
} else {
|
|
var resp []UpdateShellResp
|
|
//log.Println("UpdateShellReqData:", req.Shells)
|
|
for _, v := range req.Shells {
|
|
if service.UpdateShellByIDV2(v.ID, uint(id), v.ShellName, v.ShellContent, v.Server, v.Status, v.ShellResult, v.ShellRuntime) {
|
|
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 DeleteShellResp struct {
|
|
Success []DeleteShellRequestID `json:"success" form:"success"`
|
|
Error []DeleteShellRequestID `json:"error" form:"error"`
|
|
}
|
|
|
|
func (s *ShellHandler) DeleteShell(c *gin.Context) {
|
|
userId, _ := c.Get("id")
|
|
id := int(userId.(float64))
|
|
var req DeleteShellRequest
|
|
var resp proto.GeneralResp
|
|
|
|
if err := c.ShouldBind(&req); err == nil {
|
|
var delSuccessIDs []DeleteShellRequestID
|
|
var delErrorIDs []DeleteShellRequestID
|
|
for _, v := range req.Shells {
|
|
delSuccess := service.DeleteShellByID(v.ID, uint(id))
|
|
if delSuccess {
|
|
delSuccessIDs = append(delSuccessIDs, v)
|
|
} else {
|
|
delErrorIDs = append(delErrorIDs, v)
|
|
}
|
|
}
|
|
var delResp DeleteShellResp
|
|
delResp.Success = delSuccessIDs
|
|
delResp.Error = delErrorIDs
|
|
resp.Code = proto.SuccessCode
|
|
resp.Data = delResp
|
|
resp.Message = "success"
|
|
} else {
|
|
resp.Code = proto.ParameterError
|
|
resp.Message = "参数解析错误:" + err.Error()
|
|
}
|
|
c.JSON(http.StatusOK, resp)
|
|
}
|
|
|
|
type ServerWillRunReq struct {
|
|
Server string `json:"server"`
|
|
}
|
|
|
|
func (s *ShellHandler) ServerWillRun(c *gin.Context) {
|
|
userId, _ := c.Get("id")
|
|
id := int(userId.(float64))
|
|
var req ServerWillRunReq
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
c.JSON(200, gin.H{"code": proto.ShellUpdateFailed, "message": "参数错误", "data": err.Error()})
|
|
} else {
|
|
//log.Printf("ServerWillRunReq:%s,id:%d", req.Server, id)
|
|
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})
|
|
}
|
|
}
|