准备重构nginx的配置,先回退api-config配置,添加PM2的多端口服务的启动脚步

This commit is contained in:
2025-07-25 15:06:47 +08:00
parent ccd5cdf71e
commit d465260daf
11 changed files with 238 additions and 309 deletions
+93 -225
View File
@@ -27,26 +27,54 @@ interface ApiConfig {
};
}
// 端口特定配置映射
// 根据不同端口提供不同的API配置
const portConfigs: Record<string, Partial<ApiConfig>> = {
'51704': {
baseUrl: 'http://172.16.0.55:51704',
documentUrl: 'http://172.16.0.55:51704/docauditai/',
uploadUrl: 'http://172.16.0.55:51704/admin/documents'
},
'51705': {
baseUrl: 'http://172.16.0.55:51705',
documentUrl: 'http://172.16.0.55:51705/docauditai/',
uploadUrl: 'http://172.16.0.55:51705/admin/documents'
},
'51706': {
baseUrl: 'http://172.16.0.55:51706',
documentUrl: 'http://172.16.0.55:51706/docauditai/',
uploadUrl: 'http://172.16.0.55:51706/admin/documents'
},
'51707': {
baseUrl: 'http://172.16.0.55:51707',
documentUrl: 'http://172.16.0.55:51707/docauditai/',
uploadUrl: 'http://172.16.0.55:51707/admin/documents'
},
'51708': {
baseUrl: 'http://172.16.0.55:51708',
documentUrl: 'http://172.16.0.55:51708/docauditai/',
uploadUrl: 'http://172.16.0.55:51708/admin/documents'
}
};
// 不同环境的默认配置
// 由于合同模板的上传,后续的的uploadUrl都不需要/upload,直接写/admin/documents,由程序自动添加/upload或/upload_contract_template
const configs: Record<string, ApiConfig> = {
// 开发环境
development: {
// baseUrl: '/api', // 改为相对路径,让nginx处理
baseUrl: 'http://172.16.0.55:8008',
// baseUrl: 'http://172.16.0.81:3000',
// baseUrl: 'http://nas.7bm.co:3000',
// documentUrl: 'http://172.16.0.81:9000/docauditai/',
documentUrl: 'http://172.16.0.55:8008/docauditai/',
// documentUrl: '/api/docauditai/',
// uploadUrl: '/api/admin/documents', // 改为相对路径
uploadUrl: 'http://172.16.0.55:8008/admin/documents',
// uploadUrl: 'http://172.16.0.58:8008/admin/documents',
// uploadUrl: 'http://172.16.0.58:8008/admin/documents',
oauth: {
serverUrl: 'http://10.79.112.85', // IDaaS服务器地址
clientId: '54d2a619fe5c81ae1250434c441fccccqMtKwh7H4fO',
clientSecret: 'VYk1AC5XIJEfnEXwyq0u9JEY3fi3byCfSD58zANGeb', // 需要替换为实际的Client Secret
redirectUri: 'http://10.79.97.17/callback', // 回调地址
redirectUri: 'http://10.79.97.17/', // 回调地址
appId: 'idaasoauth2' // 应用ID,用于登出
}
},
@@ -77,8 +105,7 @@ const configs: Record<string, ApiConfig> = {
serverUrl: 'http://10.79.112.85', // IDaaS服务器地址
clientId: '54d2a619fe5c81ae1250434c441fccccqMtKwh7H4fO',
clientSecret: 'VYk1AC5XIJEfnEXwyq0u9JEY3fi3byCfSD58zANGeb', // 需要替换为实际的Client Secret
redirectUri: 'http://10.79.97.17/callback', // 回调地址
redirectUri: 'http://10.79.97.17/', // 回调地址
appId: 'idaasoauth2' // 应用ID,用于登出
}
},
@@ -98,165 +125,12 @@ const configs: Record<string, ApiConfig> = {
}
};
// 客户端特定配置 - 支持多客户端部署
// 根据环境自动选择配置
const getClientConfigs = (env: string): Record<string, Partial<ApiConfig>> => {
if (env === 'development') {
// 开发环境 - 本地nginx代理配置
return {
'client-a': {
baseUrl: '/api', // 改为相对路径,让nginx处理
uploadUrl: '/api/admin/documents', // 改为相对路径
documentUrl: '/api/docauditai/',
oauth: {
serverUrl: 'http://10.79.112.85',
clientId: '54d2a619fe5c81ae1250434c441fccccqMtKwh7H4fO',
clientSecret: 'VYk1AC5XIJEfnEXwyq0u9JEY3fi3byCfSD58zANGeb',
redirectUri: 'http://172.16.0.34:5174/callback',
appId: 'idaasoauth2'
}
},
'client-b': {
baseUrl: '/api', // 改为相对路径,让nginx处理
uploadUrl: '/api/admin/documents', // 改为相对路径
documentUrl: '/api/docauditai/',
oauth: {
serverUrl: 'http://10.79.112.85',
clientId: '54d2a619fe5c81ae1250434c441fccccqMtKwh7H4fO',
clientSecret: 'VYk1AC5XIJEfnEXwyq0u9JEY3fi3byCfSD58zANGeb',
redirectUri: 'http://172.16.0.34:5175/callback',
appId: 'idaasoauth2'
}
},
'client-c': {
baseUrl: '/api', // 改为相对路径,让nginx处理
uploadUrl: '/api/admin/documents', // 改为相对路径
documentUrl: '/api/docauditai/',
oauth: {
serverUrl: 'http://10.79.112.85',
clientId: '54d2a619fe5c81ae1250434c441fccccqMtKwh7H4fO',
clientSecret: 'VYk1AC5XIJEfnEXwyq0u9JEY3fi3byCfSD58zANGeb',
redirectUri: 'http://172.16.0.34:5176/callback',
appId: 'idaasoauth2'
}
},
'client-d': {
baseUrl: '/api', // 改为相对路径,让nginx处理
uploadUrl: '/api/admin/documents', // 改为相对路径
documentUrl: '/api/docauditai/',
oauth: {
serverUrl: 'http://10.79.112.85',
clientId: '54d2a619fe5c81ae1250434c441fccccqMtKwh7H4fO',
clientSecret: 'VYk1AC5XIJEfnEXwyq0u9JEY3fi3byCfSD58zANGeb',
redirectUri: 'http://172.16.0.34:5177/callback',
appId: 'idaasoauth2'
}
}
};
} else {
// 生产环境 - 服务器配置
return {
'provincial': {
baseUrl: 'http://10.79.97.17:51704',
uploadUrl: 'http://10.79.97.17:51704/admin/documents',
documentUrl: 'http://10.76.244.156:9000/docauditai/',
oauth: {
serverUrl: 'http://10.79.112.85',
clientId: '54d2a619fe5c81ae1250434c441fccccqMtKwh7H4fO',
clientSecret: 'VYk1AC5XIJEfnEXwyq0u9JEY3fi3byCfSD58zANGeb',
redirectUri: 'http://10.79.97.17/callback',
appId: 'idaasoauth2'
}
},
'meizhou': {
baseUrl: 'http://10.79.97.17:51705',
uploadUrl: 'http://10.79.97.17:51705/admin/documents',
documentUrl: 'http://10.76.244.156:9000/docauditai/',
oauth: {
serverUrl: 'http://10.79.112.85',
clientId: '54d2a619fe5c81ae1250434c441fccccqMtKwh7H4fO',
clientSecret: 'VYk1AC5XIJEfnEXwyq0u9JEY3fi3byCfSD58zANGeb',
redirectUri: 'http://10.79.97.17/callback',
appId: 'idaasoauth2'
}
},
'jieyang': {
baseUrl: 'http://10.79.97.17:51706',
uploadUrl: 'http://10.79.97.17:51706/admin/documents',
documentUrl: 'http://10.76.244.156:9000/docauditai/',
oauth: {
serverUrl: 'http://10.79.112.85',
clientId: '54d2a619fe5c81ae1250434c441fccccqMtKwh7H4fO',
clientSecret: 'VYk1AC5XIJEfnEXwyq0u9JEY3fi3byCfSD58zANGeb',
redirectUri: 'http://10.79.97.17/callback',
appId: 'idaasoauth2'
}
},
'yunfu': {
baseUrl: 'http://10.79.97.17:51707',
uploadUrl: 'http://10.79.97.17:51707/admin/documents',
documentUrl: 'http://10.76.244.156:9000/docauditai/',
oauth: {
serverUrl: 'http://10.79.112.85',
clientId: '54d2a619fe5c81ae1250434c441fccccqMtKwh7H4fO',
clientSecret: 'VYk1AC5XIJEfnEXwyq0u9JEY3fi3byCfSD58zANGeb',
redirectUri: 'http://10.79.97.17/callback',
appId: 'idaasoauth2'
}
}
};
}
};
// 获取当前环境,默认为development
const getCurrentEnvironment = (): string => {
// 优先使用环境变量,然后使用 NODE_ENV
return process.env.NEXT_PUBLIC_API_ENV || process.env.NODE_ENV || 'development';
};
// 获取客户端ID - 支持从请求头动态获取
const getClientId = (request?: Request): string => {
// SSR: 通过请求头的 host 判断
if (request && typeof window === 'undefined') {
// 1. 优先 X-Client-ID
const clientIdFromHeader = request.headers.get('X-Client-ID');
if (clientIdFromHeader) return clientIdFromHeader;
// 2. 通过 host 端口判断
const host = request.headers.get('host'); // 例如 172.24.238.60:5177
if (host) {
const port = host.split(':')[1];
const portToClient: Record<string, string> = {
'5174': 'client-a',
'5175': 'client-b',
'5176': 'client-c',
'5177': 'client-d'
};
if (port && portToClient[port]) {
return portToClient[port];
}
}
}
// 浏览器端
if (typeof window !== 'undefined') {
const port = window.location.port;
const portToClient: Record<string, string> = {
'5174': 'client-a',
'5175': 'client-b',
'5176': 'client-c',
'5177': 'client-d'
};
if (port && portToClient[port]) {
console.log(`🎯 浏览器端检测到客户端ID: ${portToClient[port]} (端口: ${port})`);
return portToClient[port];
}
}
// 回退到环境变量
return process.env.CLIENT_ID || process.env.NEXT_PUBLIC_CLIENT_ID || 'main';
};
// 从环境变量获取配置,如果环境变量不存在则使用默认配置
const getConfigFromEnv = (defaultConfig: ApiConfig): ApiConfig => {
return {
@@ -273,83 +147,63 @@ const getConfigFromEnv = (defaultConfig: ApiConfig): ApiConfig => {
};
};
// 获取当前配置 - 支持客户端特定配置
const getCurrentConfig = (request?: Request): ApiConfig => {
/**
* 获取当前端口号
* 优先从环境变量获取,然后从浏览器location获取
*/
const getCurrentPort = (): string => {
// 优先使用环境变量中的端口配置
if (process.env.API_PORT_CONFIG) {
return process.env.API_PORT_CONFIG;
}
// 如果是浏览器环境,从location获取端口
if (typeof window !== 'undefined' && window.location.port) {
return window.location.port;
}
return '';
};
/**
* 获取当前配置
* 支持根据端口动态切换API配置
*/
const getCurrentConfig = (): ApiConfig => {
const env = getCurrentEnvironment();
const clientId = getClientId(request);
const defaultConfig = configs[env] || configs.development;
const port = getCurrentPort();
// 获取当前环境的客户端特定配置
const clientConfigs = getClientConfigs(env);
const clientConfig = clientConfigs[clientId];
// 获取基础配置
let defaultConfig = configs[env] || configs.development;
// 合并默认配置和客户端特定配置
let finalConfig = defaultConfig;
if (clientConfig) {
finalConfig = {
...defaultConfig,
...clientConfig,
oauth: {
...defaultConfig.oauth,
...clientConfig.oauth
}
// 如果有端口特定配置,则合并配置
if (port && portConfigs[port]) {
defaultConfig = {
...defaultConfig,
...portConfigs[port],
// 保持oauth配置不变,只覆盖API相关配置
oauth: defaultConfig.oauth
};
}
// 如果是浏览器环境,尝试从环境变量覆盖配置
if (typeof window !== 'undefined' || process.env.NEXT_PUBLIC_API_BASE_URL) {
return getConfigFromEnv(finalConfig);
return getConfigFromEnv(defaultConfig);
}
return finalConfig;
return defaultConfig;
};
// 导出当前环境的配置(静态,用于兼容性)
// 导出当前环境的配置
export const apiConfig = getCurrentConfig();
// 导出动态配置获取函数(支持从请求头获取客户端ID)
export const getApiConfig = (request?: Request): ApiConfig => {
return getCurrentConfig(request);
};
// 导出具体的配置项,方便使用(现在是真正动态的)
// 使用getter函数实现动态获取,避免ES模块中exports未定义的问题
export const API_BASE_URL = {
get value() {
return getCurrentConfig().baseUrl;
}
};
export const DOCUMENT_URL = {
get value() {
return getCurrentConfig().documentUrl;
}
};
export const UPLOAD_URL = {
get value() {
return getCurrentConfig().uploadUrl;
}
};
export const OAUTH_CONFIG = {
get value() {
return getCurrentConfig().oauth;
}
};
// 动态获取配置项的函数
export const getApiBaseUrl = (request?: Request): string => {
return getApiConfig(request).baseUrl;
};
export const getUploadUrl = (request?: Request): string => {
return getApiConfig(request).uploadUrl;
};
export const getOAuthConfig = (request?: Request) => {
return getApiConfig(request).oauth;
};
// 导出具体的配置项,方便使用
export const {
baseUrl: API_BASE_URL,
documentUrl: DOCUMENT_URL,
uploadUrl: UPLOAD_URL,
oauth: OAUTH_CONFIG
} = apiConfig;
// 导出所有配置,供调试使用
export { configs };
@@ -359,6 +213,20 @@ export const setEnvironment = (env: string): ApiConfig => {
return configs[env] || configs.development;
};
/**
* 工具函数:获取当前端口配置信息(用于调试)
*/
export const getCurrentPortConfig = () => {
const port = getCurrentPort();
const env = getCurrentEnvironment();
return {
currentPort: port,
currentEnvironment: env,
hasPortConfig: !!(port && portConfigs[port]),
portConfig: port ? portConfigs[port] : null
};
};
// 调试信息(仅在开发环境显示)
// if (process.env.NODE_ENV === 'development') {
// console.log('📦 API配置信息:', {