server.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 to keep the sandbox up-to-date
  52. go func() {
  53. updateInterval := static.GetDifySandboxGlobalConfigurations().PythonDepsUpdateInterval
  54. tickerDuration, err := time.ParseDuration(updateInterval)
  55. if err != nil {
  56. log.Error("failed to parse python dependencies update interval, skip periodic updates: %v", err)
  57. return
  58. }
  59. ticker := time.NewTicker(tickerDuration)
  60. for range ticker.C {
  61. if err:=updatePythonDependencies(dependencies);err!=nil{
  62. log.Error("Failed to update Python dependencies: %v", err)
  63. }
  64. }
  65. }()
  66. }
  67. func updatePythonDependencies(dependencies static.RunnerDependencies) error {
  68. log.Info("Updating Python dependencies...")
  69. if err := python.InstallDependencies(dependencies.PythonRequirements); err != nil {
  70. log.Error("Failed to install Python dependencies: %v", err)
  71. return err
  72. }
  73. if err := python.PreparePythonDependenciesEnv(); err != nil {
  74. log.Error("Failed to prepare Python dependencies environment: %v", err)
  75. return err
  76. }
  77. log.Info("Python dependencies updated successfully.")
  78. return nil
  79. }
  80. func Run() {
  81. // init config
  82. initConfig()
  83. // init dependencies, it will cost some times
  84. go initDependencies()
  85. initServer()
  86. }