暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

prepare_directory.go 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package api
  2. import (
  3. "fmt"
  4. "log"
  5. "path/filepath"
  6. "git.x2erp.com/qdy/go-base/ctx"
  7. "git.x2erp.com/qdy/go-base/model/response"
  8. "git.x2erp.com/qdy/go-svc-code/internal/opencode/container"
  9. "git.x2erp.com/qdy/go-svc-code/internal/util"
  10. )
  11. // PrepareDirectoryHandler 准备项目目录结构
  12. // 路由: POST /api/projects/:id/prepare-directory
  13. func PrepareDirectoryHandler(manager *container.InstanceManager) func(string, *ctx.RequestContext) (*response.QueryResult[interface{}], error) {
  14. return func(id string, reqCtx *ctx.RequestContext) (*response.QueryResult[interface{}], error) {
  15. log.Printf("准备项目目录请求: 项目ID=%s", id)
  16. // 验证项目ID作为目录名的合法性
  17. if err := util.ValidateProjectDirName(id); err != nil {
  18. log.Printf("项目ID验证失败: 项目=%s, 错误=%v", id, err)
  19. return ErrorResponse(fmt.Errorf("项目ID验证失败: %v", err))
  20. }
  21. // 获取基础路径
  22. basePath := manager.GetBasePath()
  23. // 创建项目目录
  24. if err := util.CreateProjectDirectory(basePath, id); err != nil {
  25. log.Printf("创建项目目录失败: 项目=%s, 错误=%v", id, err)
  26. return ErrorResponse(fmt.Errorf("创建项目目录失败: %v", err))
  27. }
  28. // 构建响应数据
  29. projectPath := util.GetProjectPath(basePath, id)
  30. configPath := util.GetOpenCodeConfigPath(basePath, id)
  31. logsPath := util.GetProjectLogsPath(basePath, id)
  32. directoryInfo := map[string]string{
  33. "project_id": id,
  34. "project_path": projectPath,
  35. "config_path": configPath,
  36. "logs_path": logsPath,
  37. "base_path": basePath,
  38. }
  39. // 返回绝对路径
  40. if absPath, err := filepath.Abs(projectPath); err == nil {
  41. directoryInfo["absolute_path"] = absPath
  42. }
  43. log.Printf("项目目录准备成功: 项目=%s, 路径=%s", id, projectPath)
  44. return SuccessResponseWithMessage(directoryInfo, "项目目录准备成功")
  45. }
  46. }