95 lines
2.0 KiB
JavaScript
95 lines
2.0 KiB
JavaScript
var fs = require('fs');
|
|
var metaClasses = [];
|
|
|
|
const MT_WEB3SERVEWR = 0;
|
|
const MT_CONTRACT = 1;
|
|
|
|
function registerMetaClass(fileName, idx, primKey)
|
|
{
|
|
metaClasses.push({
|
|
'fileName' : fileName,
|
|
'idx' : idx,
|
|
'primKey' : primKey,
|
|
'rawList' : [],
|
|
'rawHash' : {},
|
|
});
|
|
}
|
|
|
|
function load()
|
|
{
|
|
metaClasses.forEach(function (metaClass) {
|
|
let jsondata = fs.readFileSync(metaClass['fileName'], "utf8");
|
|
let data = JSON.parse(jsondata);
|
|
if (!data) {
|
|
throw new Error('读取配置' + metaClass['fileName'] + '失败');
|
|
}
|
|
if (Array.isArray(data)) {
|
|
metaClass['rawList'] = data;
|
|
} else {
|
|
metaClass['rawList'].push(data);
|
|
}
|
|
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',
|
|
);
|
|
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 getContracts()
|
|
{
|
|
return getMetaList(MT_CONTRACT);
|
|
}
|
|
|
|
exports.init = init;
|
|
exports.unInit = unInit;
|
|
exports.getMetaByKey = getMetaByKey;
|
|
exports.getMetaList = getMetaList;
|
|
exports.getServerConf = getServerConf;
|
|
exports.getContracts = getContracts;
|