浏览代码

Support pip other mirror url. (#52)

Co-authored-by: wuhan1 <wuhan1@360.cn>
WuHan 1 年之前
父节点
当前提交
2f7bfa0036
共有 3 个文件被更改,包括 22 次插入10 次删除
  1. 8 1
      internal/core/runner/python/setup.go
  2. 4 0
      internal/static/config.go
  3. 10 9
      internal/types/config.go

+ 8 - 1
internal/core/runner/python/setup.go

@@ -98,8 +98,15 @@ func InstallDependencies(requirements string) error {
 		}
 
 		// install dependencies
-		cmd := exec.Command("pip3", "install", "-r", "requirements.txt")
+		pipMirrorURL := static.GetDifySandboxGlobalConfigurations().PythonPipMirrorURL
 
+		// Create the base command
+		args := []string{"install", "-r", "requirements.txt"}
+		if pipMirrorURL != "" {
+			// If a mirror URL is provided, include it in the command arguments
+			args = append(args, "-i", pipMirrorURL)
+		}
+		cmd := exec.Command("pip3", args...)
 		reader, err := cmd.StdoutPipe()
 		if err != nil {
 			log.Panic("failed to get stdout pipe of pip3")

+ 4 - 0
internal/static/config.go

@@ -78,6 +78,10 @@ func InitConfig(path string) error {
 		difySandboxGlobalConfigurations.PythonLibPaths = DEFAULT_PYTHON_LIB_REQUIREMENTS
 	}
 
+	python_pip_mirror_url := os.Getenv("PIP_MIRROR_URL")
+	if python_pip_mirror_url != "" {
+		difySandboxGlobalConfigurations.PythonPipMirrorURL = python_pip_mirror_url
+	}
 	nodejs_path := os.Getenv("NODEJS_PATH")
 	if nodejs_path != "" {
 		difySandboxGlobalConfigurations.NodejsPath = nodejs_path

+ 10 - 9
internal/types/config.go

@@ -6,15 +6,16 @@ type DifySandboxGlobalConfigurations struct {
 		Debug bool   `yaml:"debug"`
 		Key   string `yaml:"key"`
 	} `yaml:"app"`
-	MaxWorkers      int      `yaml:"max_workers"`
-	MaxRequests     int      `yaml:"max_requests"`
-	WorkerTimeout   int      `yaml:"worker_timeout"`
-	PythonPath      string   `yaml:"python_path"`
-	PythonLibPaths  []string `yaml:"python_lib_path"`
-	NodejsPath      string   `yaml:"nodejs_path"`
-	EnableNetwork   bool     `yaml:"enable_network"`
-	AllowedSyscalls []int    `yaml:"allowed_syscalls"`
-	Proxy           struct {
+	MaxWorkers         int      `yaml:"max_workers"`
+	MaxRequests        int      `yaml:"max_requests"`
+	WorkerTimeout      int      `yaml:"worker_timeout"`
+	PythonPath         string   `yaml:"python_path"`
+	PythonLibPaths     []string `yaml:"python_lib_path"`
+	PythonPipMirrorURL string   `yaml:"python_pip_mirror_url"`
+	NodejsPath         string   `yaml:"nodejs_path"`
+	EnableNetwork      bool     `yaml:"enable_network"`
+	AllowedSyscalls    []int    `yaml:"allowed_syscalls"`
+	Proxy              struct {
 		Socks5 string `yaml:"socks5"`
 		Https  string `yaml:"https"`
 		Http   string `yaml:"http"`