111 lines
2.3 KiB
JavaScript
111 lines
2.3 KiB
JavaScript
const utils = require('./utils');
|
|
var metaClasses = [];
|
|
|
|
const MT_WEB3SERVEWR = 0;
|
|
const MT_CONTRACT = 1;
|
|
const MT_MYSQL = 2;
|
|
|
|
function registerMetaClass(fileName, idx, primKey)
|
|
{
|
|
metaClasses.push({
|
|
'fileName' : fileName,
|
|
'idx' : idx,
|
|
'primKey' : primKey,
|
|
'rawList' : [],
|
|
'rawHash' : {},
|
|
});
|
|
}
|
|
|
|
function load()
|
|
{
|
|
metaClasses.forEach(function (metaClass) {
|
|
let 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;
|
|
});
|
|
//console.log(metaClass);
|
|
});
|
|
}
|
|
|
|
function init()
|
|
{
|
|
registerMetaClass('web3server.json',
|
|
MT_WEB3SERVEWR,
|
|
''
|
|
);
|
|
registerMetaClass('contract.json',
|
|
MT_CONTRACT,
|
|
'name',
|
|
);
|
|
registerMetaClass('mysql.json',
|
|
MT_CONTRACT,
|
|
'0',
|
|
);
|
|
load();
|
|
}
|
|
|
|
function unInit()
|
|
{
|
|
|
|
}
|
|
|
|
function getMetaClass(metaIdx)
|
|
{
|
|
return metaIdx >= 0 && metaIdx < metaClasses.length ? metaClasses[metaIdx] : null;
|
|
}
|
|
|
|
function getMetaByKey(metaIdx, key)
|
|
{
|
|
let metaClass = getMetaClass(metaIdx);
|
|
return metaClass && key in metaClass['rawHash'] ? metaClass['rawHash'][key] : null;
|
|
}
|
|
|
|
function getMetaList(metaIdx)
|
|
{
|
|
let metaClass = getMetaClass(metaIdx);
|
|
return metaClass ? metaClass['rawList'] : null;
|
|
}
|
|
|
|
function getServerConf()
|
|
{
|
|
return getMetaByKey(MT_WEB3SERVEWR, '0');
|
|
}
|
|
|
|
function getMysqlConf()
|
|
{
|
|
return getMetaByKey(MT_MYSQL, '0');
|
|
}
|
|
|
|
function getContracts()
|
|
{
|
|
return getMetaList(MT_CONTRACT);
|
|
}
|
|
|
|
function getContractByName(name)
|
|
{
|
|
return getMetaByKey(MT_CONTRACT, name);
|
|
}
|
|
|
|
exports.init = init;
|
|
exports.unInit = unInit;
|
|
exports.getMetaByKey = getMetaByKey;
|
|
exports.getMetaList = getMetaList;
|
|
exports.getServerConf = getServerConf;
|
|
exports.getMysqlConf = getMysqlConf;
|
|
exports.getContracts = getContracts;
|
|
exports.getContractByName = getContractByName;
|