config.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package static
  2. import (
  3. "os"
  4. "strconv"
  5. "strings"
  6. "github.com/langgenius/dify-sandbox/internal/types"
  7. "github.com/langgenius/dify-sandbox/internal/utils/log"
  8. "gopkg.in/yaml.v3"
  9. )
  10. var difySandboxGlobalConfigurations types.DifySandboxGlobalConfigurations
  11. func InitConfig(path string) error {
  12. difySandboxGlobalConfigurations = types.DifySandboxGlobalConfigurations{}
  13. // read config file
  14. configFile, err := os.Open(path)
  15. if err != nil {
  16. return err
  17. }
  18. defer configFile.Close()
  19. // parse config file
  20. decoder := yaml.NewDecoder(configFile)
  21. err = decoder.Decode(&difySandboxGlobalConfigurations)
  22. if err != nil {
  23. return err
  24. }
  25. debug, err := strconv.ParseBool(os.Getenv("DEBUG"))
  26. if err == nil {
  27. difySandboxGlobalConfigurations.App.Debug = debug
  28. }
  29. max_workers := os.Getenv("MAX_WORKERS")
  30. if max_workers != "" {
  31. difySandboxGlobalConfigurations.MaxWorkers, _ = strconv.Atoi(max_workers)
  32. }
  33. max_requests := os.Getenv("MAX_REQUESTS")
  34. if max_requests != "" {
  35. difySandboxGlobalConfigurations.MaxRequests, _ = strconv.Atoi(max_requests)
  36. }
  37. port := os.Getenv("SANDBOX_PORT")
  38. if port != "" {
  39. difySandboxGlobalConfigurations.App.Port, _ = strconv.Atoi(port)
  40. }
  41. timeout := os.Getenv("WORKER_TIMEOUT")
  42. if timeout != "" {
  43. difySandboxGlobalConfigurations.WorkerTimeout, _ = strconv.Atoi(timeout)
  44. }
  45. api_key := os.Getenv("API_KEY")
  46. if api_key != "" {
  47. difySandboxGlobalConfigurations.App.Key = api_key
  48. }
  49. python_path := os.Getenv("PYTHON_PATH")
  50. if python_path != "" {
  51. difySandboxGlobalConfigurations.PythonPath = python_path
  52. }
  53. if difySandboxGlobalConfigurations.PythonPath == "" {
  54. difySandboxGlobalConfigurations.PythonPath = "/usr/local/bin/python3"
  55. }
  56. python_lib_path := os.Getenv("PYTHON_LIB_PATH")
  57. if python_lib_path != "" {
  58. difySandboxGlobalConfigurations.PythonLibPaths = strings.Split(python_lib_path, ",")
  59. }
  60. if len(difySandboxGlobalConfigurations.PythonLibPaths) == 0 {
  61. difySandboxGlobalConfigurations.PythonLibPaths = DEFAULT_PYTHON_LIB_REQUIREMENTS
  62. }
  63. nodejs_path := os.Getenv("NODEJS_PATH")
  64. if nodejs_path != "" {
  65. difySandboxGlobalConfigurations.NodejsPath = nodejs_path
  66. }
  67. if difySandboxGlobalConfigurations.NodejsPath == "" {
  68. difySandboxGlobalConfigurations.NodejsPath = "/usr/local/bin/node"
  69. }
  70. enable_network := os.Getenv("ENABLE_NETWORK")
  71. if enable_network != "" {
  72. difySandboxGlobalConfigurations.EnableNetwork, _ = strconv.ParseBool(enable_network)
  73. }
  74. if difySandboxGlobalConfigurations.EnableNetwork {
  75. log.Info("network has been enabled")
  76. socks5_proxy := os.Getenv("SOCKS5_PROXY")
  77. if socks5_proxy != "" {
  78. difySandboxGlobalConfigurations.Proxy.Socks5 = socks5_proxy
  79. }
  80. if difySandboxGlobalConfigurations.Proxy.Socks5 != "" {
  81. log.Info("using socks5 proxy: %s", difySandboxGlobalConfigurations.Proxy.Socks5)
  82. }
  83. https_proxy := os.Getenv("HTTPS_PROXY")
  84. if https_proxy != "" {
  85. difySandboxGlobalConfigurations.Proxy.Https = https_proxy
  86. }
  87. if difySandboxGlobalConfigurations.Proxy.Https != "" {
  88. log.Info("using https proxy: %s", difySandboxGlobalConfigurations.Proxy.Https)
  89. }
  90. http_proxy := os.Getenv("HTTP_PROXY")
  91. if http_proxy != "" {
  92. difySandboxGlobalConfigurations.Proxy.Http = http_proxy
  93. }
  94. if difySandboxGlobalConfigurations.Proxy.Http != "" {
  95. log.Info("using http proxy: %s", difySandboxGlobalConfigurations.Proxy.Http)
  96. }
  97. }
  98. return nil
  99. }
  100. // avoid global modification, use value copy instead
  101. func GetDifySandboxGlobalConfigurations() types.DifySandboxGlobalConfigurations {
  102. return difySandboxGlobalConfigurations
  103. }
  104. type RunnerDependencies struct {
  105. PythonRequirements string
  106. }
  107. var runnerDependencies RunnerDependencies
  108. func GetRunnerDependencies() RunnerDependencies {
  109. return runnerDependencies
  110. }
  111. func SetupRunnerDependencies() error {
  112. file, err := os.ReadFile("dependencies/python-requirements.txt")
  113. if err != nil {
  114. if err == os.ErrNotExist {
  115. return nil
  116. }
  117. return err
  118. }
  119. runnerDependencies.PythonRequirements = string(file)
  120. return nil
  121. }