121 lines
2.9 KiB
JavaScript
121 lines
2.9 KiB
JavaScript
const utils = require('./utils');
|
|
const log = require('./log');
|
|
|
|
const metaClasses = [];
|
|
|
|
const MT_WEB3SERVER = 0;
|
|
const MT_CONTRACT = 1;
|
|
const MT_MYSQL = 2;
|
|
const MT_WEB3 = 3;
|
|
|
|
function registerMetaClass(fileName, idx, primKey) {
|
|
metaClasses.push({
|
|
'fileName' : fileName,
|
|
'idx' : idx,
|
|
'primKey' : primKey,
|
|
'rawList' : [],
|
|
'rawHash' : {},
|
|
});
|
|
}
|
|
|
|
function load() {
|
|
metaClasses.forEach(function (metaClass) {
|
|
const json = utils.readJsonFromFile(metaClass['fileName']);
|
|
if (!json) {
|
|
throw new Error('读取配置' + metaClass['fileName'] + '失败');
|
|
}
|
|
if (Array.isArray(json)) {
|
|
metaClass['rawList'] = json;
|
|
} else {
|
|
metaClass['rawList'].push(json);
|
|
}
|
|
let idx = 0;
|
|
metaClass['rawList'].forEach(function (item) {
|
|
if (metaClass['primKey'] == '') {
|
|
metaClass['rawHash'][idx] = item;
|
|
} else {
|
|
metaClass['rawHash'][item[metaClass['primKey']]] = item;
|
|
}
|
|
++idx;
|
|
});
|
|
//log.debug(utils.jsonEncode(metaClass));
|
|
});
|
|
}
|
|
|
|
function init() {
|
|
let configDir = '';
|
|
if (utils.isOnlineEnv()) {
|
|
configDir = '../config/';
|
|
}
|
|
registerMetaClass(configDir + 'web3server.json',
|
|
MT_WEB3SERVER,
|
|
''
|
|
);
|
|
registerMetaClass(configDir + 'contract.json',
|
|
MT_CONTRACT,
|
|
'name',
|
|
);
|
|
registerMetaClass(configDir + 'mysql.json',
|
|
MT_MYSQL,
|
|
'',
|
|
);
|
|
registerMetaClass(configDir + 'web3.json',
|
|
MT_WEB3,
|
|
''
|
|
);
|
|
load();
|
|
}
|
|
|
|
function getMetaClass(metaIdx) {
|
|
return metaIdx >= 0 && metaIdx < metaClasses.length ? metaClasses[metaIdx] : null;
|
|
}
|
|
|
|
function getMetaByKey(metaIdx, key) {
|
|
const metaClass = getMetaClass(metaIdx);
|
|
return metaClass && key in metaClass['rawHash'] ? metaClass['rawHash'][key] : null;
|
|
}
|
|
|
|
function getMetaList(metaIdx) {
|
|
const metaClass = getMetaClass(metaIdx);
|
|
return metaClass ? metaClass['rawList'] : null;
|
|
}
|
|
|
|
function getServerConf() {
|
|
return getMetaByKey(MT_WEB3SERVER, '0');
|
|
}
|
|
|
|
function getWeb3Conf() {
|
|
return getMetaByKey(MT_WEB3, '0');
|
|
}
|
|
|
|
function getMysqlConf() {
|
|
return getMetaByKey(MT_MYSQL, '0');
|
|
}
|
|
|
|
function getContracts() {
|
|
return getMetaList(MT_CONTRACT);
|
|
}
|
|
|
|
function getContractByName(name) {
|
|
return getMetaByKey(MT_CONTRACT, name);
|
|
}
|
|
|
|
function getUserAddress() {
|
|
return getWeb3Conf()['user_address'];
|
|
}
|
|
|
|
function getPrivateKey() {
|
|
return getWeb3Conf()['private_key'];
|
|
}
|
|
|
|
exports.init = init;
|
|
exports.getMetaByKey = getMetaByKey;
|
|
exports.getMetaList = getMetaList;
|
|
exports.getServerConf = getServerConf;
|
|
exports.getWeb3Conf = getWeb3Conf;
|
|
exports.getMysqlConf = getMysqlConf;
|
|
exports.getContracts = getContracts;
|
|
exports.getContractByName = getContractByName;
|
|
exports.getUserAddress = getUserAddress;
|
|
exports.getPrivateKey = getPrivateKey;
|