config.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. python_pip_mirror_url := os.Getenv("PIP_MIRROR_URL")
  64. if python_pip_mirror_url != "" {
  65. difySandboxGlobalConfigurations.PythonPipMirrorURL = python_pip_mirror_url
  66. }
  67. python_deps_update_interval := os.Getenv("PYTHON_DEPS_UPDATE_INTERVAL")
  68. if python_deps_update_interval != "" {
  69. difySandboxGlobalConfigurations.PythonDepsUpdateInterval = python_deps_update_interval
  70. }
  71. // if not set "PythonDepsUpdateInterval", update python dependencies every 30 minutes to keep the sandbox up-to-date
  72. if difySandboxGlobalConfigurations.PythonDepsUpdateInterval == "" {
  73. difySandboxGlobalConfigurations.PythonDepsUpdateInterval = "30m"
  74. }
  75. nodejs_path := os.Getenv("NODEJS_PATH")
  76. if nodejs_path != "" {
  77. difySandboxGlobalConfigurations.NodejsPath = nodejs_path
  78. }
  79. if difySandboxGlobalConfigurations.NodejsPath == "" {
  80. difySandboxGlobalConfigurations.NodejsPath = "/usr/local/bin/node"
  81. }
  82. enable_network := os.Getenv("ENABLE_NETWORK")
  83. if enable_network != "" {
  84. difySandboxGlobalConfigurations.EnableNetwork, _ = strconv.ParseBool(enable_network)
  85. }
  86. enable_preload := os.Getenv("ENABLE_PRELOAD")
  87. if enable_preload != "" {
  88. difySandboxGlobalConfigurations.EnablePreload, _ = strconv.ParseBool(enable_preload)
  89. }
  90. allowed_syscalls := os.Getenv("ALLOWED_SYSCALLS")
  91. if allowed_syscalls != "" {
  92. strs := strings.Split(allowed_syscalls, ",")
  93. ary := make([]int, len(strs))
  94. for i := range ary {
  95. ary[i], err = strconv.Atoi(strs[i])
  96. if err != nil {
  97. return err
  98. }
  99. }
  100. difySandboxGlobalConfigurations.AllowedSyscalls = ary
  101. }
  102. if difySandboxGlobalConfigurations.EnableNetwork {
  103. log.Info("network has been enabled")
  104. socks5_proxy := os.Getenv("SOCKS5_PROXY")
  105. if socks5_proxy != "" {
  106. difySandboxGlobalConfigurations.Proxy.Socks5 = socks5_proxy
  107. }
  108. if difySandboxGlobalConfigurations.Proxy.Socks5 != "" {
  109. log.Info("using socks5 proxy: %s", difySandboxGlobalConfigurations.Proxy.Socks5)
  110. }
  111. https_proxy := os.Getenv("HTTPS_PROXY")
  112. if https_proxy != "" {
  113. difySandboxGlobalConfigurations.Proxy.Https = https_proxy
  114. }
  115. if difySandboxGlobalConfigurations.Proxy.Https != "" {
  116. log.Info("using https proxy: %s", difySandboxGlobalConfigurations.Proxy.Https)
  117. }
  118. http_proxy := os.Getenv("HTTP_PROXY")
  119. if http_proxy != "" {
  120. difySandboxGlobalConfigurations.Proxy.Http = http_proxy
  121. }
  122. if difySandboxGlobalConfigurations.Proxy.Http != "" {
  123. log.Info("using http proxy: %s", difySandboxGlobalConfigurations.Proxy.Http)
  124. }
  125. }
  126. return nil
  127. }
  128. // avoid global modification, use value copy instead
  129. func GetDifySandboxGlobalConfigurations() types.DifySandboxGlobalConfigurations {
  130. return difySandboxGlobalConfigurations
  131. }
  132. type RunnerDependencies struct {
  133. PythonRequirements string
  134. }
  135. var runnerDependencies RunnerDependencies
  136. func GetRunnerDependencies() RunnerDependencies {
  137. return runnerDependencies
  138. }
  139. func SetupRunnerDependencies() error {
  140. file, err := os.ReadFile("dependencies/python-requirements.txt")
  141. if err != nil {
  142. if err == os.ErrNotExist {
  143. return nil
  144. }
  145. return err
  146. }
  147. runnerDependencies.PythonRequirements = string(file)
  148. return nil
  149. }