From a436a6b94a1d58febdd51abb67eff5b0f8881b12 Mon Sep 17 00:00:00 2001 From: "hobo.l" Date: Mon, 12 Aug 2024 19:16:42 +0300 Subject: [PATCH 1/2] add health / refresh dependency --- internal/controller/router.go | 51 +++++++++++++++++++--------- internal/controller/run.go | 13 +++++++ internal/core/runner/python/setup.go | 12 +++++++ internal/server/server.go | 11 ++++-- internal/service/python.go | 10 ++++++ 5 files changed, 78 insertions(+), 19 deletions(-) diff --git a/internal/controller/router.go b/internal/controller/router.go index 01ee0da..eb5410e 100644 --- a/internal/controller/router.go +++ b/internal/controller/router.go @@ -4,24 +4,43 @@ import ( "github.com/gin-gonic/gin" "github.com/langgenius/dify-sandbox/internal/middleware" "github.com/langgenius/dify-sandbox/internal/static" + "net/http" ) -func Setup(eng *gin.Engine) { - eng.Use(middleware.Auth()) +func Setup(Router *gin.Engine) { + PublicGroup := Router.Group("") + PrivateGroup := Router.Group("/v1/sandbox/") - eng.POST( - "/v1/sandbox/run", - middleware.MaxRequest(static.GetDifySandboxGlobalConfigurations().MaxRequests), - middleware.MaxWorker(static.GetDifySandboxGlobalConfigurations().MaxWorkers), - RunSandboxController, - ) - eng.GET( - "/v1/sandbox/dependencies", - GetDependencies, - ) + PrivateGroup.Use(middleware.Auth()) - eng.POST( - "/v1/sandbox/dependencies/update", - UpdateDependencies, - ) + { + // 健康监测 + PublicGroup.GET("/health", func(c *gin.Context) { + c.JSON(http.StatusOK, "ok") + }) + } + + InitRunRouter(PrivateGroup) + InitDependencyRouter(PrivateGroup) +} + +func InitDependencyRouter(Router *gin.RouterGroup) { + dependencyRouter := Router.Group("dependencies") + { + dependencyRouter.GET("", GetDependencies) + dependencyRouter.POST("update", UpdateDependencies) + dependencyRouter.GET("refresh", RefreshDependencies) + } +} + +func InitRunRouter(Router *gin.RouterGroup) { + runRouter := Router.Group("") + { + runRouter.POST( + "run", + middleware.MaxRequest(static.GetDifySandboxGlobalConfigurations().MaxRequests), + middleware.MaxWorker(static.GetDifySandboxGlobalConfigurations().MaxWorkers), + RunSandboxController, + ) + } } diff --git a/internal/controller/run.go b/internal/controller/run.go index 98e2794..4208756 100644 --- a/internal/controller/run.go +++ b/internal/controller/run.go @@ -54,3 +54,16 @@ func UpdateDependencies(c *gin.Context) { } }) } + +func RefreshDependencies(c *gin.Context) { + BindRequest(c, func(req struct { + Language string `json:"language" form:"language" binding:"required"` + }) { + switch req.Language { + case "python3": + c.JSON(200, service.RefreshPython3Dependencies()) + default: + c.JSON(400, types.ErrorResponse(-400, "unsupported language")) + } + }) +} diff --git a/internal/core/runner/python/setup.go b/internal/core/runner/python/setup.go index 68a00e3..a3f83c2 100644 --- a/internal/core/runner/python/setup.go +++ b/internal/core/runner/python/setup.go @@ -3,6 +3,7 @@ package python import ( _ "embed" "fmt" + "github.com/langgenius/dify-sandbox/internal/static" "os" "os/exec" "path" @@ -142,3 +143,14 @@ func InstallDependencies(requirements string) error { func ListDependencies() []types.Dependency { return python_dependencies.ListDependencies() } + +func RefreshDependencies() []types.Dependency { + log.Info("updating python dependencies...") + dependencies := static.GetRunnerDependencies() + err := InstallDependencies(dependencies.PythonRequirements) + if err != nil { + log.Error("failed to update python dependencies: %v", err) + } + log.Info("python dependencies updated") + return python_dependencies.ListDependencies() +} diff --git a/internal/server/server.go b/internal/server/server.go index 06daf4c..f221b55 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -33,6 +33,11 @@ func initServer() { } r := gin.Default() + r.Use(gin.Recovery()) + if gin.Mode() == gin.DebugMode { + r.Use(gin.Logger()) + } + controller.Setup(r) r.Run(fmt.Sprintf(":%d", config.App.Port)) @@ -40,8 +45,8 @@ func initServer() { func initDependencies() { log.Info("installing python dependencies...") - dependenices := static.GetRunnerDependencies() - err := python.InstallDependencies(dependenices.PythonRequirements) + dependencies := static.GetRunnerDependencies() + err := python.InstallDependencies(dependencies.PythonRequirements) if err != nil { log.Panic("failed to install python dependencies: %v", err) } @@ -59,7 +64,7 @@ func initDependencies() { ticker := time.NewTicker(30 * time.Minute) for range ticker.C { log.Info("updating python dependencies...") - err := python.InstallDependencies(dependenices.PythonRequirements) + err := python.InstallDependencies(dependencies.PythonRequirements) if err != nil { log.Error("failed to update python dependencies: %v", err) } diff --git a/internal/service/python.go b/internal/service/python.go index d703fb0..3bd6002 100644 --- a/internal/service/python.go +++ b/internal/service/python.go @@ -63,6 +63,16 @@ func ListPython3Dependencies() *types.DifySandboxResponse { }) } +type RefreshDependenciesResponse struct { + Dependencies []runner_types.Dependency `json:"dependencies"` +} + +func RefreshPython3Dependencies() *types.DifySandboxResponse { + return types.SuccessResponse(&RefreshDependenciesResponse{ + Dependencies: python.RefreshDependencies(), + }) +} + type UpdateDependenciesResponse struct{} func UpdateDependencies() *types.DifySandboxResponse { From 03053be1495f741902b2c2147fa78e6b048e8d31 Mon Sep 17 00:00:00 2001 From: "hobo.l" Date: Tue, 13 Aug 2024 09:36:39 +0300 Subject: [PATCH 2/2] health check --- internal/controller/router.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/controller/router.go b/internal/controller/router.go index eb5410e..ddc9d7a 100644 --- a/internal/controller/router.go +++ b/internal/controller/router.go @@ -14,7 +14,7 @@ func Setup(Router *gin.Engine) { PrivateGroup.Use(middleware.Auth()) { - // 健康监测 + // health check PublicGroup.GET("/health", func(c *gin.Context) { c.JSON(http.StatusOK, "ok") })