config.go 2.6 KB

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