diff --git a/core/config-loader.js b/core/config-loader.js index b22c77a1e7..2cc66f1f07 100644 --- a/core/config-loader.js +++ b/core/config-loader.js @@ -36,6 +36,8 @@ function writeConfigFile() { function validateConfigEnvironment() { var envVal = process.env.NODE_ENV || 'undefined', + hasHostAndPort, + hasSocket, config, parsedUrl; @@ -45,6 +47,7 @@ function validateConfigEnvironment() { } + // 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'); @@ -64,9 +67,12 @@ function validateConfigEnvironment() { 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 || !config.server.host || !config.server.port) { - errors.logError(new Error('Your server values (host and port) in config.js are invalid.'), JSON.stringify(config.server), 'Please provide them before restarting.'); + 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(); } diff --git a/core/server.js b/core/server.js index 4f752425c5..12f44c44d2 100644 --- a/core/server.js +++ b/core/server.js @@ -4,6 +4,7 @@ var express = require('express'), _ = require('underscore'), colors = require('colors'), semver = require('semver'), + fs = require('fs'), slashes = require('connect-slashes'), errors = require('./server/errorHandling'), admin = require('./server/controllers/admin'), @@ -360,68 +361,87 @@ when(ghost.init()).then(function () { server.get('/:slug/', frontend.single); server.get('/', frontend.homepage); + // Are we using sockets? Custom socket or the default? + function getSocket() { + if (ghost.config().server.hasOwnProperty('socket')) { + return _.isString(ghost.config().server.socket) ? ghost.config().server.socket : path.join(__dirname, '../content/', process.env.NODE_ENV + '.socket'); + } + return false; + } + function startGhost() { + // Tell users if their node version is not supported, and exit + if (!semver.satisfies(process.versions.node, packageInfo.engines.node)) { + console.log( + "\nERROR: Unsupported version of Node".red, + "\nGhost needs Node version".red, + packageInfo.engines.node.yellow, + "you are using version".red, + process.versions.node.yellow, + "\nPlease go to http://nodejs.org to get the latest version".green + ); + + process.exit(0); + } + + // Startup & Shutdown messages + if (process.env.NODE_ENV === 'production') { + console.log( + "Ghost is running...".green, + "\nYour blog is now available on", + ghost.config().url, + "\nCtrl+C to shut down".grey + ); + + // ensure that Ghost exits correctly on Ctrl+C + process.on('SIGINT', function () { + console.log( + "\nGhost has shut down".red, + "\nYour blog is now offline" + ); + process.exit(0); + }); + } else { + console.log( + "Ghost is running...".green, + "\nListening on", + getSocket() || ghost.config().server.host + ':' + ghost.config().server.port, + "\nUrl configured as:", + ghost.config().url, + "\nCtrl+C to shut down".grey + ); + // ensure that Ghost exits correctly on Ctrl+C + process.on('SIGINT', function () { + console.log( + "\nGhost has shutdown".red, + "\nGhost was running for", + Math.round(process.uptime()), + "seconds" + ); + process.exit(0); + }); + } + + // Let everyone know we have finished loading + loading.resolve(); + } // ## Start Ghost App - server.listen( - ghost.config().server.port, - ghost.config().server.host, - function () { + if (getSocket()) { + // Make sure the socket is gone before trying to create another + fs.unlink(getSocket(), function (err) { + server.listen( + getSocket(), + startGhost + ); + fs.chmod(getSocket(), '0744'); + }); - // Tell users if their node version is not supported, and exit - if (!semver.satisfies(process.versions.node, packageInfo.engines.node)) { - console.log( - "\nERROR: Unsupported version of Node".red, - "\nGhost needs Node version".red, - packageInfo.engines.node.yellow, - "you are using version".red, - process.versions.node.yellow, - "\nPlease go to http://nodejs.org to get the latest version".green - ); - - process.exit(0); - } - - // Startup & Shutdown messages - if (process.env.NODE_ENV === 'production') { - console.log( - "Ghost is running...".green, - "\nYour blog is now available on", - ghost.config().url, - "\nCtrl+C to shut down".grey - ); - - // ensure that Ghost exits correctly on Ctrl+C - process.on('SIGINT', function () { - console.log( - "\nGhost has shut down".red, - "\nYour blog is now offline" - ); - process.exit(0); - }); - } else { - console.log( - "Ghost is running...".green, - "\nListening on", - ghost.config().server.host + ':' + ghost.config().server.port, - "\nUrl configured as:", - ghost.config().url, - "\nCtrl+C to shut down".grey - ); - // ensure that Ghost exits correctly on Ctrl+C - process.on('SIGINT', function () { - console.log( - "\nGhost has shutdown".red, - "\nGhost was running for", - Math.round(process.uptime()), - "seconds" - ); - process.exit(0); - }); - } - - // Let everyone know we have finished loading - loading.resolve(); - } - ); + } else { + server.listen( + ghost.config().server.port, + ghost.config().server.host, + startGhost + ); + } }, errors.logAndThrowError);