webio-node/libs/confLoader_cls.js

40 lines
767 B
JavaScript
Raw Normal View History

2017-03-22 05:35:45 +00:00
var fs = require('fs');
var path = require('path');
class confLoader {
constructor() {
this._conf = null;
this._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;
}
get(key) {
return this._conf[key];
}
}
module.exports = confLoader;