webapi-eca/js/config.js
Dominic Bosch 8c115aa71d What a wonderful detour over docco, only to recognize that it was always possible to auto generate documentation.
... if one would just define the correct folder...
at least deep knowledge about docco now, plus that it doesn't use glob (leaves it a user task...) while groc does use glob to resolve file paths, yay!
2013-11-20 15:41:41 +01:00

79 lines
No EOL
1.6 KiB
JavaScript

'use strict';
var path = require('path'),
log = require('./logging'),
config;
exports = module.exports = function(args) {
args = args || {};
log(args);
if(typeof args.relPath === 'string') loadConfigFile(args.relPath);
return module.exports;
};
loadConfigFile(path.join('config', 'config.json'));
function loadConfigFile(relPath) {
try {
config = JSON.parse(require('fs').readFileSync(path.resolve(__dirname, '..', relPath)));
if(config && config.http_port && config.db_port
&& config.crypto_key && config.session_secret) {
log.print('CF', 'config file loaded successfully!');
} else {
log.error('CF', new Error('Missing property in config file, requires:\n'
+ ' - http_port\n'
+ ' - db_port\n'
+ ' - crypto_key\n'
+ ' - session_secret'));
}
} catch (e) {
e.addInfo = 'no config ready';
log.error('CF', e);
}
}
/**
* Answer true if the config file is ready, else false
*/
exports.isReady = function() {
if(config) return true;
else return false;
};
/**
* Fetch a property from the configuration
* @param {String} prop
*/
function fetchProp(prop) {
if(config) return config[prop];
}
/**
* Get the HTTP port
*/
exports.getHttpPort = function() {
return fetchProp('http_port');
};
/**
* Get the DB port
*/
exports.getDBPort = function() {
return fetchProp('db_port');
};
/**
* Get the crypto key
*/
exports.getCryptoKey = function() {
return fetchProp('crypto_key');
};
/**
* Get the session secret
*/
exports.getSessionSecret = function() {
return fetchProp('session_secret');
};