index.js•3.33 kB
import dotenv from 'dotenv';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import { existsSync } from 'fs';
// 获取当前文件目录
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const rootDir = join(__dirname, '../..');
// 加载环境变量
const envPath = join(rootDir, '.env');
if (existsSync(envPath)) {
dotenv.config({ path: envPath });
} else {
console.warn('警告: .env 文件不存在,请参考 .env.example 创建配置文件');
}
/**
* 配置管理类
*/
export class Config {
constructor() {
this.gitlab = {
url: process.env.GITLAB_URL || 'https://gitlab.com',
token: process.env.GITLAB_TOKEN,
username: process.env.GITLAB_USERNAME,
};
this.wechat = {
webhookUrl: process.env.WECHAT_WEBHOOK_URL,
corpId: process.env.WECHAT_CORP_ID,
corpSecret: process.env.WECHAT_CORP_SECRET,
agentId: process.env.WECHAT_AGENT_ID,
};
this.mcp = {
serverName: process.env.MCP_SERVER_NAME || 'gitlab-wechat-mcp',
serverVersion: process.env.MCP_SERVER_VERSION || '1.0.0',
};
// 验证配置
this.validate();
}
/**
* 验证必要的配置项
*/
validate() {
const errors = [];
// 验证GitLab配置
if (!this.gitlab.token) {
errors.push('GITLAB_TOKEN 是必需的');
}
if (!this.gitlab.username) {
errors.push('GITLAB_USERNAME 是必需的');
}
// 验证企业微信配置(至少需要一种方式)
const hasWebhook = !!this.wechat.webhookUrl;
const hasApiConfig = !!(this.wechat.corpId && this.wechat.corpSecret && this.wechat.agentId);
if (!hasWebhook && !hasApiConfig) {
errors.push('企业微信配置不完整:需要设置 WECHAT_WEBHOOK_URL 或完整的API配置(WECHAT_CORP_ID, WECHAT_CORP_SECRET, WECHAT_AGENT_ID)');
}
if (errors.length > 0) {
console.error('配置验证失败:');
errors.forEach(error => console.error(` - ${error}`));
console.error('\n请检查 .env 文件或环境变量配置');
process.exit(1);
}
}
/**
* 获取GitLab配置
*/
getGitLabConfig() {
return { ...this.gitlab };
}
/**
* 获取企业微信配置
*/
getWeChatConfig() {
return { ...this.wechat };
}
/**
* 获取MCP配置
*/
getMCPConfig() {
return { ...this.mcp };
}
/**
* 打印配置信息(隐藏敏感信息)
*/
printConfig() {
console.log('当前配置:');
console.log('GitLab:');
console.log(` URL: ${this.gitlab.url}`);
console.log(` Username: ${this.gitlab.username}`);
console.log(` Token: ${this.gitlab.token ? '已设置' : '未设置'}`);
console.log('企业微信:');
console.log(` Webhook URL: ${this.wechat.webhookUrl ? '已设置' : '未设置'}`);
console.log(` Corp ID: ${this.wechat.corpId ? '已设置' : '未设置'}`);
console.log(` Corp Secret: ${this.wechat.corpSecret ? '已设置' : '未设置'}`);
console.log(` Agent ID: ${this.wechat.agentId ? '已设置' : '未设置'}`);
console.log('MCP:');
console.log(` Server Name: ${this.mcp.serverName}`);
console.log(` Server Version: ${this.mcp.serverVersion}`);
}
}
// 创建全局配置实例
export const config = new Config();