|
|
|
|
@ -4,13 +4,44 @@ import path from 'path';
|
|
|
|
|
|
|
|
|
|
dotenv.config();
|
|
|
|
|
|
|
|
|
|
// 配置文件类型定义
|
|
|
|
|
interface ConfigFile {
|
|
|
|
|
server?: {
|
|
|
|
|
port?: number;
|
|
|
|
|
};
|
|
|
|
|
security?: {
|
|
|
|
|
jwtSecret?: string;
|
|
|
|
|
rateLimit?: {
|
|
|
|
|
windowMs?: number;
|
|
|
|
|
max?: number;
|
|
|
|
|
};
|
|
|
|
|
cors?: {
|
|
|
|
|
origin?: string;
|
|
|
|
|
methods?: string[];
|
|
|
|
|
allowedHeaders?: string[];
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
database?: {
|
|
|
|
|
postgreSQL?: {
|
|
|
|
|
host?: string;
|
|
|
|
|
port?: string;
|
|
|
|
|
username?: string;
|
|
|
|
|
password?: string;
|
|
|
|
|
database?: string;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
dataSource?: any;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 从文件读取配置
|
|
|
|
|
let fileConfig = {};
|
|
|
|
|
let fileConfig: ConfigFile = {};
|
|
|
|
|
try {
|
|
|
|
|
const configPath = path.join(__dirname, '../../../config.json');
|
|
|
|
|
if (fs.existsSync(configPath)) {
|
|
|
|
|
const configData = fs.readFileSync(configPath, 'utf8');
|
|
|
|
|
fileConfig = JSON.parse(configData);
|
|
|
|
|
console.log('已读取配置文件:', configPath);
|
|
|
|
|
console.log('配置文件内容:', fileConfig);
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('读取配置文件失败:', error);
|
|
|
|
|
@ -43,7 +74,8 @@ export const config = {
|
|
|
|
|
methods: fileConfig.security?.cors?.methods || ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
|
|
|
|
|
allowedHeaders: fileConfig.security?.cors?.allowedHeaders || ['Content-Type', 'Authorization']
|
|
|
|
|
},
|
|
|
|
|
dataSource: fileConfig.dataSource || {
|
|
|
|
|
dataSource: (() => {
|
|
|
|
|
const dataSourceConfig = fileConfig.dataSource || {
|
|
|
|
|
test: {
|
|
|
|
|
enabled: true,
|
|
|
|
|
timeout: 10000,
|
|
|
|
|
@ -76,5 +108,14 @@ export const config = {
|
|
|
|
|
refreshInterval: 60000
|
|
|
|
|
},
|
|
|
|
|
defaultDataSource: 'tqsdk'
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
console.log('数据源配置初始化完成:', {
|
|
|
|
|
hasFileConfig: !!fileConfig.dataSource,
|
|
|
|
|
enabledDataSources: Object.keys(dataSourceConfig).filter(key =>
|
|
|
|
|
key !== 'defaultDataSource' && dataSourceConfig[key].enabled
|
|
|
|
|
),
|
|
|
|
|
defaultDataSource: dataSourceConfig.defaultDataSource
|
|
|
|
|
});
|
|
|
|
|
return dataSourceConfig;
|
|
|
|
|
})()
|
|
|
|
|
};
|