webapi-eca/js-coffee/config.js

145 lines
2.9 KiB
JavaScript
Raw Normal View History

// Generated by CoffeeScript 1.6.3
/*
Configuration
=============
2013-11-26 22:24:15 +00:00
> Loads the configuration file and acts as an interface to it.
*/
(function() {
var exports, fetchProp, fs, loadConfigFile, path,
2013-11-26 22:24:15 +00:00
_this = this;
fs = require('fs');
path = require('path');
/*
Module call
-----------
Calling the module as a function will act as a constructor and load the config file.
It is possible to hand an args object with the properties nolog (true if no outputs shall
be generated) and configPath for a custom configuration file path.
@param {Object} args
*/
exports = module.exports = function(args) {
args = args != null ? args : {};
if (args.nolog) {
_this.nolog = true;
}
if (args.configPath) {
2014-02-04 07:35:07 +00:00
loadConfigFile(args.configPath);
} else {
loadConfigFile(path.join('config', 'config.json'));
}
return module.exports;
};
/*
Tries to load a configuration file from the path relative to this module's parent folder.
Reads the config file synchronously from the file system and try to parse it.
@private loadConfigFile
2014-02-04 07:35:07 +00:00
@param {String} configPath
*/
2014-02-04 07:35:07 +00:00
loadConfigFile = function(configPath) {
var confProperties, e, isReady, prop, _i, _len;
2014-01-13 14:23:06 +00:00
_this.config = null;
confProperties = ['log', 'http-port', 'db-port'];
try {
2014-02-04 07:35:07 +00:00
_this.config = JSON.parse(fs.readFileSync(path.resolve(__dirname, '..', configPath)));
isReady = true;
for (_i = 0, _len = confProperties.length; _i < _len; _i++) {
prop = confProperties[_i];
if (!_this.config[prop]) {
isReady = false;
}
}
if (!isReady && !_this.nolog) {
return console.error("Missing property in config file, requires: \n- " + (confProperties.join("\n -")));
}
} catch (_error) {
e = _error;
if (!_this.nolog) {
return console.error("Failed loading config file: " + e.message);
}
2013-11-19 13:53:36 +00:00
}
};
/*
Fetch a property from the configuration
@private fetchProp( *prop* )
@param {String} prop
*/
fetchProp = function(prop) {
2013-11-26 22:24:15 +00:00
var _ref;
return (_ref = _this.config) != null ? _ref[prop] : void 0;
};
/*
***Returns*** true if the config file is ready, else false
@public isReady()
*/
exports.isReady = function() {
2013-11-26 22:24:15 +00:00
return _this.config != null;
};
/*
***Returns*** the HTTP port
@public getHttpPort()
*/
exports.getHttpPort = function() {
return fetchProp('http-port');
};
/*
***Returns*** the DB port*
@public getDBPort()
*/
exports.getDbPort = function() {
return fetchProp('db-port');
};
/*
***Returns*** the log conf object
@public getLogConf()
*/
exports.getLogConf = function() {
return fetchProp('log');
};
/*
***Returns*** the crypto key
@public getCryptoKey()
*/
exports.getCryptoKey = function() {
return fetchProp('crypto-key');
};
}).call(this);