webio-node/libs/confLoader.js

37 lines
746 B
JavaScript
Raw Normal View History

2017-03-22 05:35:45 +00:00
var fs = require('fs');
var path = require('path');
var confLoader = {
_conf: null,
_path: null,
load: (p) => {
var self = this;
if (!p || p == undefined) {
p = path.resolve(__dirname, '..', 'config.json');
}
self._path = p;
if (!fs.existsSync(p)) {
throw `Config file not exists , file location : ${p}`;
}
var filecontent = fs.readFileSync(p);
if (!filecontent) {
throw `Config file read fail`;
}
filecontent = filecontent.toString();
var conf = null;
try {
conf = JSON.parse(filecontent);
self._conf = conf;
} catch (e) {
throw e;
}
return self._conf;
},
get: (key) => {
return this._conf[key];
}
};
module.exports = confLoader;