srvoa
Version:
Infrastructure for service oriented architecture.
187 lines (153 loc) • 6.28 kB
JavaScript
/**
* srvoa - soa infrastructure for node js
*
* @copyright Copyright (c) 2015, Alrik Zachert
* @license https://gitlab.com/kermit-js/kermit/blob/support/srvoa/LICENSE BSD-2-Clause
*/
;
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var fs = require('fs'),
Service = require('./Service'),
Config = require('./Config');
/**
* The srvoa config service class can read and merge (recursively) multiple config files.
* It wraps the config class and allows querying specific configs by nested-config-key-string-representation.
*/
var ConfigService = (function (_Service) {
_inherits(ConfigService, _Service);
/**
* The files to read and merge the config from.
*
* @property files {Array}
*/
/**
* The config instance.
*
* @property config {Config}
*/
/**
* Initialize the config service.
*
* @constructor
*/
function ConfigService(serviceManager) {
_classCallCheck(this, ConfigService);
_get(Object.getPrototypeOf(ConfigService.prototype), 'constructor', this).call(this, serviceManager);
this.config = new Config();
this.files = [];
}
/**
* Configure the config service.
* The optional config hash may contain a `files` array.
*
* @param {Object} config (optional)
* @return {ConfigService}
*/
_createClass(ConfigService, [{
key: 'configure',
value: function configure(config) {
if (config && typeof config.files !== 'undefined') {
this.setFiles(config.files);
}
return this;
}
/**
* Launch the config service by reading and merging the config files.
*
* @inheritDoc
*/
}, {
key: 'launch',
value: function launch() {
this.readConfig();
return this;
}
/**
* Return the list of files to read the config from.
*
* @return {Array}
*/
}, {
key: 'getFiles',
value: function getFiles() {
return this.files;
}
/**
* Set the files to read the config from.
*
* @param {Array} files
* @returns {ConfigService}
*/
}, {
key: 'setFiles',
value: function setFiles(files) {
this.files = files;
return this;
}
/**
* Read the config files (if existent) and recursively merge the settings.
* The reader also adds support for simple comments in the config.json files.
*
* @return {ConfigService}
*/
}, {
key: 'readConfig',
value: function readConfig() {
var files = this.files,
i = 0,
l = files.length,
file,
data;
// reset the config
this.config.setConfig({});
for (; i < l; ++i) {
file = files[i];
if (fs.existsSync(file)) {
data = require(file);
this.config.mergeConfig(data);
}
}
return this;
}
/**
* Returns the config hash of the internal config instance.
*
* @return {Object}
*/
}, {
key: 'getConfig',
value: function getConfig() {
return this.config.getHash();
}
/**
* Sets and merges multiple config sets.
*
* @param {...Object} config
* @return {ConfigService}
*/
}, {
key: 'setConfig',
value: function setConfig(config) {
this.config.setConfig.apply(this.config, arguments);
return this;
}
/**
* Retrieve a config by its key (dot separated string representation).
* If no config for the given key is found, the default value (if any) is returned.
*
* @param {String} key The dot separared key eg. `srv.module.feature.setting`
* @param {*} defaultValue
* @returns {*}
*/
}, {
key: 'get',
value: function get(key, defaultValue) {
return this.config.get(key, defaultValue);
}
}]);
return ConfigService;
})(Service);
module.exports = ConfigService;