Browse Source

custom config file support

master
filesite 6 months ago
parent
commit
c58311eaf8
  1. 6
      heroUnion.mjs
  2. 13
      router_api.mjs

6
heroUnion.mjs

@ -31,8 +31,9 @@ import md5 from 'md5'; @@ -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 { @@ -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) {

13
router_api.mjs

@ -7,7 +7,18 @@ import common from './common.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();

Loading…
Cancel
Save