feat: 为QQ机器人添加Git仓库管理接口

新增了Git仓库管理相关功能:
1. 在router中添加/git路由组
2. 新增代码拉取、构建和重启的HTTP接口
3. 将Git拉取逻辑抽取到util包中
4. 支持通过HTTP请求触发代码拉取、构建和重启操作
5. 扩展CORS配置,允许git.lxtend.com域名访问
This commit is contained in:
lixiangwuxian
2025-03-09 01:32:49 +08:00
parent 8049ec7946
commit cfd20cbefe
3 changed files with 52 additions and 13 deletions

View File

@@ -3,6 +3,7 @@ package restart
import (
"errors"
"log"
"net/http"
"os"
"os/exec"
"time"
@@ -11,6 +12,8 @@ import (
"git.lxtend.com/qqbot/handler"
"git.lxtend.com/qqbot/model"
docker "git.lxtend.com/qqbot/service/exec"
"git.lxtend.com/qqbot/util"
"github.com/gin-gonic/gin"
)
var hasVaildBuild = true
@@ -94,7 +97,7 @@ func buildAndRestart(msg model.Message) model.Reply {
}
func pullCode(msg model.Message) model.Reply {
err := gitPull()
err := util.GitPull()
if err != nil {
return model.Reply{
ReplyMsg: "拉取代码失败,报错如下\n" + err.Error(),
@@ -164,17 +167,26 @@ func buildBot() error {
return nil
}
func gitPull() error {
workDir, err := os.Getwd()
func PullCodeHandler(c *gin.Context) {
err := util.GitPull()
if err != nil {
return err
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}
cmd := exec.Command("git", "pull")
cmd.Dir = workDir
output, err := cmd.CombinedOutput()
if err != nil {
return errors.New(string(output) + err.Error())
}
return nil
c.JSON(http.StatusOK, gin.H{"message": "拉取代码成功"})
}
func BuildBotHandler(c *gin.Context) {
err := buildBot()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}
c.JSON(http.StatusOK, gin.H{"message": "构建成功"})
}
func RestartBotHandler(c *gin.Context) {
err := restartProgram()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}
c.JSON(http.StatusOK, gin.H{"message": "重启成功"})
}