config.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package static
  2. import (
  3. "os"
  4. "strconv"
  5. "github.com/langgenius/dify-sandbox/internal/types"
  6. "github.com/langgenius/dify-sandbox/internal/utils/log"
  7. "gopkg.in/yaml.v3"
  8. )
  9. var difySandboxGlobalConfigurations types.DifySandboxGlobalConfigurations
  10. func InitConfig(path string) error {
  11. difySandboxGlobalConfigurations = types.DifySandboxGlobalConfigurations{}
  12. // read config file
  13. configFile, err := os.Open(path)
  14. if err != nil {
  15. return err
  16. }
  17. defer configFile.Close()
  18. // parse config file
  19. decoder := yaml.NewDecoder(configFile)
  20. err = decoder.Decode(&difySandboxGlobalConfigurations)
  21. if err != nil {
  22. return err
  23. }
  24. debug, err := strconv.ParseBool(os.Getenv("DEBUG"))
  25. if err == nil {
  26. difySandboxGlobalConfigurations.App.Debug = debug
  27. }
  28. max_workers := os.Getenv("MAX_WORKERS")
  29. if max_workers != "" {
  30. difySandboxGlobalConfigurations.MaxWorkers, _ = strconv.Atoi(max_workers)
  31. }
  32. max_requests := os.Getenv("MAX_REQUESTS")
  33. if max_requests != "" {
  34. difySandboxGlobalConfigurations.MaxRequests, _ = strconv.Atoi(max_requests)
  35. }
  36. port := os.Getenv("SANDBOX_PORT")
  37. if port != "" {
  38. difySandboxGlobalConfigurations.App.Port, _ = strconv.Atoi(port)
  39. }
  40. timeout := os.Getenv("WORKER_TIMEOUT")
  41. if timeout != "" {
  42. difySandboxGlobalConfigurations.WorkerTimeout, _ = strconv.Atoi(timeout)
  43. }
  44. api_key := os.Getenv("API_KEY")
  45. if api_key != "" {
  46. difySandboxGlobalConfigurations.App.Key = api_key
  47. }
  48. python_path := os.Getenv("PYTHON_PATH")
  49. if python_path != "" {
  50. difySandboxGlobalConfigurations.PythonPath = python_path
  51. }
  52. if difySandboxGlobalConfigurations.PythonPath == "" {
  53. difySandboxGlobalConfigurations.PythonPath = "/usr/local/bin/python3"
  54. }
  55. nodejs_path := os.Getenv("NODEJS_PATH")
  56. if nodejs_path != "" {
  57. difySandboxGlobalConfigurations.NodejsPath = nodejs_path
  58. }
  59. if difySandboxGlobalConfigurations.NodejsPath == "" {
  60. difySandboxGlobalConfigurations.NodejsPath = "/usr/local/bin/node"
  61. }
  62. enable_network := os.Getenv("ENABLE_NETWORK")
  63. if enable_network != "" {
  64. difySandboxGlobalConfigurations.EnableNetwork, _ = strconv.ParseBool(enable_network)
  65. }
  66. if difySandboxGlobalConfigurations.EnableNetwork {
  67. log.Info("network has been enabled")
  68. }
  69. return nil
  70. }
  71. // avoid global modification, use value copy instead
  72. func GetDifySandboxGlobalConfigurations() types.DifySandboxGlobalConfigurations {
  73. return difySandboxGlobalConfigurations
  74. }
  75. type RunnerDependencies struct {
  76. PythonRequirements string
  77. }
  78. var runnerDependencies RunnerDependencies
  79. func GetRunnerDependencies() RunnerDependencies {
  80. return runnerDependencies
  81. }
  82. func SetupRunnerDependencies() error {
  83. file, err := os.ReadFile("dependencies/python-requirements.txt")
  84. if err != nil {
  85. return err
  86. }
  87. runnerDependencies.PythonRequirements = string(file)
  88. return nil
  89. }