iconsulting/scripts/init-kong.sh

114 lines
3.3 KiB
Bash

#!/bin/bash
#===============================================================================
# Kong API Gateway 初始化脚本
# 配置所有微服务的路由
#===============================================================================
KONG_ADMIN_URL="${KONG_ADMIN_URL:-http://localhost:8001}"
echo "Waiting for Kong to be ready..."
until curl -s "$KONG_ADMIN_URL/status" > /dev/null 2>&1; do
echo "Kong is not ready yet, waiting..."
sleep 2
done
echo "Kong is ready!"
# 函数: 创建服务和路由
create_service_and_route() {
local service_name=$1
local service_url=$2
local route_name=$3
shift 3
local paths=("$@")
echo "Creating service: $service_name -> $service_url"
# 检查服务是否已存在
if curl -s "$KONG_ADMIN_URL/services/$service_name" | grep -q '"id"'; then
echo " Service $service_name already exists, updating..."
curl -s -X PATCH "$KONG_ADMIN_URL/services/$service_name" \
-d "url=$service_url" > /dev/null
else
curl -s -X POST "$KONG_ADMIN_URL/services" \
-d "name=$service_name" \
-d "url=$service_url" > /dev/null
fi
echo "Creating route: $route_name"
# 构建路径参数
local path_args=""
for path in "${paths[@]}"; do
path_args="$path_args -d paths[]=$path"
done
# 检查路由是否已存在
if curl -s "$KONG_ADMIN_URL/routes/$route_name" | grep -q '"id"'; then
echo " Route $route_name already exists, updating..."
curl -s -X PATCH "$KONG_ADMIN_URL/routes/$route_name" \
$path_args \
-d "strip_path=false" > /dev/null
else
curl -s -X POST "$KONG_ADMIN_URL/services/$service_name/routes" \
-d "name=$route_name" \
$path_args \
-d "strip_path=false" > /dev/null
fi
echo " Done!"
}
#===============================================================================
# 配置各服务路由
#===============================================================================
# User Service - 用户认证和管理
create_service_and_route \
"user-service" \
"http://user-service:3001" \
"user-routes" \
"/api/v1/users" "/api/v1/auth"
# Payment Service - 支付处理
create_service_and_route \
"payment-service" \
"http://payment-service:3002" \
"payment-routes" \
"/api/v1/payments" "/api/v1/subscriptions"
# Knowledge Service - 知识库
create_service_and_route \
"knowledge-service" \
"http://knowledge-service:3003" \
"knowledge-routes" \
"/api/v1/knowledge"
# Conversation Service - 对话服务
create_service_and_route \
"conversation-service" \
"http://conversation-service:3004" \
"conversation-routes" \
"/api/v1/conversations" "/api/v1/messages"
# Evolution Service - 进化服务
create_service_and_route \
"evolution-service" \
"http://evolution-service:3005" \
"evolution-routes" \
"/api/v1/evolution"
#===============================================================================
# 显示配置结果
#===============================================================================
echo ""
echo "=========================================="
echo "Kong configuration completed!"
echo "=========================================="
echo ""
echo "Services:"
curl -s "$KONG_ADMIN_URL/services" | jq -r '.data[] | " - \(.name): \(.host):\(.port)"'
echo ""
echo "Routes:"
curl -s "$KONG_ADMIN_URL/routes" | jq -r '.data[] | " - \(.name): \(.paths | join(", "))"'