mirror of
https://github.com/Hopiu/webapi-eca.git
synced 2026-05-01 03:54:48 +00:00
135 lines
2.5 KiB
JavaScript
135 lines
2.5 KiB
JavaScript
// Generated by CoffeeScript 1.6.3
|
|
/*
|
|
|
|
Config
|
|
======
|
|
|
|
Loads the configuration file and acts as an interface to it.
|
|
*/
|
|
|
|
|
|
(function() {
|
|
'use strict';
|
|
var config, exports, fetchProp, fs, loadConfigFile, log, path;
|
|
|
|
path = require('path');
|
|
|
|
log = require('./logging');
|
|
|
|
fs = require('fs');
|
|
|
|
config = null;
|
|
|
|
/*
|
|
##Module call
|
|
|
|
Calling the module as a function will make it look for the `relPath` property in the
|
|
args object and then try to load a config file from that relative path.
|
|
@param {Object} args
|
|
*/
|
|
|
|
|
|
exports = module.exports = function(args) {
|
|
args = args != null ? args : {};
|
|
log(args);
|
|
if (typeof args.relPath === 'string') {
|
|
loadConfigFiles(args.relPath);
|
|
}
|
|
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
|
|
@param {String} relPath
|
|
*/
|
|
|
|
|
|
loadConfigFile = function(relPath) {
|
|
var e;
|
|
try {
|
|
config = JSON.parse(fs.readFileSync(path.resolve(__dirname, '..', relPath)));
|
|
if (config && config.http_port && config.db_port && config.crypto_key && config.session_secret) {
|
|
return log.print('CF', 'config file loaded successfully');
|
|
} else {
|
|
return log.error('CF', new Error("Missing property in config file, requires:\n- http_port\n- db_port\n- crypto_key\n- session_secret"));
|
|
}
|
|
} catch (_error) {
|
|
e = _error;
|
|
e.addInfo = 'no config ready';
|
|
return log.error('CF', e);
|
|
}
|
|
};
|
|
|
|
loadConfigFile(path.join('config', 'config.json'));
|
|
|
|
/*
|
|
Fetch a property from the configuration
|
|
|
|
@private fetchProp( *prop* )
|
|
@param {String} prop
|
|
*/
|
|
|
|
|
|
fetchProp = function(prop) {
|
|
return config != null ? config[prop] : void 0;
|
|
};
|
|
|
|
/*
|
|
Answer true if the config file is ready, else false
|
|
|
|
@public isReady()
|
|
*/
|
|
|
|
|
|
exports.isReady = function() {
|
|
return 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 crypto key
|
|
|
|
@public getCryptoKey()
|
|
*/
|
|
|
|
|
|
exports.getCryptoKey = function() {
|
|
return fetchProp('crypto_key');
|
|
};
|
|
|
|
/*
|
|
Returns the session secret
|
|
|
|
@public getSessionSecret()
|
|
*/
|
|
|
|
|
|
exports.getSessionSecret = function() {
|
|
return fetchProp('session_secret');
|
|
};
|
|
|
|
}).call(this);
|