2014-08-17 10:17:23 +04:00
|
|
|
var Promise = require('bluebird'),
|
2014-08-19 20:36:46 +04:00
|
|
|
fs = require('fs'),
|
|
|
|
semver = require('semver'),
|
|
|
|
packageInfo = require('../../package.json'),
|
2014-09-23 19:59:39 +04:00
|
|
|
errors = require('./errors'),
|
2014-08-19 20:36:46 +04:00
|
|
|
config = require('./config');
|
|
|
|
|
2014-09-19 20:17:58 +04:00
|
|
|
function GhostServer(rootApp) {
|
|
|
|
this.rootApp = rootApp;
|
2014-08-19 20:36:46 +04:00
|
|
|
this.httpServer = null;
|
2014-11-11 09:35:37 +03:00
|
|
|
this.connections = {};
|
|
|
|
this.connectionId = 0;
|
2014-08-19 20:36:46 +04:00
|
|
|
this.upgradeWarning = setTimeout(this.logUpgradeWarning.bind(this), 5000);
|
2014-08-24 00:42:44 +04:00
|
|
|
|
|
|
|
// Expose config module for use externally.
|
|
|
|
this.config = config;
|
2014-08-19 20:36:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
GhostServer.prototype.connection = function (socket) {
|
2014-11-11 09:35:37 +03:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
self.connectionId += 1;
|
|
|
|
socket._ghostId = self.connectionId;
|
|
|
|
|
|
|
|
socket.on('close', function () {
|
|
|
|
delete self.connections[this._ghostId];
|
|
|
|
});
|
|
|
|
|
|
|
|
self.connections[socket._ghostId] = socket;
|
2014-08-19 20:36:46 +04:00
|
|
|
};
|
|
|
|
|
2014-11-11 09:35:37 +03:00
|
|
|
// Most browsers keep a persistent connection open to the server
|
2014-08-19 20:36:46 +04:00
|
|
|
// which prevents the close callback of httpServer from returning
|
|
|
|
// We need to destroy all connections manually
|
|
|
|
GhostServer.prototype.closeConnections = function () {
|
2014-11-11 09:35:37 +03:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
Object.keys(self.connections).forEach(function (socketId) {
|
|
|
|
var socket = self.connections[socketId];
|
|
|
|
|
|
|
|
if (socket) {
|
|
|
|
socket.destroy();
|
|
|
|
}
|
2014-08-19 20:36:46 +04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
GhostServer.prototype.logStartMessages = function () {
|
|
|
|
// Tell users if their node version is not supported, and exit
|
2015-02-04 00:44:59 +03:00
|
|
|
if (!semver.satisfies(process.versions.node, packageInfo.engines.node) &&
|
|
|
|
!semver.satisfies(process.versions.node, packageInfo.engines.iojs)) {
|
2014-08-19 20:36:46 +04:00
|
|
|
console.log(
|
2014-09-10 08:06:24 +04:00
|
|
|
'\nERROR: Unsupported version of Node'.red,
|
|
|
|
'\nGhost needs Node version'.red,
|
2014-08-19 20:36:46 +04:00
|
|
|
packageInfo.engines.node.yellow,
|
2014-09-10 08:06:24 +04:00
|
|
|
'you are using version'.red,
|
2014-08-19 20:36:46 +04:00
|
|
|
process.versions.node.yellow,
|
2014-09-10 08:06:24 +04:00
|
|
|
'\nPlease go to http://nodejs.org to get a supported version'.green
|
2014-08-19 20:36:46 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Startup & Shutdown messages
|
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
|
|
console.log(
|
2014-09-10 08:06:24 +04:00
|
|
|
'Ghost is running...'.green,
|
|
|
|
'\nYour blog is now available on',
|
2014-08-19 20:36:46 +04:00
|
|
|
config.url,
|
2014-09-10 08:06:24 +04:00
|
|
|
'\nCtrl+C to shut down'.grey
|
2014-08-19 20:36:46 +04:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
console.log(
|
2014-09-10 08:06:24 +04:00
|
|
|
('Ghost is running in ' + process.env.NODE_ENV + '...').green,
|
|
|
|
'\nListening on',
|
2014-08-19 20:36:46 +04:00
|
|
|
config.getSocket() || config.server.host + ':' + config.server.port,
|
2014-09-10 08:06:24 +04:00
|
|
|
'\nUrl configured as:',
|
2014-08-19 20:36:46 +04:00
|
|
|
config.url,
|
2014-09-10 08:06:24 +04:00
|
|
|
'\nCtrl+C to shut down'.grey
|
2014-08-19 20:36:46 +04:00
|
|
|
);
|
2014-11-05 15:20:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function shutdown() {
|
|
|
|
console.log('\nGhost has shut down'.red);
|
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
|
|
console.log(
|
|
|
|
'\nYour blog is now offline'
|
|
|
|
);
|
|
|
|
} else {
|
2014-08-19 20:36:46 +04:00
|
|
|
console.log(
|
2014-09-10 08:06:24 +04:00
|
|
|
'\nGhost was running for',
|
2014-08-19 20:36:46 +04:00
|
|
|
Math.round(process.uptime()),
|
2014-09-10 08:06:24 +04:00
|
|
|
'seconds'
|
2014-08-19 20:36:46 +04:00
|
|
|
);
|
2014-11-05 15:20:10 +03:00
|
|
|
}
|
|
|
|
process.exit(0);
|
2014-08-19 20:36:46 +04:00
|
|
|
}
|
2014-11-05 15:20:10 +03:00
|
|
|
// ensure that Ghost exits correctly on Ctrl+C and SIGTERM
|
|
|
|
process.
|
|
|
|
removeAllListeners('SIGINT').on('SIGINT', shutdown).
|
|
|
|
removeAllListeners('SIGTERM').on('SIGTERM', shutdown);
|
2014-08-19 20:36:46 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
GhostServer.prototype.logShutdownMessages = function () {
|
|
|
|
console.log('Ghost is closing connections'.red);
|
|
|
|
};
|
|
|
|
|
|
|
|
GhostServer.prototype.logUpgradeWarning = function () {
|
2014-09-23 19:59:39 +04:00
|
|
|
errors.logWarn(
|
|
|
|
'Ghost no longer starts automatically when using it as an npm module.',
|
|
|
|
'If you\'re seeing this message, you may need to update your custom code.',
|
|
|
|
'Please see the docs at http://tinyurl.com/npm-upgrade for more information.'
|
|
|
|
);
|
2014-08-19 20:36:46 +04:00
|
|
|
};
|
|
|
|
|
2014-08-24 00:42:44 +04:00
|
|
|
/**
|
|
|
|
* Starts the ghost server listening on the configured port.
|
|
|
|
* Alternatively you can pass in your own express instance and let Ghost
|
2015-03-15 00:50:00 +03:00
|
|
|
* start listening for you.
|
2014-08-24 00:42:44 +04:00
|
|
|
* @param {Object=} externalApp Optional express app instance.
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
GhostServer.prototype.start = function (externalApp) {
|
|
|
|
var self = this,
|
2014-09-19 20:17:58 +04:00
|
|
|
rootApp = externalApp ? externalApp : self.rootApp;
|
2014-08-17 10:17:23 +04:00
|
|
|
|
2014-08-19 20:36:46 +04:00
|
|
|
// ## Start Ghost App
|
2014-08-17 10:17:23 +04:00
|
|
|
return new Promise(function (resolve) {
|
2014-12-31 18:23:46 +03:00
|
|
|
var socketConfig = config.getSocket();
|
|
|
|
|
|
|
|
if (socketConfig) {
|
2014-08-17 10:17:23 +04:00
|
|
|
// Make sure the socket is gone before trying to create another
|
|
|
|
try {
|
2014-12-31 18:23:46 +03:00
|
|
|
fs.unlinkSync(socketConfig.path);
|
2014-08-17 10:17:23 +04:00
|
|
|
} catch (e) {
|
|
|
|
// We can ignore this.
|
|
|
|
}
|
|
|
|
|
2014-12-31 18:23:46 +03:00
|
|
|
self.httpServer = rootApp.listen(socketConfig.path);
|
2014-08-19 20:36:46 +04:00
|
|
|
|
2014-12-31 18:23:46 +03:00
|
|
|
fs.chmod(socketConfig.path, socketConfig.permissions);
|
2014-08-17 10:17:23 +04:00
|
|
|
} else {
|
2014-09-19 20:17:58 +04:00
|
|
|
self.httpServer = rootApp.listen(
|
2014-08-17 10:17:23 +04:00
|
|
|
config.server.port,
|
|
|
|
config.server.host
|
|
|
|
);
|
|
|
|
}
|
2014-08-19 20:36:46 +04:00
|
|
|
|
2014-09-09 18:20:31 +04:00
|
|
|
self.httpServer.on('error', function (error) {
|
|
|
|
if (error.errno === 'EADDRINUSE') {
|
2014-09-23 19:59:39 +04:00
|
|
|
errors.logError(
|
|
|
|
'(EADDRINUSE) Cannot start Ghost.',
|
|
|
|
'Port ' + config.server.port + ' is already in use by another program.',
|
|
|
|
'Is another Ghost instance already running?'
|
|
|
|
);
|
2014-09-09 18:20:31 +04:00
|
|
|
} else {
|
2014-09-23 19:59:39 +04:00
|
|
|
errors.logError(
|
|
|
|
'(Code: ' + error.errno + ')',
|
|
|
|
'There was an error starting your server.',
|
|
|
|
'Please use the error code above to search for a solution.'
|
2014-09-09 18:20:31 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
process.exit(-1);
|
|
|
|
});
|
2014-08-17 10:17:23 +04:00
|
|
|
self.httpServer.on('connection', self.connection.bind(self));
|
|
|
|
self.httpServer.on('listening', function () {
|
|
|
|
self.logStartMessages();
|
|
|
|
clearTimeout(self.upgradeWarning);
|
|
|
|
resolve(self);
|
|
|
|
});
|
|
|
|
});
|
2014-08-19 20:36:46 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Returns a promise that will be fulfilled when the server stops.
|
|
|
|
// If the server has not been started, the promise will be fulfilled
|
|
|
|
// immediately
|
|
|
|
GhostServer.prototype.stop = function () {
|
2014-08-17 10:17:23 +04:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
return new Promise(function (resolve) {
|
|
|
|
if (self.httpServer === null) {
|
|
|
|
resolve(self);
|
|
|
|
} else {
|
|
|
|
self.httpServer.close(function () {
|
|
|
|
self.httpServer = null;
|
|
|
|
self.logShutdownMessages();
|
|
|
|
resolve(self);
|
|
|
|
});
|
|
|
|
|
|
|
|
self.closeConnections();
|
|
|
|
}
|
|
|
|
});
|
2014-08-19 20:36:46 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Restarts the ghost application
|
|
|
|
GhostServer.prototype.restart = function () {
|
|
|
|
return this.stop().then(this.start.bind(this));
|
|
|
|
};
|
|
|
|
|
|
|
|
// To be called after `stop`
|
|
|
|
GhostServer.prototype.hammertime = function () {
|
|
|
|
console.log('Can\'t touch this'.green);
|
2014-08-17 10:17:23 +04:00
|
|
|
|
|
|
|
return Promise.resolve(this);
|
2014-08-19 20:36:46 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = GhostServer;
|