Ghost/core/server/config/loader.js
rektide 42dc8b4a8f Accept a config filename as an optional parameter to Ghost start-up.
Closes #1110.
- Promotes config-loader from a validator, to the central place where configuration state is held
- Allow config-loader two means to be told of config file to be used:
  - A preferred first argument passed into Ghost
  - A secondary GHOST_CONFIG environmental variable
- Failing to see either of the above passed in, config-loader will continue to use "config.js"
- Config-loader validates the target configuration (unchanged) & then copies that object into it's own exports
- Components needing to read configuration now require config-loader to retrieve the configuration state
- Config file continues to be loaded via require(): this is assumed to be a static json file
2013-11-28 13:47:00 +00:00

104 lines
3.7 KiB
JavaScript

var fs = require('fs'),
url = require('url'),
when = require('when'),
errors = require('../errorHandling'),
path = require('path'),
paths = require('./paths'),
appRoot = paths().appRoot,
configexample = paths().configExample,
configFile = process.env.GHOST_CONFIG || paths().config;
function writeConfigFile() {
var written = when.defer();
/* Check for config file and copy from config.example.js
if one doesn't exist. After that, start the server. */
fs.exists(configexample, function checkTemplate(templateExists) {
var read,
write;
if (!templateExists) {
return errors.logError(new Error('Could not locate a configuration file.'), appRoot, 'Please check your deployment for config.js or config.example.js.');
}
// Copy config.example.js => config.js
read = fs.createReadStream(configexample);
read.on('error', function (err) {
/*jslint unparam:true*/
return errors.logError(new Error('Could not open config.example.js for read.'), appRoot, 'Please check your deployment for config.js or config.example.js.');
});
read.on('end', written.resolve);
write = fs.createWriteStream(configFile);
write.on('error', function (err) {
/*jslint unparam:true*/
return errors.logError(new Error('Could not open config.js for write.'), appRoot, 'Please check your deployment for config.js or config.example.js.');
});
read.pipe(write);
});
return written.promise;
}
function validateConfigEnvironment() {
var envVal = process.env.NODE_ENV || 'undefined',
hasHostAndPort,
hasSocket,
config,
parsedUrl;
try {
config = require(configFile)[envVal];
} catch (ignore) {
}
// Check if we don't even have a config
if (!config) {
errors.logError(new Error('Cannot find the configuration for the current NODE_ENV'), "NODE_ENV=" + envVal, 'Ensure your config.js has a section for the current NODE_ENV value');
return when.reject();
}
// Check that our url is valid
parsedUrl = url.parse(config.url || 'invalid', false, true);
if (!parsedUrl.host) {
errors.logError(new Error('Your site url in config.js is invalid.'), config.url, 'Please make sure this is a valid url before restarting');
return when.reject();
}
// Check that we have database values
if (!config.database) {
errors.logError(new Error('Your database configuration in config.js is invalid.'), JSON.stringify(config.database), 'Please make sure this is a valid Bookshelf database configuration');
return when.reject();
}
hasHostAndPort = config.server && !!config.server.host && !!config.server.port;
hasSocket = config.server && !!config.server.socket;
// Check for valid server host and port values
if (!config.server || !(hasHostAndPort || hasSocket)) {
errors.logError(new Error('Your server values (socket, or host and port) in config.js are invalid.'), JSON.stringify(config.server), 'Please provide them before restarting.');
return when.reject();
}
return when.resolve(config);
}
function loadConfig() {
var loaded = when.defer(),
pendingConfig;
/* Check for config file and copy from config.example.js
if one doesn't exist. After that, start the server. */
fs.exists(configFile, function checkConfig(configExists) {
if (!configExists) {
pendingConfig = writeConfigFile();
}
when(pendingConfig).then(validateConfigEnvironment).then(loaded.resolve).otherwise(loaded.reject);
});
return loaded.promise;
}
module.exports = loadConfig;