server.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package server
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/gin-gonic/gin"
  6. "github.com/langgenius/dify-sandbox/internal/controller"
  7. "github.com/langgenius/dify-sandbox/internal/core/runner/python"
  8. "github.com/langgenius/dify-sandbox/internal/static"
  9. "github.com/langgenius/dify-sandbox/internal/utils/log"
  10. )
  11. func initConfig() {
  12. // auto migrate database
  13. err := static.InitConfig("conf/config.yaml")
  14. if err != nil {
  15. log.Panic("failed to init config: %v", err)
  16. }
  17. log.Info("config init success")
  18. err = static.SetupRunnerDependencies()
  19. if err != nil {
  20. log.Error("failed to setup runner dependencies: %v", err)
  21. }
  22. log.Info("runner dependencies init success")
  23. }
  24. func initServer() {
  25. config := static.GetDifySandboxGlobalConfigurations()
  26. if !config.App.Debug {
  27. gin.SetMode(gin.ReleaseMode)
  28. }
  29. r := gin.Default()
  30. r.Use(gin.Recovery())
  31. if gin.Mode() == gin.DebugMode {
  32. r.Use(gin.Logger())
  33. }
  34. controller.Setup(r)
  35. r.Run(fmt.Sprintf(":%d", config.App.Port))
  36. }
  37. func initDependencies() {
  38. log.Info("installing python dependencies...")
  39. dependencies := static.GetRunnerDependencies()
  40. err := python.InstallDependencies(dependencies.PythonRequirements)
  41. if err != nil {
  42. log.Panic("failed to install python dependencies: %v", err)
  43. }
  44. log.Info("python dependencies installed")
  45. log.Info("initializing python dependencies sandbox...")
  46. err = python.PreparePythonDependenciesEnv()
  47. if err != nil {
  48. log.Panic("failed to initialize python dependencies sandbox: %v", err)
  49. }
  50. log.Info("python dependencies sandbox initialized")
  51. // start a ticker to update python dependencies every 30 minutes to keep the sandbox up-to-date
  52. go func() {
  53. ticker := time.NewTicker(30 * time.Minute)
  54. for range ticker.C {
  55. if err:=updatePythonDependencies(dependencies);err!=nil{
  56. log.Error("Failed to update Python dependencies: %v", err)
  57. }
  58. }
  59. }()
  60. }
  61. func updatePythonDependencies(dependencies static.RunnerDependencies) error {
  62. log.Info("Updating Python dependencies...")
  63. if err := python.InstallDependencies(dependencies.PythonRequirements); err != nil {
  64. log.Error("Failed to install Python dependencies: %v", err)
  65. return err
  66. }
  67. if err := python.PreparePythonDependenciesEnv(); err != nil {
  68. log.Error("Failed to prepare Python dependencies environment: %v", err)
  69. return err
  70. }
  71. log.Info("Python dependencies updated successfully.")
  72. return nil
  73. }
  74. func Run() {
  75. // init config
  76. initConfig()
  77. // init dependencies, it will cost some times
  78. go initDependencies()
  79. initServer()
  80. }