diff --git a/heroUnion.mjs b/heroUnion.mjs index 6cc8ae7..4e943bd 100644 --- a/heroUnion.mjs +++ b/heroUnion.mjs @@ -31,8 +31,9 @@ import md5 from 'md5'; class HeroUnion { //构造函数,设置默认配置 - constructor() { + constructor(configFilename) { this.config = null; + this.configFile = typeof(configFilename) != 'undefined' && configFilename ? configFilename : 'config.json'; //默认配置 this.systemLogDir = 'log/'; //系统日志保存目录 @@ -100,7 +101,8 @@ class HeroUnion { const _self = this; if ( !this.config || (typeof(forceReload) != 'undefined' && forceReload) ) { - let config = await common.getConfigFromJsonFile('config.json'); + common.log("Load config from %s", this.configFile); + let config = await common.getConfigFromJsonFile(this.configFile); //覆盖默认配置 for (const key in config) { diff --git a/router_api.mjs b/router_api.mjs index cceab20..fedba4c 100644 --- a/router_api.mjs +++ b/router_api.mjs @@ -7,7 +7,18 @@ import common from './common.mjs'; import HeroUnion from './heroUnion.mjs'; //初始化爬虫联盟 -const heroUnion = new HeroUnion(); +let configFile = 'config.json'; + +//命令行参数支持,格式:npm start -- my_config.json +if (process.argv.length >= 3) { + configFile = process.argv[2]; +} + +//环境变量支持,格式:CONFIGFILE=my_config.json pm2 start server.mjs +if (typeof(process.env.CONFIGFILE) != 'undefined') { + configFile = process.env.CONFIGFILE; +} +const heroUnion = new HeroUnion(configFile); heroUnion.init();