server.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package server
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "github.com/langgenius/dify-sandbox/internal/controller"
  6. "github.com/langgenius/dify-sandbox/internal/core/runner/python"
  7. "github.com/langgenius/dify-sandbox/internal/static"
  8. "github.com/langgenius/dify-sandbox/internal/utils/log"
  9. )
  10. func initConfig() {
  11. // auto migrate database
  12. err := static.InitConfig("conf/config.yaml")
  13. if err != nil {
  14. log.Panic("failed to init config: %v", err)
  15. }
  16. log.Info("config init success")
  17. err = static.SetupRunnerDependencies()
  18. if err != nil {
  19. log.Error("failed to setup runner dependencies: %v", err)
  20. }
  21. log.Info("runner dependencies init success")
  22. }
  23. func initServer() {
  24. config := static.GetDifySandboxGlobalConfigurations()
  25. if !config.App.Debug {
  26. gin.SetMode(gin.ReleaseMode)
  27. }
  28. r := gin.Default()
  29. controller.Setup(r)
  30. r.Run(fmt.Sprintf(":%d", config.App.Port))
  31. }
  32. func initDependencies() {
  33. log.Info("installing python dependencies...")
  34. dependenices := static.GetRunnerDependencies()
  35. err := python.InstallDependencies(dependenices.PythonRequirements)
  36. if err != nil {
  37. log.Panic("failed to install python dependencies: %v", err)
  38. }
  39. log.Info("python dependencies installed")
  40. }
  41. func Run() {
  42. initConfig()
  43. initDependencies()
  44. initServer()
  45. }