1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- package server
- import (
- "fmt"
- "time"
- "github.com/gin-gonic/gin"
- "github.com/langgenius/dify-sandbox/internal/controller"
- "github.com/langgenius/dify-sandbox/internal/core/runner/python"
- "github.com/langgenius/dify-sandbox/internal/static"
- "github.com/langgenius/dify-sandbox/internal/utils/log"
- )
- func initConfig() {
- // auto migrate database
- err := static.InitConfig("conf/config.yaml")
- if err != nil {
- log.Panic("failed to init config: %v", err)
- }
- log.Info("config init success")
- err = static.SetupRunnerDependencies()
- if err != nil {
- log.Error("failed to setup runner dependencies: %v", err)
- }
- log.Info("runner dependencies init success")
- }
- func initServer() {
- config := static.GetDifySandboxGlobalConfigurations()
- if !config.App.Debug {
- gin.SetMode(gin.ReleaseMode)
- }
- 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))
- }
- func initDependencies() {
- log.Info("installing python dependencies...")
- dependencies := static.GetRunnerDependencies()
- err := python.InstallDependencies(dependencies.PythonRequirements)
- if err != nil {
- log.Panic("failed to install python dependencies: %v", err)
- }
- log.Info("python dependencies installed")
- log.Info("initializing python dependencies sandbox...")
- err = python.PreparePythonDependenciesEnv()
- if err != nil {
- log.Panic("failed to initialize python dependencies sandbox: %v", err)
- }
- log.Info("python dependencies sandbox initialized")
- // start a ticker to update python dependencies every 30 minutes to keep the sandbox up-to-date
- go func() {
- ticker := time.NewTicker(30 * time.Minute)
- for range ticker.C {
- if err:=updatePythonDependencies(dependencies);err!=nil{
- log.Error("Failed to update Python dependencies: %v", err)
- }
- }
- }()
- }
- func updatePythonDependencies(dependencies static.RunnerDependencies) error {
- log.Info("Updating Python dependencies...")
- if err := python.InstallDependencies(dependencies.PythonRequirements); err != nil {
- log.Error("Failed to install Python dependencies: %v", err)
- return err
- }
- if err := python.PreparePythonDependenciesEnv(); err != nil {
- log.Error("Failed to prepare Python dependencies environment: %v", err)
- return err
- }
- log.Info("Python dependencies updated successfully.")
- return nil
- }
- func Run() {
- // init config
- initConfig()
- // init dependencies, it will cost some times
- go initDependencies()
- initServer()
- }
|