-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.sh
More file actions
executable file
·68 lines (58 loc) · 1.92 KB
/
Copy pathdev.sh
File metadata and controls
executable file
·68 lines (58 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/bash
# 本地开发启动脚本
# 启动 PostgreSQL + Redis 容器,运行数据库迁移,再并行启动前后端
# 数据库迁移由 API 服务启动时自动执行(见 internal/migrate)
set -e
echo "启动开发环境..."
# 检查并创建 .env 文件
if [ ! -f .env ]; then
echo "⚠️ 未找到 .env 文件,正在从 .env.example 创建..."
cp .env.example .env
echo "✅ 已创建 .env 文件,请根据需要修改配置(特别是 DATABASE_PASSWORD)"
fi
# 从根 .env 提取数据库连接参数(仅供下方 pg_isready 使用;
# api 进程由 config.Load 经 godotenv 自行加载根 .env,不在此 export)
DATABASE_USER=$(grep '^DATABASE_USER=' .env | cut -d= -f2-)
DATABASE_NAME=$(grep '^DATABASE_NAME=' .env | cut -d= -f2-)
# 启动 PostgreSQL 和 Redis
docker compose up -d postgres redis
# 等待数据库就绪(轮询替代脆弱的 sleep)
echo "等待数据库就绪..."
MAX_WAIT=30
WAITED=0
until docker compose exec -T postgres pg_isready -U "${DATABASE_USER}" -d "${DATABASE_NAME}" >/dev/null 2>&1; do
WAITED=$((WAITED + 1))
if [ "$WAITED" -ge "$MAX_WAIT" ]; then
echo "❌ 数据库 ${MAX_WAIT}s 内未就绪,请检查 docker compose logs postgres"
exit 1
fi
sleep 1
done
echo "✓ 数据库已就绪 (等待 ${WAITED}s)"
# 启动后端(启动时会自动执行数据库迁移)
echo "启动 Go API (支持热更新)..."
cd api
if command -v air >/dev/null 2>&1; then
air &
else
go run github.com/air-verse/air@latest &
fi
API_PID=$!
cd ..
# 启动前端
echo "启动前端开发服务器..."
cd web
pnpm dev &
WEB_PID=$!
cd ..
echo ""
echo "开发环境已启动:"
echo " 前端: http://localhost:5173"
echo " API: http://localhost:9090"
echo " 数据库: localhost:5432"
echo " Redis: localhost:6379"
echo ""
echo "按 Ctrl+C 停止所有服务"
# 等待子进程,退出时清理
trap "kill $API_PID $WEB_PID 2>/dev/null; echo '已停止服务'" EXIT
wait