fix(admin-web): fix trading-service proxy routing for Kong gateway
- Add production/development environment detection - Production: route through Kong gateway at /api/v2/trading/* - Development: direct connection to trading-service at localhost:3022 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
8018fa5110
commit
2da02e0823
|
|
@ -3,25 +3,47 @@ const nextConfig = {
|
|||
reactStrictMode: true,
|
||||
output: 'standalone',
|
||||
async rewrites() {
|
||||
// NEXT_PUBLIC_API_URL 应该是后端服务的基础 URL,如 http://mining-admin-service:3023
|
||||
// 前端请求 /api/xxx 会被转发到 {API_URL}/api/v2/xxx
|
||||
const apiBaseUrl = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3023';
|
||||
const tradingServiceUrl = process.env.TRADING_SERVICE_URL || 'http://localhost:3020';
|
||||
// 移除末尾可能存在的 /api/v2 避免重复
|
||||
const cleanUrl = apiBaseUrl.replace(/\/api\/v2\/?$/, '');
|
||||
const cleanTradingUrl = tradingServiceUrl.replace(/\/api\/v2\/?$/, '');
|
||||
return [
|
||||
// trading-service 路由
|
||||
{
|
||||
source: '/api/trading/:path*',
|
||||
destination: `${cleanTradingUrl}/api/v2/:path*`,
|
||||
},
|
||||
// mining-admin-service 路由 (默认)
|
||||
{
|
||||
source: '/api/:path*',
|
||||
destination: `${cleanUrl}/api/v2/:path*`,
|
||||
},
|
||||
];
|
||||
// 获取 API 基础 URL(Kong 网关地址)
|
||||
// 生产环境: https://rwaapi.szaiai.com
|
||||
// 开发环境: http://localhost:3023 (mining-admin-service)
|
||||
const apiGatewayUrl = process.env.API_GATEWAY_URL || 'https://rwaapi.szaiai.com';
|
||||
const miningAdminUrl = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3023';
|
||||
const tradingServiceUrl = process.env.TRADING_SERVICE_URL || 'http://localhost:3022';
|
||||
|
||||
// 检查是否是生产环境(使用 Kong 网关)
|
||||
const isProduction = process.env.NODE_ENV === 'production';
|
||||
|
||||
// 移除末尾可能存在的路径避免重复
|
||||
const cleanMiningUrl = miningAdminUrl.replace(/\/api\/v2.*$/, '');
|
||||
const cleanTradingUrl = tradingServiceUrl.replace(/\/api\/v2.*$/, '');
|
||||
|
||||
if (isProduction) {
|
||||
// 生产环境:通过 Kong 网关
|
||||
// /api/trading/* -> Kong -> trading-service
|
||||
// /api/* -> Kong -> mining-admin-service
|
||||
return [
|
||||
{
|
||||
source: '/api/trading/:path*',
|
||||
destination: `${apiGatewayUrl}/api/v2/trading/:path*`,
|
||||
},
|
||||
{
|
||||
source: '/api/:path*',
|
||||
destination: `${apiGatewayUrl}/api/v2/mining-admin/:path*`,
|
||||
},
|
||||
];
|
||||
} else {
|
||||
// 开发环境:直连各服务
|
||||
return [
|
||||
{
|
||||
source: '/api/trading/:path*',
|
||||
destination: `${cleanTradingUrl}/api/v2/:path*`,
|
||||
},
|
||||
{
|
||||
source: '/api/:path*',
|
||||
destination: `${cleanMiningUrl}/api/v2/:path*`,
|
||||
},
|
||||
];
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue