2015-05-28 18:16:09 +03:00
|
|
|
// # Ghost Server
|
|
|
|
// Handles the creation of an HTTP Server for Ghost
|
2014-08-17 10:17:23 +04:00
|
|
|
var Promise = require('bluebird'),
|
2015-05-19 18:51:53 +03:00
|
|
|
chalk = require('chalk'),
|
2014-08-19 20:36:46 +04:00
|
|
|
fs = require('fs'),
|
2014-09-23 19:59:39 +04:00
|
|
|
errors = require('./errors'),
|
2015-11-12 15:29:45 +03:00
|
|
|
config = require('./config'),
|
2016-09-19 13:28:18 +03:00
|
|
|
i18n = require('./i18n'),
|
|
|
|
moment = require('moment');
|
2014-08-19 20:36:46 +04:00
|
|
|
|
2015-05-28 18:16:09 +03:00
|
|
|
/**
|
|
|
|
* ## GhostServer
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} rootApp - parent express instance
|
|
|
|
*/
|
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-24 00:42:44 +04:00
|
|
|
|
|
|
|
// Expose config module for use externally.
|
|
|
|
this.config = config;
|
2014-08-19 20:36:46 +04:00
|
|
|
}
|
|
|
|
|
2015-05-28 18:16:09 +03:00
|
|
|
/**
|
|
|
|
* ## Public API methods
|
|
|
|
*
|
|
|
|
* ### Start
|
|
|
|
* Starts the ghost server listening on the configured port.
|
|
|
|
* Alternatively you can pass in your own express instance and let Ghost
|
|
|
|
* start listening for you.
|
|
|
|
* @param {Object} externalApp - Optional express app instance.
|
|
|
|
* @return {Promise} Resolves once Ghost has started
|
|
|
|
*/
|
|
|
|
GhostServer.prototype.start = function (externalApp) {
|
|
|
|
var self = this,
|
|
|
|
rootApp = externalApp ? externalApp : self.rootApp;
|
|
|
|
|
|
|
|
return new Promise(function (resolve) {
|
|
|
|
var socketConfig = config.getSocket();
|
|
|
|
|
|
|
|
if (socketConfig) {
|
|
|
|
// Make sure the socket is gone before trying to create another
|
|
|
|
try {
|
|
|
|
fs.unlinkSync(socketConfig.path);
|
|
|
|
} catch (e) {
|
|
|
|
// We can ignore this.
|
|
|
|
}
|
|
|
|
|
|
|
|
self.httpServer = rootApp.listen(socketConfig.path);
|
|
|
|
|
|
|
|
fs.chmod(socketConfig.path, socketConfig.permissions);
|
|
|
|
} else {
|
|
|
|
self.httpServer = rootApp.listen(
|
|
|
|
config.server.port,
|
|
|
|
config.server.host
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.httpServer.on('error', function (error) {
|
|
|
|
if (error.errno === 'EADDRINUSE') {
|
|
|
|
errors.logError(
|
2015-11-12 15:29:45 +03:00
|
|
|
i18n.t('errors.httpServer.addressInUse.error'),
|
|
|
|
i18n.t('errors.httpServer.addressInUse.context', {port: config.server.port}),
|
|
|
|
i18n.t('errors.httpServer.addressInUse.help')
|
2015-05-28 18:16:09 +03:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
errors.logError(
|
2015-11-12 15:29:45 +03:00
|
|
|
i18n.t('errors.httpServer.otherError.error', {errorNumber: error.errno}),
|
|
|
|
i18n.t('errors.httpServer.otherError.context'),
|
|
|
|
i18n.t('errors.httpServer.otherError.help')
|
2015-05-28 18:16:09 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
process.exit(-1);
|
|
|
|
});
|
|
|
|
self.httpServer.on('connection', self.connection.bind(self));
|
|
|
|
self.httpServer.on('listening', function () {
|
|
|
|
self.logStartMessages();
|
|
|
|
resolve(self);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Stop
|
|
|
|
* Returns a promise that will be fulfilled when the server stops. If the server has not been started,
|
|
|
|
* the promise will be fulfilled immediately
|
|
|
|
* @returns {Promise} Resolves once Ghost has stopped
|
|
|
|
*/
|
|
|
|
GhostServer.prototype.stop = function () {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Restart
|
|
|
|
* Restarts the ghost application
|
|
|
|
* @returns {Promise} Resolves once Ghost has restarted
|
|
|
|
*/
|
|
|
|
GhostServer.prototype.restart = function () {
|
|
|
|
return this.stop().then(this.start.bind(this));
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Hammertime
|
|
|
|
* To be called after `stop`
|
|
|
|
*/
|
|
|
|
GhostServer.prototype.hammertime = function () {
|
2015-11-12 15:29:45 +03:00
|
|
|
console.log(chalk.green(i18n.t('notices.httpServer.cantTouchThis')));
|
2015-05-28 18:16:09 +03:00
|
|
|
|
|
|
|
return Promise.resolve(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ## Private (internal) methods
|
|
|
|
*
|
|
|
|
* ### Connection
|
|
|
|
* @param {Object} socket
|
|
|
|
*/
|
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
|
|
|
};
|
|
|
|
|
2015-05-28 18:16:09 +03:00
|
|
|
/**
|
|
|
|
* ### Close Connections
|
|
|
|
* Most browsers keep a persistent connection open to the server, which prevents the close callback of
|
|
|
|
* httpServer from returning. We need to destroy all connections manually.
|
|
|
|
*/
|
2014-08-19 20:36:46 +04:00
|
|
|
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
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-05-28 18:16:09 +03:00
|
|
|
/**
|
|
|
|
* ### Log Start Messages
|
|
|
|
*/
|
2014-08-19 20:36:46 +04:00
|
|
|
GhostServer.prototype.logStartMessages = function () {
|
|
|
|
// Startup & Shutdown messages
|
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
|
|
console.log(
|
2015-11-12 15:29:45 +03:00
|
|
|
chalk.green(i18n.t('notices.httpServer.ghostIsRunningIn', {env: process.env.NODE_ENV})),
|
|
|
|
i18n.t('notices.httpServer.yourBlogIsAvailableOn', {url: config.url}),
|
|
|
|
chalk.gray(i18n.t('notices.httpServer.ctrlCToShutDown'))
|
2014-08-19 20:36:46 +04:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
console.log(
|
2015-11-12 15:29:45 +03:00
|
|
|
chalk.green(i18n.t('notices.httpServer.ghostIsRunningIn', {env: process.env.NODE_ENV})),
|
|
|
|
i18n.t('notices.httpServer.listeningOn'),
|
|
|
|
config.getSocket() || config.server.host + ':' + config.server.port,
|
|
|
|
i18n.t('notices.httpServer.urlConfiguredAs', {url: config.url}),
|
|
|
|
chalk.gray(i18n.t('notices.httpServer.ctrlCToShutDown'))
|
2014-08-19 20:36:46 +04:00
|
|
|
);
|
2014-11-05 15:20:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function shutdown() {
|
2015-11-12 15:29:45 +03:00
|
|
|
console.log(chalk.red(i18n.t('notices.httpServer.ghostHasShutdown')));
|
2014-11-05 15:20:10 +03:00
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
|
|
console.log(
|
2015-11-12 15:29:45 +03:00
|
|
|
i18n.t('notices.httpServer.yourBlogIsNowOffline')
|
2014-11-05 15:20:10 +03:00
|
|
|
);
|
|
|
|
} else {
|
2014-08-19 20:36:46 +04:00
|
|
|
console.log(
|
2015-11-12 15:29:45 +03:00
|
|
|
i18n.t('notices.httpServer.ghostWasRunningFor'),
|
2016-09-19 13:28:18 +03:00
|
|
|
moment.duration(process.uptime(), 'seconds').humanize()
|
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
|
|
|
};
|
|
|
|
|
2015-05-28 18:16:09 +03:00
|
|
|
/**
|
|
|
|
* ### Log Shutdown Messages
|
|
|
|
*/
|
2014-08-19 20:36:46 +04:00
|
|
|
GhostServer.prototype.logShutdownMessages = function () {
|
2015-11-12 15:29:45 +03:00
|
|
|
console.log(chalk.red(i18n.t('notices.httpServer.ghostIsClosingConnections')));
|
2014-08-19 20:36:46 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = GhostServer;
|