Allow Ghost to run using sockets

Closes #887
- Adds getSocket function > Returns the socket location if sockets are enabled or false
- Adds startGhost function > Callback for server.listen
This commit is contained in:
Patrick Garman 2013-10-09 11:07:43 -05:00 committed by Hannah Wolfe
parent 54f8a04779
commit 97f592aa41

View File

@ -4,6 +4,7 @@ var express = require('express'),
_ = require('underscore'), _ = require('underscore'),
colors = require('colors'), colors = require('colors'),
semver = require('semver'), semver = require('semver'),
fs = require('fs'),
slashes = require('connect-slashes'), slashes = require('connect-slashes'),
errors = require('./server/errorHandling'), errors = require('./server/errorHandling'),
admin = require('./server/controllers/admin'), admin = require('./server/controllers/admin'),
@ -360,14 +361,15 @@ when(ghost.init()).then(function () {
server.get('/:slug/', frontend.single); server.get('/:slug/', frontend.single);
server.get('/', frontend.homepage); 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() {
// ## Start Ghost App
server.listen(
ghost.config().server.port,
ghost.config().server.host,
function () {
// Tell users if their node version is not supported, and exit // Tell users if their node version is not supported, and exit
if (!semver.satisfies(process.versions.node, packageInfo.engines.node)) { if (!semver.satisfies(process.versions.node, packageInfo.engines.node)) {
console.log( console.log(
@ -403,7 +405,7 @@ when(ghost.init()).then(function () {
console.log( console.log(
"Ghost is running...".green, "Ghost is running...".green,
"\nListening on", "\nListening on",
ghost.config().server.host + ':' + ghost.config().server.port, getSocket() || ghost.config().server.host + ':' + ghost.config().server.port,
"\nUrl configured as:", "\nUrl configured as:",
ghost.config().url, ghost.config().url,
"\nCtrl+C to shut down".grey "\nCtrl+C to shut down".grey
@ -423,5 +425,23 @@ when(ghost.init()).then(function () {
// Let everyone know we have finished loading // Let everyone know we have finished loading
loading.resolve(); loading.resolve();
} }
// ## Start Ghost App
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');
});
} else {
server.listen(
ghost.config().server.port,
ghost.config().server.host,
startGhost
);
}
}, errors.logAndThrowError); }, errors.logAndThrowError);