topfans/backend/start-with-swagger.sh
2026-04-07 22:29:48 +08:00

81 lines
1.9 KiB
Bash
Executable File
Raw Permalink 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.

#!/bin/bash
# 快速启动 Gateway 服务并测试 Swagger
# 用途:启动 Gateway 服务并自动打开 Swagger UI
set -e
echo "======================================"
echo "启动 Gateway 服务并测试 Swagger"
echo "======================================"
# 进入 gateway 目录
cd backend/gateway
# 检查是否已生成 Swagger 文档
if [ ! -f "docs/swagger.json" ]; then
echo ""
echo "⚠️ Swagger 文档不存在,正在生成..."
cd ..
bash gen-swagger.sh
cd gateway
fi
echo ""
echo "🚀 正在启动 Gateway 服务..."
echo ""
# 检查端口 3000 是否被占用
if lsof -Pi :3000 -sTCP:LISTEN -t >/dev/null 2>&1; then
echo "⚠️ 端口 3000 已被占用,请先停止正在运行的服务"
echo " 可以使用: lsof -ti:3000 | xargs kill -9"
exit 1
fi
# 启动 Gateway 服务(后台运行)
nohup ./gateway > /tmp/gateway.log 2>&1 &
GATEWAY_PID=$!
echo "✅ Gateway 服务已启动 (PID: $GATEWAY_PID)"
echo ""
# 等待服务启动
echo "⏳ 等待服务启动..."
sleep 3
# 检查服务是否正常
if curl -s http://localhost:3000/health > /dev/null 2>&1; then
echo "✅ 服务健康检查通过"
else
echo "❌ 服务启动失败,请查看日志: /tmp/gateway.log"
kill $GATEWAY_PID 2>/dev/null
exit 1
fi
echo ""
echo "======================================"
echo "✅ Gateway 服务启动成功!"
echo "======================================"
echo ""
echo "📚 Swagger UI 地址:"
echo " http://localhost:3000/swagger/index.html"
echo ""
echo "🔍 健康检查:"
echo " http://localhost:3000/health"
echo ""
echo "📝 查看日志:"
echo " tail -f /tmp/gateway.log"
echo ""
echo "🛑 停止服务:"
echo " kill $GATEWAY_PID"
echo ""
# 尝试自动打开浏览器(仅限 macOS
if [[ "$OSTYPE" == "darwin"* ]]; then
echo "正在自动打开浏览器..."
open http://localhost:3000/swagger/index.html
fi
echo "按 Ctrl+C 停止服务..."
wait $GATEWAY_PID