videoplayer/handler/cid.go

201 lines
5.9 KiB
Go
Raw Normal View History

package handler
import (
"bytes"
"github.com/gin-gonic/gin"
"os"
"os/exec"
"strconv"
"videoplayer/dao"
"videoplayer/proto"
)
type CIDCreateReq struct {
Name string `json:"name" form:"name"`
Url string `json:"url" form:"url"`
Script string `json:"script" form:"script"`
}
type CIDDeleteReq struct {
ID int `json:"id" form:"id"`
}
type CIDRunReq struct {
ID int `json:"id" form:"id"`
}
type CIDUpdateReq struct {
ID int `json:"id" form:"id"`
Name string `json:"name" form:"name"`
Url string `json:"url" form:"url"`
Script string `json:"script" form:"script"`
Token string `json:"token" form:"token"`
}
func SetUpCIDGroup(router *gin.Engine) {
cidGroup := router.Group("/cid") //持续集成、部署
cidGroup.POST("/create", CreateCID)
cidGroup.POST("/delete", DeleteCID)
cidGroup.POST("/update", UpdateCID)
cidGroup.POST("/list", GetCIDList)
cidGroup.POST("/run", RunCID)
cidGroup.POST("/log", GetCIDLogList) //获取执行日志
cidGroup.POST("/log/detail", GetCIDLog) //获取执行日志详情
cidGroup.POST("/callback", CIDCallback)
}
func RunCID(c *gin.Context) {
var req CIDRunReq
if err := c.ShouldBind(&req); err == nil {
// 获取用户ID
id, _ := c.Get("id")
authID := int(id.(float64))
cid := dao.FindCIDByID(req.ID, authID)
if cid.ID == 0 {
c.JSON(200, gin.H{"error": "CID not found", "code": proto.OperationFailed, "message": "failed"})
return
} else {
scriptContent := ``
str, _ := generateRandomHexString(16)
scriptName := "" + str + `.sh`
// 写入脚本
err2 := os.WriteFile(scriptName, []byte(scriptContent), 0755)
if err2 != nil {
c.JSON(200, gin.H{"error": err2.Error(), "code": proto.OperationFailed, "message": "failed"})
return
} else {
//执行脚本
cmd := exec.Command("/bin/bash", "-c", scriptName)
err3 := cmd.Run()
// 使用bytes.Buffer捕获输出
var out bytes.Buffer
cmd.Stdout = &out
dao.CreateRunLog(req.ID, authID, out.String(), err3.Error()) //添加执行日志
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "success"})
}
}
} else {
c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
}
}
func CreateCID(c *gin.Context) {
var req CIDCreateReq
if err := c.ShouldBind(&req); err == nil {
// 获取用户ID
id, _ := c.Get("id")
authID := int(id.(float64))
token, _ := generateRandomHexString(32)
res := dao.CreateCID(req.Name, req.Url, req.Script, token, authID)
if res != 0 {
c.JSON(200, gin.H{})
} else {
c.JSON(200, gin.H{"error": "CreateCID failed", "code": proto.OperationFailed, "message": "failed"})
}
} else {
c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
}
}
func DeleteCID(c *gin.Context) {
var req CIDDeleteReq
if err := c.ShouldBind(&req); err == nil {
// 获取用户ID
id, _ := c.Get("id")
authID := int(id.(float64))
cid := dao.DeleteCIDByID(req.ID, authID)
if cid == false {
c.JSON(200, gin.H{"error": "CID not found", "code": proto.OperationFailed, "message": "failed"})
return
} else {
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "success"})
}
} else {
c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
}
}
func UpdateCID(c *gin.Context) {
var req CIDUpdateReq
if err := c.ShouldBind(&req); err == nil {
// 获取用户ID
id, _ := c.Get("id")
authID := int(id.(float64))
cid := dao.UpdateCIDByID(req.ID, authID, req.Name, req.Url, req.Script, req.Token)
if cid == false {
c.JSON(200, gin.H{"error": "CID not found", "code": proto.OperationFailed, "message": "failed"})
return
} else {
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "success"})
}
} else {
c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
}
}
func GetCIDList(c *gin.Context) {
// 获取用户ID
id, _ := c.Get("id")
authID := int(id.(float64))
cids := dao.FindCIDByAuthID(authID)
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": cids})
}
func GetCIDLog(c *gin.Context) {
var req CIDRunReq
if err := c.ShouldBind(&req); err == nil {
// 获取用户ID
id, _ := c.Get("id")
authID := int(id.(float64))
cidLogs := dao.FindRunLogByID(req.ID, authID)
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": cidLogs})
} else {
c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"})
}
}
func GetCIDLogList(c *gin.Context) {
// 获取用户ID
id, _ := c.Get("id")
authID := int(id.(float64))
cidLogs := dao.FindRunLogByAuthID(authID)
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": cidLogs})
}
func CIDCallback(c *gin.Context) {
// 获取用户ID
token := c.Query("token")
cid_id := c.Query("id")
//将cid转换为int
cid, _ := strconv.Atoi(cid_id)
if token == "" || cid == 0 {
c.JSON(200, gin.H{"error": "parameter error", "code": proto.ParameterError, "message": "failed"})
return
}
res := dao.FindCIDByIDAndToken(cid, token)
if res.ID != 0 {
scriptContent := ``
str, _ := generateRandomHexString(16)
scriptName := "" + str + `.sh`
// 写入脚本
err2 := os.WriteFile(scriptName, []byte(scriptContent), 0755)
if err2 != nil {
c.JSON(200, gin.H{"error": err2.Error(), "code": proto.OperationFailed, "message": "failed"})
return
} else {
//执行脚本
cmd := exec.Command("/bin/bash", "-c", scriptName)
err3 := cmd.Run()
// 使用bytes.Buffer捕获输出
var out bytes.Buffer
cmd.Stdout = &out
dao.CreateRunLog(cid, res.Auth_id, out.String(), err3.Error()) //添加执行日志
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "success"})
}
} else {
c.JSON(200, gin.H{"error": "CID not found by id and token", "code": proto.OperationFailed, "message": "failed"})
return
}
}