修改部分http返回码

This commit is contained in:
3343780376 2023-01-10 20:43:28 +08:00
parent b0a8b70ca9
commit 194455aa2a
8 changed files with 53 additions and 18 deletions

View File

@ -18,7 +18,7 @@ func get() gin.HandlerFunc {
return func(ctx *gin.Context) {
dependences, err := models.QueryDependences(ctx.Query("searchValue"))
if err != nil {
ctx.JSON(502, res.Err(502, err))
ctx.JSON(503, res.Err(503, err))
return
}
ctx.JSON(200, res.Ok(dependences))
@ -30,7 +30,7 @@ func post() gin.HandlerFunc {
var deps []*models.Dependences
err := ctx.ShouldBindJSON(&deps)
if err != nil {
ctx.JSON(502, res.Err(502, err))
ctx.JSON(503, res.Err(503, err))
return
}
for _, dep := range deps {

View File

@ -34,7 +34,7 @@ func put() gin.HandlerFunc {
r := new(Req)
err := ctx.ShouldBindJSON(r)
if err != nil {
ctx.JSON(502, res.Err(502, err))
ctx.JSON(503, res.Err(503, err))
return
}
err = os.WriteFile(fmt.Sprintf("data/scripts/%s/%s", r.Path, r.FileName), []byte(r.Content), 0666)
@ -57,7 +57,7 @@ func post() gin.HandlerFunc {
r := new(Req)
err := ctx.ShouldBind(r)
if err != nil {
ctx.JSON(502, res.Err(502, err))
ctx.JSON(503, res.Err(503, err))
return
}
if r.Directory != "" {
@ -111,7 +111,7 @@ func del() gin.HandlerFunc {
r := new(Req)
err := ctx.ShouldBindJSON(r)
if err != nil {
ctx.JSON(502, res.Err(502, err))
ctx.JSON(503, res.Err(503, err))
return
}
if r.Type == "file" {

View File

@ -26,7 +26,7 @@ func run() gin.HandlerFunc {
var ids []int
err := ctx.ShouldBindJSON(&ids)
if err != nil {
ctx.JSON(502, res.Err(502, err))
ctx.JSON(503, res.Err(503, err))
return
}
err = subscription.RunSubscription(ids)
@ -43,7 +43,7 @@ func stop() gin.HandlerFunc {
var ids []int
err := ctx.ShouldBindJSON(&ids)
if err != nil {
ctx.JSON(502, res.Err(502, err))
ctx.JSON(503, res.Err(503, err))
return
}
err = subscription.StopSubscription(ids)
@ -75,7 +75,7 @@ func post() gin.HandlerFunc {
sub := new(models.Subscriptions)
err := ctx.ShouldBindJSON(sub)
if err != nil {
ctx.JSON(502, res.Err(502, err))
ctx.JSON(503, res.Err(503, err))
return
}
id, err := subscription.AddSubscription(sub)
@ -93,7 +93,7 @@ func enable() gin.HandlerFunc {
var ids []int
err := ctx.ShouldBindJSON(&ids)
if err != nil {
ctx.JSON(503, res.Err(502, err))
ctx.JSON(503, res.Err(503, err))
return
}
err = subscription.EnableSubscription(ids)
@ -109,7 +109,7 @@ func disable() gin.HandlerFunc {
var ids []int
err := ctx.ShouldBindJSON(&ids)
if err != nil {
ctx.JSON(502, res.Err(502, err))
ctx.JSON(503, res.Err(503, err))
return
}
err = subscription.DisableSubscription(ids)
@ -126,7 +126,7 @@ func put() gin.HandlerFunc {
s := new(models.Subscriptions)
err := ctx.ShouldBindJSON(s)
if err != nil {
ctx.JSON(503, res.Err(502, err))
ctx.JSON(503, res.Err(503, err))
return
}
err = subscription.UpdateSubscription(s)
@ -143,7 +143,7 @@ func del() gin.HandlerFunc {
var ids []int
err := ctx.ShouldBindJSON(&ids)
if err != nil {
ctx.JSON(503, res.Err(502, err))
ctx.JSON(503, res.Err(503, err))
return
}
err = subscription.DeleteSubscription(ids)

View File

@ -57,6 +57,18 @@ PipMirror="https://pypi.doubanio.com/simple/"
## 安装node依赖时指定npm源
NpmMirror="https://registry.npmmirror.com"
# 运行以.py结尾的文件时的命令
PythonCmd="python"
# 运行以.js结尾的文件时的命令
JsCmd="node"
# 运行以.ts结尾的文件时的命令
TsCmd="ts-node-transpile-only"
# 运行以.sh结尾的文件时的命令
ShCmd="bash"
## 通知环境变量
## 1. Server酱
## https://sct.ftqq.com

View File

@ -13,7 +13,7 @@ func getNotification() gin.HandlerFunc {
return func(ctx *gin.Context) {
data, err := os.ReadFile(path.Join("data", "config", "push.json"))
if err != nil {
ctx.JSON(502, res.Err(502, err))
ctx.JSON(503, res.Err(503, err))
return
}
m := make(map[string]interface{}, 5)
@ -31,7 +31,7 @@ func putNotification() gin.HandlerFunc {
}
err = notification.HandlePush(string(data))
if err != nil {
ctx.JSON(502, res.Err(502, err))
ctx.JSON(503, res.Err(503, err))
return
}
ctx.JSON(200, res.Ok(true))

View File

@ -26,7 +26,7 @@ func Api(group *gin.RouterGroup) {
group.PUT("/init", appInit())
group.POST("/login", login())
group.POST("/logout", logout())
group.PUT("/notification/init", putNotification())
group.PUT("/notification", putNotification())
group.GET("/notification", getNotification())
}
@ -72,7 +72,7 @@ func appInit() gin.HandlerFunc {
r := new(Req)
err = ctx.ShouldBindJSON(r)
if err != nil {
ctx.JSON(502, res.ErrMessage(502, err.Error()))
ctx.JSON(503, res.ErrMessage(503, err.Error()))
return
}
m := new(models.AuthFile)
@ -95,7 +95,7 @@ func login() gin.HandlerFunc {
r := new(Req)
err := ctx.ShouldBindJSON(r)
if err != nil {
ctx.JSON(502, res.Err(502, err))
ctx.JSON(503, res.Err(503, err))
return
}
data, err := os.ReadFile(path.Join("data", "config", "auth.json"))

View File

@ -158,7 +158,7 @@ func handCommand(command string) *task {
commands := strings.Split(command, " ")
if commands[0] == "task" {
if strings.HasSuffix(commands[1], ".py") {
ta.cmd = "poetry run python " + commands[1]
ta.cmd = "python3 " + commands[1]
} else if strings.HasSuffix(commands[1], ".js") {
ta.cmd = "node " + commands[1]
} else if strings.HasSuffix(commands[1], ".sh") {

View File

@ -14,6 +14,8 @@ import (
func AddDep(dep *models.Dependences) {
if dep.Type == models.NODE {
addNodeDep(dep)
} else if dep.Type == models.PYTHON {
addPythonDep(dep)
}
}
@ -37,3 +39,24 @@ func addNodeDep(dep *models.Dependences) {
models.AddDependences(dep)
}, buffer)
}
func addPythonDep(dep *models.Dependences) {
log := ""
buffer := bytes.NewBufferString(log)
ctx := context.WithValue(context.Background(), "cancel", make(chan int, 1))
now := time.Now()
utils.RunTask(ctx, fmt.Sprintf("pip install %s", dep.Name), map[string]string{}, func(ctx context.Context) {
writer := ctx.Value("log").(io.Writer)
writer.Write([]byte(fmt.Sprintf("##开始执行.. %s\n\n", now.Format("2006-01-02 15:04:05"))))
}, func(ctx context.Context) {
writer := ctx.Value("log").(io.Writer)
writer.Write([]byte(fmt.Sprintf("\n##执行结束.. %s耗时%.1f秒\n\n", time.Now().Format("2006-01-02 15:04:05"), time.Now().Sub(now).Seconds())))
dep.Status = 1
var logs []string
for _, i2 := range strings.Split(buffer.String(), "\n") {
logs = append(logs, i2+"\n\n")
}
dep.Log = logs
models.AddDependences(dep)
}, buffer)
}