cockpit-source/.trae/documents/plan-启动项目.md
2026-04-02 14:12:43 +08:00

70 lines
3.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 计划:读取 README 并启动项目backend + frontend
## Summary
- 目标:在本机启动该仓库的后端 APIGo/Gin/Gorm与前端管理台Vite/Vue3确保可以用默认管理员账号登录并正常调用 API。
- 成功标准:
- 后端可访问 `http://localhost:8080`,且能成功登录获取 token`/api/auth/login` 返回 200
- 前端可访问 `http://localhost:5173`,可使用 `admin/admin123` 登录并进入页面。
## Current State Analysis基于仓库现状
- 仓库结构:
- `backend/`Go 1.22Gin 路由挂在 `/api/*`[main.go](file:///e:/private_basic/cockpit-source/backend/cmd/server/main.go) 启动时会连接 DB 并执行 AutoMigrate + Seed默认管理员、权限、默认状态
- `frontend/`Vite + Vue3[package.json](file:///e:/private_basic/cockpit-source/frontend/package.json) 提供 `npm run dev` 启动开发服务器。
- 配置方式:
- 后端配置:优先读取 `backend/configs/config.yaml`不存在也可使用默认值DSN 默认 `root:root@tcp(127.0.0.1:3306)/cockpit...`),见 [config.go](file:///e:/private_basic/cockpit-source/backend/internal/config/config.go)。
- 前端 API Base默认 `VITE_API_BASE=http://localhost:8080`(可在 `.env.local` 覆盖),见 [frontend/.env.example](file:///e:/private_basic/cockpit-source/frontend/.env.example)。
- 依赖前置:
- MySQL 必须可用,并且目标库 `cockpit` 存在README 提示)。
## Proposed Changes执行步骤只启动不改业务代码
### 1) 数据库准备MySQL
- 确保本机 MySQL 在 `127.0.0.1:3306` 可连接。
- 创建数据库(示例):
- `CREATE DATABASE cockpit DEFAULT CHARACTER SET utf8mb4;`
- DSN 决策(选一种):
- A. 使用默认 DSN确保本机存在 `root/root` 账号并可访问 `cockpit` 库。
- B. 使用你本机的 MySQL 账号:修改后端配置的 `db.dsn` 为你的实际账号密码/主机。
### 2) 后端启动Go
- 配置(推荐显式配置,避免默认弱密钥):
- 从示例复制一份配置文件到实际配置Windows PowerShell 示例):
- `Copy-Item backend\configs\config.example.yaml backend\configs\config.yaml`
- 编辑 `backend/configs/config.yaml`
- `db.dsn`:指向可用的 MySQL DSN
- `auth.accessTokenSecret` / `auth.refreshTokenSecret`:替换为强随机串
- 启动命令(在仓库根目录下开新终端):
- `cd backend`
- `go mod tidy`
- `go run ./cmd/server`
- 预期结果:
- 监听 `http://localhost:8080`
- 首次启动会 AutoMigrate + Seed含默认管理员admin/admin123
### 3) 前端启动Node
- 配置(如需改后端地址):
- 从示例复制 `.env.local`(可选):
- `Copy-Item frontend\.env.example frontend\.env.local`
- 确认 `VITE_API_BASE=http://localhost:8080`
- 启动命令(另开新终端):
- `cd frontend`
- `npm install`
- `npm run dev`
- 预期结果:
- 打开 `http://localhost:5173`
- 使用默认账号 `admin/admin123` 登录
## Assumptions & Decisions
- 同时启动 `backend``frontend`README 指向的“对应项目”按全栈运行理解)。
- 采用本机 MySQL仓库未提供 docker-compose / 容器化启动脚本)。
- 后端密钥在本地开发环境允许临时使用弱默认值,但计划中仍按安全推荐要求替换为强随机串。
## Verification启动后验证方式
- 后端进程日志无 panic且在浏览器/命令行可访问:
- `POST http://localhost:8080/api/auth/login`body`{"username":"admin","password":"admin123"}`
- 返回包含 access/refresh token或类似字段并且后续 `GET /api/me` 携带 `Authorization: Bearer <token>` 返回 200。
- 前端:
- `http://localhost:5173` 能打开
- 登录成功后能进入总览/订单等页面(至少不报跨域与 401 循环刷新)