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;
