kindergarten_java/start-all.sh

124 lines
3.3 KiB
Bash
Raw Normal View History

2026-02-28 16:41:39 +08:00
#!/bin/bash
# 统一启动脚本 - 启动前后端服务
BACKEND_DIR="/Users/retirado/ccProgram/reading-platform-backend"
FRONTEND_DIR="/Users/retirado/ccProgram/reading-platform-frontend"
echo "======================================"
echo " 幼儿阅读教学服务平台"
echo " 统一启动脚本"
echo "======================================"
echo ""
# 函数:检查端口是否被占用
check_port() {
local port=$1
local name=$2
if lsof -ti:$port > /dev/null 2>&1; then
echo "⚠️ 端口 $port 已被占用 ($name)"
read -p "是否停止并重启? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
lsof -ti:$port | xargs kill -9 2>/dev/null
echo "✅ 已停止占用端口 $port 的进程"
return 0
else
return 1
fi
fi
return 0
}
# 检查并停止占用端口的进程
check_port 3000 "后端"
if [ $? -eq 1 ]; then
echo "❌ 后端启动取消"
exit 1
fi
check_port 5173 "前端"
if [ $? -eq 1 ]; then
echo "❌ 前端启动取消"
exit 1
fi
# 启动后端
echo ""
echo "📦 [1/2] 启动后端服务..."
cd "$BACKEND_DIR" || exit 1
echo "📂 后端目录: $(pwd)"
if [ ! -d "node_modules" ]; then
echo "📦 正在安装后端依赖..."
npm install
fi
# 后台启动后端
nohup npm run start:dev > /tmp/reading-platform-backend.log 2>&1 &
BACKEND_PID=$!
echo "✅ 后端启动成功 (PID: $BACKEND_PID)"
echo "📄 日志文件: /tmp/reading-platform-backend.log"
# 等待后端启动
echo "⏳ 等待后端服务启动..."
for i in {1..30}; do
if curl -s http://localhost:3000/api/v1/courses > /dev/null 2>&1; then
echo "✅ 后端服务就绪"
break
fi
if [ $i -eq 30 ]; then
echo "❌ 后端服务启动超时"
echo "📄 请查看日志: tail -f /tmp/reading-platform-backend.log"
exit 1
fi
sleep 1
done
# 启动前端
echo ""
echo "📦 [2/2] 启动前端服务..."
cd "$FRONTEND_DIR" || exit 1
echo "📂 前端目录: $(pwd)"
if [ ! -d "node_modules" ]; then
echo "📦 正在安装前端依赖..."
npm install
fi
# 后台启动前端
nohup npm run dev > /tmp/reading-platform-frontend.log 2>&1 &
FRONTEND_PID=$!
echo "✅ 前端启动成功 (PID: $FRONTEND_PID)"
echo "📄 日志文件: /tmp/reading-platform-frontend.log"
# 等待前端启动
echo "⏳ 等待前端服务启动..."
for i in {1..30}; do
if curl -s http://localhost:5173 > /dev/null 2>&1; then
echo "✅ 前端服务就绪"
break
fi
if [ $i -eq 30 ]; then
echo "⚠️ 前端服务可能需要更多时间启动"
echo "📄 请查看日志: tail -f /tmp/reading-platform-frontend.log"
break
fi
sleep 1
done
echo ""
echo "======================================"
echo " ✅ 所有服务启动成功!"
echo "======================================"
echo ""
echo "📍 后端 API: http://localhost:3000"
echo "📍 前端页面: http://localhost:5173"
echo "📍 API 文档: http://localhost:3000/api/v1"
echo ""
echo "📊 查看后端日志: tail -f /tmp/reading-platform-backend.log"
echo "📊 查看前端日志: tail -f /tmp/reading-platform-frontend.log"
echo ""
echo "🛑 停止所有服务: pkill -f 'npm run start:dev' && pkill -f 'vite'"
echo ""