config.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. socks5_proxy := os.Getenv("SOCKS5_PROXY")
  69. if socks5_proxy != "" {
  70. difySandboxGlobalConfigurations.Proxy.Socks5 = socks5_proxy
  71. }
  72. if difySandboxGlobalConfigurations.Proxy.Socks5 != "" {
  73. log.Info("using socks5 proxy: %s", difySandboxGlobalConfigurations.Proxy.Socks5)
  74. }
  75. https_proxy := os.Getenv("HTTPS_PROXY")
  76. if https_proxy != "" {
  77. difySandboxGlobalConfigurations.Proxy.Https = https_proxy
  78. }
  79. if difySandboxGlobalConfigurations.Proxy.Https != "" {
  80. log.Info("using https proxy: %s", difySandboxGlobalConfigurations.Proxy.Https)
  81. }
  82. http_proxy := os.Getenv("HTTP_PROXY")
  83. if http_proxy != "" {
  84. difySandboxGlobalConfigurations.Proxy.Http = http_proxy
  85. }
  86. if difySandboxGlobalConfigurations.Proxy.Http != "" {
  87. log.Info("using http proxy: %s", difySandboxGlobalConfigurations.Proxy.Http)
  88. }
  89. }
  90. return nil
  91. }
  92. // avoid global modification, use value copy instead
  93. func GetDifySandboxGlobalConfigurations() types.DifySandboxGlobalConfigurations {
  94. return difySandboxGlobalConfigurations
  95. }
  96. type RunnerDependencies struct {
  97. PythonRequirements string
  98. }
  99. var runnerDependencies RunnerDependencies
  100. func GetRunnerDependencies() RunnerDependencies {
  101. return runnerDependencies
  102. }
  103. func SetupRunnerDependencies() error {
  104. file, err := os.ReadFile("dependencies/python-requirements.txt")
  105. if err != nil {
  106. if err == os.ErrNotExist {
  107. return nil
  108. }
  109. return err
  110. }
  111. runnerDependencies.PythonRequirements = string(file)
  112. return nil
  113. }