This commit is contained in:
chaosBreaking
2019-08-31 14:59:13 +08:00
commit dc5423d356
65 changed files with 3813 additions and 0 deletions

0
src/common/Auth.js Normal file
View File

64
src/common/Config.js Normal file
View File

@@ -0,0 +1,64 @@
const commander = require('commander');
const deepmerge = require('deepmerge');
const fs = require('fs');
class Config {
constructor() {
mylog.info('★★★★★★★★ [Config]初始化:依次载入系统配置、用户配置、命令行参数 ★★★★★★★★');
this.VERSION = '0.0.1';
this._config = {};
this.loadConfigFile();
this.loadCommander();
}
static getInstance() {
if (!Config.instance) {
Config.instance = new Config();
}
return Config.instance;
}
loadConfigFile() {
// 读取配置文件
try {
// CAUTION!!! fs.existsSync的base在文件根目录下
if (fs.existsSync('config.js')) {
this._config = require('../../config.js');
mylog.info('基本配置加载完成');
}
if (fs.existsSync(`configSec.js`)) { // 如果存在,覆盖掉 ConfigBasic 里的默认参数
this._config = deepmerge(this._config, require(`../../configSec.js`)); // 注意objectMerge后产生了一个新的对象而不是在原来的Config里添加
mylog.info('隐私配置加载完成');
}
} catch (err) {
throw new Error('配置加载出错: ' + err.message);
}
return this._config;
}
loadCommander() {
commander
.version(this.VERSION, '-v, --version')
.option('--dbType <type>', 'Database type: mysql|sqlite. ')
.option('--dbName <name>', 'Database name')
.option('-H, --host <host>', 'Host ip or domain name. ')
.option('-P, --protocol <protocol>', 'Server protocol: http|https|httpall. ')
.option('-p, --port <port>', 'Server port number.')
.option('--sslType <type>', `SSL provider type: file|greenlock`)
.option('--sslCert <cert>', 'SSL certificate file. ')
.option('--sslKey <key>', 'SSL private key file. ')
.option('--sslCA <ca>', 'SSL ca bundle file')
.parse(process.argv);
// 把命令行参数 合并入配置。
this._config.dbType = commander.dbType || this._config.dbType;
this._config.dbName = commander.dbName || this._config.dbName;
this._config.protocol = commander.protocol || this._config.protocol;
this._config.port = parseInt(commander.port) || parseInt(this._config.port) || (this._config.protocol === 'http' ? 80 : this._config.protocol === 'https' ? 443 : undefined); // 端口默认为http 80, https 443, 或80|443(httpall)
this._config.sslCert = commander.sslCert || this._config.sslCert;
this._config.sslKey = commander.sslKey || this._config.sslKey;
this._config.sslCA = commander.sslCA || this._config.sslCA;
}
get config() {
return this._config;
}
}
module.exports = Config.getInstance();

7
src/common/DepMap.js Normal file
View File

@@ -0,0 +1,7 @@
'use strict';
// 在此注册要放在服务容器Provider里的模块
module.exports = {
SysConfig: require('./Config').config,
System: require('../modules/system/system.controller').provider
};

56
src/common/Provider.js Normal file
View File

@@ -0,0 +1,56 @@
const { getFuncProps } = require('../utils');
const moduleMap = require('./DepMap');
class Provider {
constructor() {
this._cache = {};
this.init();
}
static getInstance() {
if (!Provider.instance) {
Provider.instance = new Provider();
}
return Provider.instance;
}
get cache() {
return this._cache;
}
init() {
this._cache = moduleMap;
}
regist(key, value) {
this._cache[key] = value;
}
loadDep (depName) {
const dep = moduleMap[depName];
if (dep) {
this._cache[depName] = require(dep);
}
}
inject(fn, scope = {}) {
const deps = getFuncProps(fn) || [];
for(let i = 0, len = deps.length; i < len; i++) {
const depName = deps[i];
const dep = this.cache[depName];
if(dep) {
deps[i] = dep;
} else {
throw new Error('Cannot find dependence');
}
}
return fn.apply(scope, deps);
}
getModule(depName) {
if (!this.cache[depName]) {
this.loadDep(depName);
}
return this.cache[depName] || {};
}
}
const provider = Provider.getInstance();
global.Provider = provider;
module.exports = {
inject: (...args) => provider.inject(...args),
regist: (...args) => provider.regist(...args),
};

31
src/common/dbConnect.js Normal file
View File

@@ -0,0 +1,31 @@
'use strict';
const { inject } = require('./Provider');
const mongoose = require('mongoose');
process.on('SIGTERM', () => {
mongoose.disconnect();
process.exit(0);
});
process.on('SIGINT', () => {
mongoose.disconnect();
process.exit(0);
});
module.exports = inject(async function connect2db(SysConfig) {
mongoose.set('useCreateIndex', true);
const { DB_USER_NAME, DB_PASSWD, DB_HOST, DB_PORT, DB_NAME } = SysConfig;
const db = mongoose.connection;
db.on('error', mylog.error);
db.once('open', function() {
mylog.info('数据库连接成功...');
});
mylog.info('开始连接数据库...');
await mongoose.connect(`mongodb://${DB_USER_NAME}:${DB_PASSWD}@${DB_HOST}:${DB_PORT}/${DB_NAME}`, {
useNewUrlParser: true,
bufferMaxEntries: 0,
autoReconnect: true,
useFindAndModify: false
});
return db;
});