168 lines
5.8 KiB
Bash
168 lines
5.8 KiB
Bash
#!/bin/sh
|
|
#===============================================================================
|
|
# Kong API Gateway 初始化脚本
|
|
#
|
|
# 配置所有微服务的路由和全局插件
|
|
# 此脚本可在容器内或主机上运行
|
|
#
|
|
# 用法:
|
|
# 在 Docker 容器内: KONG_ADMIN_URL=http://kong:8001 ./init-kong.sh
|
|
# 在主机上: KONG_ADMIN_URL=http://localhost:8001 ./init-kong.sh
|
|
#===============================================================================
|
|
|
|
set -e
|
|
|
|
KONG_ADMIN_URL="${KONG_ADMIN_URL:-http://kong:8001}"
|
|
|
|
echo "=============================================="
|
|
echo "Kong API Gateway Configuration"
|
|
echo "Admin URL: $KONG_ADMIN_URL"
|
|
echo "=============================================="
|
|
|
|
# 等待 Kong 就绪
|
|
echo ""
|
|
echo "Waiting for Kong to be ready..."
|
|
max_retries=30
|
|
retry_count=0
|
|
until curl -s "$KONG_ADMIN_URL/status" > /dev/null 2>&1; do
|
|
retry_count=$((retry_count + 1))
|
|
if [ $retry_count -ge $max_retries ]; then
|
|
echo "ERROR: Kong is not ready after $max_retries attempts"
|
|
exit 1
|
|
fi
|
|
echo " Attempt $retry_count/$max_retries - Kong is not ready yet..."
|
|
sleep 2
|
|
done
|
|
echo "Kong is ready!"
|
|
|
|
#===============================================================================
|
|
# 创建服务和路由
|
|
#===============================================================================
|
|
|
|
echo ""
|
|
echo "Configuring services and routes..."
|
|
|
|
# User Service - 用户认证和管理
|
|
echo " [1/5] user-service..."
|
|
curl -s -X PUT "$KONG_ADMIN_URL/services/user-service" \
|
|
-d url=http://user-service:3001 \
|
|
-d connect_timeout=60000 \
|
|
-d write_timeout=60000 \
|
|
-d read_timeout=60000 > /dev/null
|
|
curl -s -X PUT "$KONG_ADMIN_URL/services/user-service/routes/user-routes" \
|
|
-d "paths[]=/api/v1/users" \
|
|
-d "paths[]=/api/v1/auth" \
|
|
-d strip_path=false \
|
|
-d preserve_host=true > /dev/null
|
|
|
|
# Payment Service - 支付处理
|
|
echo " [2/5] payment-service..."
|
|
curl -s -X PUT "$KONG_ADMIN_URL/services/payment-service" \
|
|
-d url=http://payment-service:3002 \
|
|
-d connect_timeout=60000 \
|
|
-d write_timeout=60000 \
|
|
-d read_timeout=60000 > /dev/null
|
|
curl -s -X PUT "$KONG_ADMIN_URL/services/payment-service/routes/payment-routes" \
|
|
-d "paths[]=/api/v1/payments" \
|
|
-d "paths[]=/api/v1/subscriptions" \
|
|
-d strip_path=false \
|
|
-d preserve_host=true > /dev/null
|
|
|
|
# Knowledge Service - 知识库
|
|
echo " [3/5] knowledge-service..."
|
|
curl -s -X PUT "$KONG_ADMIN_URL/services/knowledge-service" \
|
|
-d url=http://knowledge-service:3003 \
|
|
-d connect_timeout=60000 \
|
|
-d write_timeout=60000 \
|
|
-d read_timeout=60000 > /dev/null
|
|
curl -s -X PUT "$KONG_ADMIN_URL/services/knowledge-service/routes/knowledge-routes" \
|
|
-d "paths[]=/api/v1/knowledge" \
|
|
-d strip_path=false \
|
|
-d preserve_host=true > /dev/null
|
|
|
|
# Conversation Service - 对话服务 (需要更长的超时)
|
|
echo " [4/5] conversation-service..."
|
|
curl -s -X PUT "$KONG_ADMIN_URL/services/conversation-service" \
|
|
-d url=http://conversation-service:3004 \
|
|
-d connect_timeout=60000 \
|
|
-d write_timeout=120000 \
|
|
-d read_timeout=120000 > /dev/null
|
|
curl -s -X PUT "$KONG_ADMIN_URL/services/conversation-service/routes/conversation-routes" \
|
|
-d "paths[]=/api/v1/conversations" \
|
|
-d "paths[]=/api/v1/messages" \
|
|
-d strip_path=false \
|
|
-d preserve_host=true > /dev/null
|
|
|
|
# Evolution Service - 进化服务
|
|
echo " [5/5] evolution-service..."
|
|
curl -s -X PUT "$KONG_ADMIN_URL/services/evolution-service" \
|
|
-d url=http://evolution-service:3005 \
|
|
-d connect_timeout=60000 \
|
|
-d write_timeout=60000 \
|
|
-d read_timeout=60000 > /dev/null
|
|
curl -s -X PUT "$KONG_ADMIN_URL/services/evolution-service/routes/evolution-routes" \
|
|
-d "paths[]=/api/v1/evolution" \
|
|
-d strip_path=false \
|
|
-d preserve_host=true > /dev/null
|
|
|
|
#===============================================================================
|
|
# 配置全局插件
|
|
#===============================================================================
|
|
|
|
echo ""
|
|
echo "Configuring global plugins..."
|
|
|
|
# 检查 CORS 插件是否已存在
|
|
cors_exists=$(curl -s "$KONG_ADMIN_URL/plugins" | grep -c '"name":"cors"' || true)
|
|
if [ "$cors_exists" = "0" ]; then
|
|
echo " Adding CORS plugin..."
|
|
curl -s -X POST "$KONG_ADMIN_URL/plugins" \
|
|
-d name=cors \
|
|
-d "config.origins[]=https://iconsulting.szaiai.com" \
|
|
-d "config.origins[]=http://localhost:5173" \
|
|
-d "config.origins[]=http://localhost:3000" \
|
|
-d "config.methods[]=GET" \
|
|
-d "config.methods[]=POST" \
|
|
-d "config.methods[]=PUT" \
|
|
-d "config.methods[]=PATCH" \
|
|
-d "config.methods[]=DELETE" \
|
|
-d "config.methods[]=OPTIONS" \
|
|
-d "config.headers[]=Accept" \
|
|
-d "config.headers[]=Accept-Version" \
|
|
-d "config.headers[]=Content-Type" \
|
|
-d "config.headers[]=Content-Length" \
|
|
-d "config.headers[]=Authorization" \
|
|
-d "config.headers[]=X-User-Id" \
|
|
-d "config.headers[]=X-Request-Id" \
|
|
-d "config.exposed_headers[]=X-Request-Id" \
|
|
-d config.credentials=true \
|
|
-d config.max_age=3600 > /dev/null
|
|
else
|
|
echo " CORS plugin already exists, skipping..."
|
|
fi
|
|
|
|
#===============================================================================
|
|
# 显示配置结果
|
|
#===============================================================================
|
|
|
|
echo ""
|
|
echo "=============================================="
|
|
echo "Kong configuration completed!"
|
|
echo "=============================================="
|
|
echo ""
|
|
echo "Services:"
|
|
curl -s "$KONG_ADMIN_URL/services" 2>/dev/null | \
|
|
grep -o '"name":"[^"]*"' | sed 's/"name":"//g; s/"//g' | \
|
|
while read name; do echo " - $name"; done
|
|
echo ""
|
|
echo "Routes:"
|
|
curl -s "$KONG_ADMIN_URL/routes" 2>/dev/null | \
|
|
grep -o '"name":"[^"]*"' | sed 's/"name":"//g; s/"//g' | \
|
|
while read name; do echo " - $name"; done
|
|
echo ""
|
|
echo "Plugins:"
|
|
curl -s "$KONG_ADMIN_URL/plugins" 2>/dev/null | \
|
|
grep -o '"name":"[^"]*"' | sed 's/"name":"//g; s/"//g' | \
|
|
while read name; do echo " - $name"; done
|
|
echo ""
|