chore: Update .gitignore and Makefile for development enhancements

- Added 'logs/' and '*.pid' to .gitignore to exclude log files and process ID files from version control.
- Expanded Makefile with new development commands for easier local environment management, including 'dev-start', 'dev-stop', 'dev-restart', 'dev-logs', 'dev-status', 'dev-app', and 'dev-frontend'.
- Updated README_CN.md to include instructions for the new development mode and commands for improved developer experience.
This commit is contained in:
wizardchen
2025-11-07 15:52:52 +08:00
parent 655df49d7d
commit d387fd672f
24 changed files with 2447 additions and 644 deletions
+283
View File
@@ -0,0 +1,283 @@
# WeKnora 开发指南
## 快速开发模式(推荐)
如果你需要频繁修改 `app` 或 `frontend` 代码,**不需要每次都重新构建 Docker 镜像**,可以使用本地开发模式。
### 方式一:使用 Make 命令(推荐)
#### 1. 启动基础设施服务
```bash
make dev-start
```
这将启动以下服务的 Docker 容器:
- PostgreSQL(数据库)
- Redis(缓存)
- MinIO(对象存储)
- Neo4j(图数据库)
- DocReader(文档读取服务)
- Jaeger(链路追踪)
#### 2. 启动后端应用(新终端)
```bash
make dev-app
```
这将在本地直接运行 Go 应用,修改代码后 Ctrl+C 停止,重新运行即可。
#### 3. 启动前端(新终端)
```bash
make dev-frontend
```
这将启动 Vite 开发服务器,支持热重载,修改代码后自动刷新。
#### 4. 查看服务状态
```bash
make dev-status
```
#### 5. 停止所有服务
```bash
make dev-stop
```
### 方式二:使用脚本命令
如果你更喜欢直接使用脚本:
```bash
# 启动基础设施
./scripts/dev.sh start
# 启动后端(新终端)
./scripts/dev.sh app
# 启动前端(新终端)
./scripts/dev.sh frontend
# 查看日志
./scripts/dev.sh logs
# 停止所有服务
./scripts/dev.sh stop
```
## 访问地址
### 开发环境
- **前端开发服务器**: http://localhost:5173
- **后端 API**: http://localhost:8080
- **PostgreSQL**: localhost:5432
- **Redis**: localhost:6379
- **MinIO Console**: http://localhost:9001
- **Neo4j Browser**: http://localhost:7474
- **Jaeger UI**: http://localhost:16686
## 开发工作流对比
### ❌ 旧方式(慢)
```bash
# 每次修改代码后都需要:
sh scripts/build_images.sh -p # 重新构建镜像(很慢)
sh scripts/start_all.sh --no-pull # 重启容器
```
**耗时**:每次修改需要 2-5 分钟
### ✅ 新方式(快)
```bash
# 首次启动(只需要一次):
make dev-start
# 在另外两个终端分别运行:
make dev-app # 修改 Go 代码后 Ctrl+C 重启即可(秒级)
make dev-frontend # 修改前端代码自动热重载(无需重启)
```
**耗时**:
- 首次启动:1-2 分钟
- 后续修改后端:5-10 秒(重启 Go 应用)
- 后续修改前端:实时热重载
## 使用 Air 实现后端热重载(可选)
如果你希望后端代码修改后也能自动重启,可以安装 `air`:
### 1. 安装 Air
```bash
go install github.com/cosmtrek/air@latest
```
### 2. 创建配置文件
在项目根目录创建 `.air.toml`:
```toml
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"
[build]
args_bin = []
bin = "./tmp/main"
cmd = "go build -o ./tmp/main ./cmd/server"
delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata", "frontend", "migrations"]
exclude_file = []
exclude_regex = ["_test.go"]
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html", "yaml"]
include_file = []
kill_delay = "0s"
log = "build-errors.log"
poll = false
poll_interval = 0
rerun = false
rerun_delay = 500
send_interrupt = false
stop_on_error = false
[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"
[log]
main_only = false
time = false
[misc]
clean_on_exit = false
[screen]
clear_on_rebuild = false
keep_scroll = true
```
### 3. 使用 Air 启动
```bash
# 在项目根目录
air
```
现在修改 Go 代码后会自动重新编译和重启!
## 其他开发技巧
### 只修改前端
如果只修改前端,只需要:
```bash
cd frontend
npm run dev
```
前端会连接到 http://localhost:8080 的后端 API。
### 只修改后端
如果只修改后端,只需要:
```bash
# 启动基础设施
make dev-start
# 运行后端
make dev-app
```
### 调试模式
#### 后端调试
使用 VS Code 或 GoLand 的调试功能,配置连接到本地运行的 Go 应用。
VS Code 配置示例(`.vscode/launch.json`):
```json
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch Server",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/server",
"env": {
"DB_HOST": "localhost",
"DOCREADER_ADDR": "localhost:50051",
"MINIO_ENDPOINT": "localhost:9000",
"REDIS_ADDR": "localhost:6379",
"OTEL_EXPORTER_OTLP_ENDPOINT": "localhost:4317",
"NEO4J_URI": "bolt://localhost:7687"
},
"args": []
}
]
}
```
#### 前端调试
使用浏览器开发者工具即可,Vite 提供了 source map。
## 生产环境部署
当你完成开发需要部署时,才需要构建镜像:
```bash
# 构建所有镜像
sh scripts/build_images.sh
# 或只构建特定镜像
sh scripts/build_images.sh -p # 只构建后端
sh scripts/build_images.sh -f # 只构建前端
# 启动生产环境
sh scripts/start_all.sh
```
## 常见问题
### Q: 启动 dev-app 时报错连接不到数据库
A: 确保先运行了 `make dev-start`,并等待所有服务启动完成(大约 30 秒)。
### Q: 前端访问 API 时报 CORS 错误
A: 检查前端的代理配置,确保 `vite.config.ts` 中配置了正确的代理。
### Q: DocReader 服务需要重新构建怎么办?
A: DocReader 仍然使用 Docker 镜像,如果需要修改,需要重新构建:
```bash
sh scripts/build_images.sh -d
make dev-restart
```
## 总结
- **日常开发**:使用 `make dev-*` 命令,快速迭代
- **测试集成**:使用 `sh scripts/start_all.sh --no-pull` 测试完整环境
- **生产部署**:使用 `sh scripts/build_images.sh` + `sh scripts/start_all.sh`
+255
View File
@@ -0,0 +1,255 @@
# 快速开发模式说明
## 🎯 问题背景
之前的开发流程中,每次修改 `app`(后端)或 `frontend`(前端)代码后,都需要:
```bash
# 重新构建 Docker 镜像(耗时 2-5 分钟)
sh scripts/build_images.sh -p # 构建后端
sh scripts/build_images.sh -f # 构建前端
# 重启容器
sh scripts/start_all.sh --no-pull
```
这个流程非常耗时,严重影响开发效率。
## ✨ 解决方案
现在提供了**快速开发模式**,可以直接在本地运行应用和前端,只在 Docker 中启动基础设施服务(数据库、缓存等),实现:
- ✅ **前端热重载**:修改代码自动刷新,无需重启
- ✅ **后端快速重启**:修改代码后 5-10 秒即可重启
- ✅ **无需构建镜像**:跳过耗时的镜像构建过程
- ✅ **支持调试**:可以使用 IDE 断点调试
## 📦 新增文件
### 1. 核心配置文件
- **`docker-compose.dev.yml`** - 开发环境 Docker Compose 配置
- 只启动基础设施服务(postgres, redis, minio, neo4j, docreader, jaeger)
- 不启动 app 和 frontend 容器
### 2. 开发脚本
- **`scripts/dev.sh`** - 开发环境管理脚本
- 支持启动/停止基础设施
- 支持本地运行 app 和 frontend
- 自动检测并使用 Air(Go 热重载工具)
- **`scripts/quick-dev.sh`** - 一键启动开发环境脚本
- 交互式启动所有服务
- 自动创建日志目录
### 3. 配置文件
- **`.air.toml`** - Air 热重载配置
- 监控 Go 文件变化
- 自动重新编译和重启
- **`frontend/vite.config.ts`** - 更新的 Vite 配置
- 添加 API 代理配置
- 避免 CORS 问题
### 4. 文档
- **`DEVELOPMENT.md`** - 开发环境快速入门指南
- **`docs/开发指南.md`** - 完整开发指南
- **`docs/快速开发模式说明.md`** - 本文档
### 5. Makefile 更新
新增的 Make 命令:
- `make dev-start` - 启动开发环境基础设施
- `make dev-stop` - 停止开发环境
- `make dev-restart` - 重启开发环境
- `make dev-logs` - 查看服务日志
- `make dev-status` - 查看服务状态
- `make dev-app` - 启动后端应用(本地)
- `make dev-frontend` - 启动前端(本地)
## 🚀 使用方法
### 方式 1:使用 Make 命令(推荐)
```bash
# 终端 1:启动基础设施
make dev-start
# 终端 2:启动后端
make dev-app
# 终端 3:启动前端
make dev-frontend
```
### 方式 2:使用开发脚本
```bash
# 终端 1
./scripts/dev.sh start
# 终端 2
./scripts/dev.sh app
# 终端 3
./scripts/dev.sh frontend
```
### 方式 3:一键启动(交互式)
```bash
./scripts/quick-dev.sh
```
## 📊 效率对比
### 旧方式(慢)
| 操作 | 耗时 |
|------|------|
| 修改代码 | - |
| 重新构建镜像 | 2-5 分钟 |
| 重启容器 | 30-60 秒 |
| **总计** | **2.5-6 分钟** |
### 新方式(快)
| 操作 | 耗时 |
|------|------|
| 首次启动基础设施 | 1-2 分钟(仅一次) |
| **后续修改后端** | **5-10 秒** |
| **后续修改前端** | **实时热重载** |
**效率提升:20-60 倍!**
## 🎓 高级功能
### 使用 Air 实现后端热重载
安装 Air 后,后端代码修改会自动重新编译和重启:
```bash
# 安装 Air
go install github.com/cosmtrek/air@latest
# 确保在 PATH 中
export PATH=$PATH:$(go env GOPATH)/bin
# 使用 Air 启动(自动检测)
make dev-app
```
### VS Code 调试配置
创建 `.vscode/launch.json`:
```json
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch WeKnora Server",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/server",
"env": {
"DB_HOST": "localhost",
"DOCREADER_ADDR": "localhost:50051",
"MINIO_ENDPOINT": "localhost:9000",
"REDIS_ADDR": "localhost:6379",
"OTEL_EXPORTER_OTLP_ENDPOINT": "localhost:4317",
"NEO4J_URI": "bolt://localhost:7687"
}
}
]
}
```
## 🔄 架构说明
### 开发模式架构
```
┌─────────────────────────────────────────────────────────┐
│ 本地开发环境 │
├─────────────────────────────────────────────────────────┤
│ │
│ ┌──────────┐ ┌──────────┐ │
│ │ 后端 App │◄────────┤ 前端 UI │ │
│ │ (本地运行)│ │ (本地运行)│ │
│ │ :8080 │ │ :5173 │ │
│ └────┬─────┘ └──────────┘ │
│ │ │
│ │ 连接基础设施服务 │
│ ▼ │
│ ┌─────────────────────────────────────────────────┐ │
│ │ Docker 基础设施容器 │ │
│ ├─────────────────────────────────────────────────┤ │
│ │ PostgreSQL │ Redis │ MinIO │ Neo4j │ DocReader │ │
│ │ :5432 │ :6379 │ :9000 │ :7687 │ :50051 │ │
│ └─────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────┘
```
### 生产模式架构
```
┌─────────────────────────────────────────────────────────┐
│ Docker Compose 环境 │
├─────────────────────────────────────────────────────────┤
│ │
│ ┌──────────┐ ┌──────────┐ │
│ │ 后端 App │◄────────┤ 前端 UI │ │
│ │ (容器运行)│ │ (容器运行)│ │
│ │ :8080 │ │ :80 │ │
│ └────┬─────┘ └──────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────┐ │
│ │ 基础设施容器 │ │
│ ├─────────────────────────────────────────────────┤ │
│ │ PostgreSQL │ Redis │ MinIO │ Neo4j │ DocReader │ │
│ └─────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────┘
```
## 🎯 使用场景
### 日常开发
使用**开发模式**(`make dev-*`),享受快速迭代。
### 集成测试
使用**生产模式**(`make start-all`),测试完整环境。
### 生产部署
```bash
# 构建镜像
make build-images
# 启动服务
make start-all
```
## 🔧 故障排除
详见 [DEVELOPMENT.md](../DEVELOPMENT.md) 的故障排除部分。
## 📚 相关文档
- [开发环境快速入门](../DEVELOPMENT.md)
- [完整开发指南](./开发指南.md)
- [API 文档](./API.md)
## 💬 反馈与建议
如有问题或建议,请提交 [Issue](https://github.com/Tencent/WeKnora/issues)。