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

125 lines
3.6 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
# 快速更新 Swagger 文档
# 用途:在修改代码后重新生成 Swagger 文档
set -e
# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo -e "${GREEN}======================================${NC}"
echo -e "${GREEN} 快速更新 Swagger 文档${NC}"
echo -e "${GREEN}======================================${NC}"
echo ""
# 进入 backend 目录
cd "$(dirname "$0")"
BACKEND_DIR="$(pwd)"
# 进入 gateway 目录
cd gateway
# 检查 swag 是否已安装
if ! command -v swag &> /dev/null; then
echo -e "${RED}❌ swag 未安装!${NC}"
echo "请运行: bash ../install-swagger-deps.sh"
exit 1
fi
# 添加 GOPATH/bin 到 PATH
export PATH="$PATH:$(go env GOPATH)/bin"
echo -e "${YELLOW}📝 正在重新生成 Swagger 文档...${NC}"
# 设置 GOPROXY如果网络有问题
if [ -z "$GOPROXY" ]; then
export GOPROXY=https://goproxy.cn,direct
fi
# 重新生成文档
swag init --parseDependency --parseInternal \
--parseDepth 1 \
--output ./docs
echo -e "${GREEN}✅ Swagger 文档更新完成!${NC}"
echo ""
echo -e "${YELLOW}📋 生成的文件:${NC}"
echo " - backend/gateway/docs/docs.go"
echo " - backend/gateway/docs/swagger.json"
echo " - backend/gateway/docs/swagger.yaml"
echo ""
# 检查 Gateway 是否正在运行
GATEWAY_PID=""
if lsof -ti:8080 > /dev/null 2>&1; then
GATEWAY_PID=$(lsof -ti:8080)
echo -e "${YELLOW}🔍 检测到 Gateway 正在运行 (PID: $GATEWAY_PID)${NC}"
echo ""
# 询问是否重启
echo -e "${YELLOW}是否重启 Gateway 以加载新的 Swagger 文档?${NC}"
echo " 1) 是 (重启)"
echo " 2) 否 (不重启)"
echo -n "请选择 [1/2]: "
read -r choice
case $choice in
1)
echo ""
echo -e "${YELLOW}🔄 正在重启 Gateway...${NC}"
kill $GATEWAY_PID
sleep 2
# 重新构建并启动
cd "$BACKEND_DIR/gateway"
go build -o gateway . > "$BACKEND_DIR/logs/gateway-rebuild.log" 2>&1
nohup ./gateway > "$BACKEND_DIR/logs/gateway.log" 2>&1 &
NEW_PID=$!
sleep 3
# 检查是否启动成功
if curl -s http://localhost:8080/health > /dev/null 2>&1; then
echo -e "${GREEN}✅ Gateway 重启成功 (PID: $NEW_PID)${NC}"
echo ""
echo -e "${GREEN}📚 Swagger UI 地址:${NC}"
echo -e " http://localhost:8080/swagger/index.html"
else
echo -e "${RED}❌ Gateway 重启失败,请查看日志${NC}"
echo " 查看日志: tail -f logs/gateway.log"
fi
;;
2)
echo -e "${YELLOW}⚠️ Gateway 未重启Swagger 文档需要重启后才能生效${NC}"
echo ""
echo -e "${YELLOW}手动重启方法:${NC}"
echo " kill $GATEWAY_PID"
echo " cd gateway && go build -o gateway ."
echo " nohup ./gateway > ../logs/gateway.log 2>&1 &"
;;
*)
echo -e "${RED}❌ 无效选择${NC}"
;;
esac
else
echo -e "${YELLOW} Gateway 未运行${NC}"
echo ""
echo -e "${YELLOW}启动方法:${NC}"
echo " ./start-all.sh"
echo " 或"
echo " cd gateway && ./gateway"
fi
echo ""
echo -e "${YELLOW}🔍 查看接口数量:${NC}"
if [ -f "docs/swagger.json" ]; then
COUNT=$(cat docs/swagger.json | grep -o '"paths"' | wc -l | tr -d ' ')
echo -e " 生成的接口数量: $COUNT"
fi
echo ""