添加日志查看及添加回调接口,脚本内容待拼接
This commit is contained in:
parent
c4b96f9a9f
commit
f57d9ee392
16
dao/cid.go
16
dao/cid.go
|
|
@ -83,8 +83,20 @@ func CreateRunLog(cid_id, auth_id int, log, err string) uint {
|
||||||
return cidRunLog.ID
|
return cidRunLog.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
func FindRunLogByAuthID(auth_id, cid_id int) []CIDRunLog {
|
func FindRunLogByAuthID(auth_id int) []CIDRunLog {
|
||||||
var cidRunLogs []CIDRunLog
|
var cidRunLogs []CIDRunLog
|
||||||
DB.Debug().Where("cid_id = ? and auth_id = ?", cid_id, auth_id).Find(&cidRunLogs)
|
DB.Debug().Where(" auth_id = ?", auth_id).Find(&cidRunLogs).Order("created_at desc")
|
||||||
return cidRunLogs
|
return cidRunLogs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func FindRunLogByID(auth_id, cid_id int) CIDRunLog {
|
||||||
|
var cidRunLog CIDRunLog
|
||||||
|
DB.Debug().Where("cid_id = ? and auth_id = ?", cid_id, auth_id).First(&cidRunLog)
|
||||||
|
return cidRunLog
|
||||||
|
}
|
||||||
|
|
||||||
|
func FindCIDByIDAndToken(id int, token string) CID {
|
||||||
|
var cid CID
|
||||||
|
DB.Debug().Where("id = ? and token = ?", id, token).First(&cid)
|
||||||
|
return cid
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"strconv"
|
||||||
"videoplayer/dao"
|
"videoplayer/dao"
|
||||||
"videoplayer/proto"
|
"videoplayer/proto"
|
||||||
)
|
)
|
||||||
|
|
@ -38,6 +39,9 @@ func SetUpCIDGroup(router *gin.Engine) {
|
||||||
cidGroup.POST("/update", UpdateCID)
|
cidGroup.POST("/update", UpdateCID)
|
||||||
cidGroup.POST("/list", GetCIDList)
|
cidGroup.POST("/list", GetCIDList)
|
||||||
cidGroup.POST("/run", RunCID)
|
cidGroup.POST("/run", RunCID)
|
||||||
|
cidGroup.POST("/log", GetCIDLogList) //获取执行日志
|
||||||
|
cidGroup.POST("/log/detail", GetCIDLog) //获取执行日志详情
|
||||||
|
cidGroup.POST("/callback", CIDCallback)
|
||||||
}
|
}
|
||||||
func RunCID(c *gin.Context) {
|
func RunCID(c *gin.Context) {
|
||||||
var req CIDRunReq
|
var req CIDRunReq
|
||||||
|
|
@ -137,3 +141,60 @@ func GetCIDList(c *gin.Context) {
|
||||||
cids := dao.FindCIDByAuthID(authID)
|
cids := dao.FindCIDByAuthID(authID)
|
||||||
c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": cids})
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue