package api import ( "fmt" "log" "path/filepath" "git.x2erp.com/qdy/go-base/ctx" "git.x2erp.com/qdy/go-base/model/response" "git.x2erp.com/qdy/go-svc-code/internal/opencode/container" "git.x2erp.com/qdy/go-svc-code/internal/util" ) // PrepareDirectoryHandler 准备项目目录结构 // 路由: POST /api/projects/:id/prepare-directory func PrepareDirectoryHandler(manager *container.InstanceManager) func(string, *ctx.RequestContext) (*response.QueryResult[interface{}], error) { return func(id string, reqCtx *ctx.RequestContext) (*response.QueryResult[interface{}], error) { log.Printf("准备项目目录请求: 项目ID=%s", id) // 验证项目ID作为目录名的合法性 if err := util.ValidateProjectDirName(id); err != nil { log.Printf("项目ID验证失败: 项目=%s, 错误=%v", id, err) return ErrorResponse(fmt.Errorf("项目ID验证失败: %v", err)) } // 获取基础路径 basePath := manager.GetBasePath() // 创建项目目录 if err := util.CreateProjectDirectory(basePath, id); err != nil { log.Printf("创建项目目录失败: 项目=%s, 错误=%v", id, err) return ErrorResponse(fmt.Errorf("创建项目目录失败: %v", err)) } // 构建响应数据 projectPath := util.GetProjectPath(basePath, id) configPath := util.GetOpenCodeConfigPath(basePath, id) logsPath := util.GetProjectLogsPath(basePath, id) directoryInfo := map[string]string{ "project_id": id, "project_path": projectPath, "config_path": configPath, "logs_path": logsPath, "base_path": basePath, } // 返回绝对路径 if absPath, err := filepath.Abs(projectPath); err == nil { directoryInfo["absolute_path"] = absPath } log.Printf("项目目录准备成功: 项目=%s, 路径=%s", id, projectPath) return SuccessResponseWithMessage(directoryInfo, "项目目录准备成功") } }