Permalink
Browse files

Config file cleanup and start of PencilBlue modularization

  • Loading branch information...
Brian Hyder
Brian Hyder committed Jan 28, 2015
1 parent 495fb45 commit 666e1fb0e5f374cd73d419d8d3a58db0711bfd9a
Showing with 154 additions and 51 deletions.
  1. +136 −46 include/config.js
  2. +1 −0 lib/index.js
  3. +17 −5 pencilblue.js
View
@@ -33,19 +33,96 @@ var winston = require('winston');
* 2) Override the properties as desired.
* 3) Add any custom properties you wish to provide for your specific purposes.
*/
function Configuration(){}
// Don't change this setting
global.DOCUMENT_ROOT = __dirname.substr(0, __dirname.indexOf(path.sep+'include'));
global.EXTERNAL_ROOT = path.join(path.sep, 'etc', 'pencilblue');
/**
*
* @static
* @readonly
* @property DOCUMENT_ROOT
* @type {String}
*/
Configuration.DOCUMENT_ROOT = __dirname.substr(0, __dirname.indexOf(path.sep+'include'));
global.LOG_LEVEL = 'info';
global.LOG_DIR = path.join(DOCUMENT_ROOT, 'log');
global.LOG_FILE = path.join(LOG_DIR, 'pencilblue.log');
/**
*
* @static
* @readonly
* @property EXTERNAL_ROOT
* @type {String}
*/
Configuration.EXTERNAL_ROOT = path.join(path.sep, 'etc', 'pencilblue');
/**
* Ascending index value
* @private
* @static
* @readonly
* @property ASC
* @type {Integer}
*/
var ASC = 1;
/**
* Descending index value
* @private
* @static
* @readonly
* @property DESC
* @type {Integer}
*/
var DESC = -1;
var config = {
/**
* The default logging directory absolute file path
* @private
* @static
* @readonly
* @property LOG_DIR
* @type {String}
*/
var LOG_DIR = path.join(Configuration.DOCUMENT_ROOT, 'log');
/**
* The default logging file absolute path
* @private
* @static
* @readonly
* @property LOG_FILESC
* @type {String}
*/
var LOG_FILE = path.join(LOG_DIR, 'pencilblue.log');
/**
* The configuration module overrides file name
* @private
* @static
* @readonly
* @property CONFIG_MODULE_NAME
* @type {String}
*/
var CONFIG_MODULE_NAME = 'config.js';
/**
* The default list of absolute file paths to try when loading the configuration
* @private
* @static
* @readonly
* @property OVERRIDE_FILE_PATHS
* @type {Array}
*/
var OVERRIDE_FILE_PATHS = [
path.join(Configuration.DOCUMENT_ROOT, CONFIG_MODULE_NAME),
path.join(Configuration.EXTERNAL_ROOT, CONFIG_MODULE_NAME),
];
/**
* @private
* @static
* @readonly
* @property BASE_CONFIG
*/
var BASE_CONFIG = {
//The name of the site.
siteName: 'pencilblue',
@@ -63,7 +140,7 @@ var config = {
sitePort: process.env.port || process.env.PORT || 8080,
//the absolute file path to the directory where installation lives
docRoot: DOCUMENT_ROOT,
docRoot: Configuration.DOCUMENT_ROOT,
//provides a configuration for connecting to persistent storage. The
//default configuration is meant for mongodb.
@@ -397,7 +474,7 @@ var config = {
},
//The global log level: silly, debug, info, warn, error
log_level: LOG_LEVEL,
log_level: "info",
//System settings always have the persistent storage layer on. Optionally,
//the cache and/or memory can be used. It is not recommended to use memory
@@ -539,67 +616,86 @@ var config = {
//The maximum size of media files that can be uploaded to the server in
//bytes
max_upload_size: 2 * 1024 * 1024
}
},
//Pulls in the package.json file for PB and extracts the version so it is
//available in the configuration.
version: require(path.join(Configuration.DOCUMENT_ROOT, 'package.json')).version
};
var CONFIG_FILE_NAME = 'config.json';
var CONFIG_MODULE_NAME = 'config.js';
var OVERRIDE_FILE_PATHS = [
path.join(DOCUMENT_ROOT, CONFIG_FILE_NAME),
path.join(EXTERNAL_ROOT, CONFIG_FILE_NAME),
path.join(DOCUMENT_ROOT, CONFIG_MODULE_NAME),
path.join(EXTERNAL_ROOT, CONFIG_MODULE_NAME),
];
/**
* Retrieve the base configuration
*/
Configuration.getBaseConfig = function() {
return util.clone(BASE_CONFIG);
};
/**
* Loads an external configuration.
* NOTE: This should only be called once by the core code at startup. Calling
* this function after the server starts may cause unintended behavior across
* the system.
*/
var loadConfiguration = function() {
// If no log file exists, we should create one
if (!fs.existsSync(LOG_FILE)) {
if (!fs.existsSync(LOG_DIR)) {
fs.mkdirSync(LOG_DIR);
}
console.log('SystemStartup: Creating log file [%s]', LOG_FILE);
fs.writeFileSync(LOG_FILE, '');
Configuration.load = function(filePaths) {
if (util.isString(filePaths)) {
filePaths = [filePaths];
}
else if (!filePaths) {
filePaths = OVERRIDE_FILE_PATHS;
}
//find the override file, if exists
var override = {};
var overrideFile = null;
var overridesFound = false;
for (var i = 0; i < OVERRIDE_FILE_PATHS.length; i++) {
for (var i = 0; i < filePaths.length; i++) {
overrideFile = OVERRIDE_FILE_PATHS[i];
overrideFile = filePaths[i];
if (fs.existsSync(overrideFile)) {
try{
override = require(overrideFile);
overridesFound = true;
break;
override = require(overrideFile);
overridesFound = true;
break;
}
catch(e){
console.log('SystemStartup: Failed to parse configuration file [%s]: %s', overrideFile, e.stack);
console.log('SystemStartup: Failed to parse configuration file [%s]: %s', overrideFile, e.stack);
}
}
else {
console.log('SystemStartup: No configuration file [%s] found.', overrideFile);
}
}
//log result
var message = overridesFound ? 'Override file ['+overrideFile+'] will be applied.' : 'No overrides are available, skipping to defaults';
console.log('SystemStartup: %s', message);
var message;
if (overridesFound) {
message = util.format('Override file [%s] will be applied.', overrideFile);
}
else {
message = 'No overrides are available, skipping to defaults after searching the following files:';
filePaths.forEach(function(filePath) {
message += util.format('\n* %s', filePath);
});
}
//perform any overrides
config = util.deepMerge(override, config);
return Configuration.mergeWithBase(override);
};
Configuration.mergeWithBase = function(overrides) {
var config = util.deepMerge(overrides, Configuration.getBaseConfig());
//setup logging
if (!config.logging) {
// If no log file exists, we should create one
if (!fs.existsSync(LOG_FILE)) {
if (!fs.existsSync(LOG_DIR)) {
fs.mkdirSync(LOG_DIR);
}
console.log('SystemStartup: Creating log file [%s]', LOG_FILE);
fs.writeFileSync(LOG_FILE, '');
}
config.logging = {
transports: [
new (winston.transports.Console)({ level: config.log_level, timestamp: true, label: cluster.worker ? cluster.worker.id : 'M'}),
@@ -612,15 +708,9 @@ var loadConfiguration = function() {
if (config.siteRoot.lastIndexOf('/') === (config.siteRoot.length - 1)) {
config.siteRoot = config.siteRoot.substring(0, config.siteRoot.length - 1);
}
//ensure that the version is provided
var packageInfo = require(path.join(DOCUMENT_ROOT, 'package.json'));
config.version = packageInfo.version;
return config;
};
//export configuration
var config = loadConfiguration();
config.loadConfiguration = loadConfiguration;
module.exports = Object.freeze(config);
module.exports = Configuration;
View
@@ -0,0 +1 @@
module.exports = require('../include/requirements.js');
View
@@ -21,7 +21,7 @@ var http = require('http');
var https = require('https');
var async = require('async');
var util = require('./include/util.js');
var pb = require('./include/requirements');
var lib = require('./lib');
/**
* The main driver file for PencilBlue. Provides the function necessary to
@@ -31,14 +31,16 @@ var pb = require('./include/requirements');
* @class PencilBlue
* @constructor
*/
function PencilBlue(){}
function PencilBlue(config){
this.pb = lib(config);
};
/**
* To be called when the configuration is loaded. The function is responsible
* for triggered the startup of the HTTP connection listener as well as start a
* connection pool to the core DB.
*/
PencilBlue.init = function(){
PencilBlue.prototype.init = function(){
var tasks = [
PencilBlue.initRequestHandler,
PencilBlue.initDBConnections,
@@ -252,5 +254,15 @@ PencilBlue.initLibraries = function(cb) {
pb.libraries.init(cb);
};
//start system
pb.system.onStart(PencilBlue.init);
PencilBlue.prototype.start = function() {
pb.system.onStart(PencilBlue.init);
}
//start system only when the module is called directly
if (require.main === module) {
var Configuration = require('./include/config.js');
var config = Configuration.load();
var pb = new PencilBlue(config);
pb.start();
}

0 comments on commit 666e1fb

Please sign in to comment.