diff --git a/api/dependencies/dependencies.go b/api/dependencies/dependencies.go index 50539aa..380893f 100644 --- a/api/dependencies/dependencies.go +++ b/api/dependencies/dependencies.go @@ -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 { diff --git a/api/scripts/scripts.go b/api/scripts/scripts.go index 733ebfd..60cd39b 100644 --- a/api/scripts/scripts.go +++ b/api/scripts/scripts.go @@ -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" { diff --git a/api/subscription/subscription.go b/api/subscription/subscription.go index 35a941f..ea3c9b5 100644 --- a/api/subscription/subscription.go +++ b/api/subscription/subscription.go @@ -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) diff --git a/api/user/config_sample.sh b/api/user/config_sample.sh index 7b87362..74d3bec 100644 --- a/api/user/config_sample.sh +++ b/api/user/config_sample.sh @@ -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 diff --git a/api/user/notification.go b/api/user/notification.go index b2cd468..dbb36de 100644 --- a/api/user/notification.go +++ b/api/user/notification.go @@ -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)) diff --git a/api/user/user.go b/api/user/user.go index 47de3da..7a7cb9f 100644 --- a/api/user/user.go +++ b/api/user/user.go @@ -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")) diff --git a/service/cron/manager.go b/service/cron/manager.go index a3c293f..d8f869e 100644 --- a/service/cron/manager.go +++ b/service/cron/manager.go @@ -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") { diff --git a/service/dependencies/dependencies.go b/service/dependencies/dependencies.go index 8277777..556fbc3 100644 --- a/service/dependencies/dependencies.go +++ b/service/dependencies/dependencies.go @@ -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) +}