2013-09-06 19:54:50 +04:00
|
|
|
// Module dependencies
|
2013-12-31 03:13:25 +04:00
|
|
|
var crypto = require('crypto'),
|
|
|
|
express = require('express'),
|
|
|
|
hbs = require('express-hbs'),
|
2014-05-04 22:32:37 +04:00
|
|
|
compress = require('compression'),
|
2013-12-31 03:13:25 +04:00
|
|
|
fs = require('fs'),
|
|
|
|
uuid = require('node-uuid'),
|
|
|
|
Polyglot = require('node-polyglot'),
|
|
|
|
semver = require('semver'),
|
2014-02-05 12:40:30 +04:00
|
|
|
_ = require('lodash'),
|
2013-12-31 03:13:25 +04:00
|
|
|
when = require('when'),
|
|
|
|
|
|
|
|
api = require('./api'),
|
|
|
|
config = require('./config'),
|
2014-05-09 14:11:29 +04:00
|
|
|
errors = require('./errors'),
|
2013-12-31 03:13:25 +04:00
|
|
|
helpers = require('./helpers'),
|
|
|
|
mailer = require('./mail'),
|
|
|
|
middleware = require('./middleware'),
|
|
|
|
models = require('./models'),
|
|
|
|
permissions = require('./permissions'),
|
2014-01-21 12:45:27 +04:00
|
|
|
apps = require('./apps'),
|
2013-12-31 03:13:25 +04:00
|
|
|
routes = require('./routes'),
|
|
|
|
packageInfo = require('../../package.json'),
|
|
|
|
|
2013-09-06 19:54:50 +04:00
|
|
|
// Variables
|
2014-03-25 16:42:52 +04:00
|
|
|
httpServer,
|
2013-12-06 18:13:15 +04:00
|
|
|
dbHash;
|
2013-09-06 19:54:50 +04:00
|
|
|
|
2013-10-07 17:28:53 +04:00
|
|
|
// If we're in development mode, require "when/console/monitor"
|
2013-11-25 23:20:01 +04:00
|
|
|
// for help in seeing swallowed promise errors, and log any
|
|
|
|
// stderr messages from bluebird promises.
|
2013-10-07 17:28:53 +04:00
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
require('when/monitor/console');
|
|
|
|
}
|
|
|
|
|
2013-12-06 18:13:15 +04:00
|
|
|
function doFirstRun() {
|
|
|
|
var firstRunMessage = [
|
|
|
|
'Welcome to Ghost.',
|
|
|
|
'You\'re running under the <strong>',
|
|
|
|
process.env.NODE_ENV,
|
|
|
|
'</strong>environment.',
|
|
|
|
|
|
|
|
'Your URL is set to',
|
|
|
|
'<strong>' + config().url + '</strong>.',
|
|
|
|
'See <a href="http://docs.ghost.org/">http://docs.ghost.org</a> for instructions.'
|
|
|
|
];
|
|
|
|
|
|
|
|
return api.notifications.add({
|
|
|
|
type: 'info',
|
2014-04-29 00:58:18 +04:00
|
|
|
message: firstRunMessage.join(' ')
|
2013-12-06 18:13:15 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function initDbHashAndFirstRun() {
|
2014-05-07 04:49:25 +04:00
|
|
|
return api.settings.read.call({ internal: true }, 'dbHash').then(function (response) {
|
2014-04-28 03:28:50 +04:00
|
|
|
var hash = response.settings[0].value,
|
|
|
|
initHash;
|
|
|
|
|
|
|
|
dbHash = hash;
|
2013-12-16 14:16:06 +04:00
|
|
|
|
|
|
|
if (dbHash === null) {
|
2014-04-28 03:28:50 +04:00
|
|
|
initHash = uuid.v4();
|
2014-05-07 04:49:25 +04:00
|
|
|
return api.settings.edit.call({ internal: true }, 'dbHash', initHash).then(function (response) {
|
2014-04-28 03:28:50 +04:00
|
|
|
dbHash = response.settings[0].value;
|
2013-12-16 14:16:06 +04:00
|
|
|
return dbHash;
|
|
|
|
}).then(doFirstRun);
|
|
|
|
}
|
2014-05-04 22:32:37 +04:00
|
|
|
|
2014-04-28 03:28:50 +04:00
|
|
|
return dbHash;
|
2013-12-06 18:13:15 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-01-15 03:14:44 +04:00
|
|
|
// Checks for the existence of the "built" javascript files from grunt concat.
|
2014-02-09 01:16:58 +04:00
|
|
|
// Returns a promise that will be resolved if all files exist or rejected if
|
2014-01-15 03:14:44 +04:00
|
|
|
// any are missing.
|
|
|
|
function builtFilesExist() {
|
|
|
|
var deferreds = [],
|
2014-01-05 10:40:53 +04:00
|
|
|
location = config().paths.builtScriptPath,
|
2014-01-15 03:14:44 +04:00
|
|
|
|
|
|
|
fileNames = process.env.NODE_ENV === 'production' ?
|
|
|
|
helpers.scriptFiles.production : helpers.scriptFiles.development;
|
|
|
|
|
|
|
|
function checkExist(fileName) {
|
|
|
|
var deferred = when.defer(),
|
|
|
|
errorMessage = "Javascript files have not been built.",
|
|
|
|
errorHelp = "\nPlease read the getting started instructions at:" +
|
|
|
|
"\nhttps://github.com/TryGhost/Ghost#getting-started-guide-for-developers";
|
|
|
|
|
|
|
|
fs.exists(fileName, function (exists) {
|
|
|
|
if (exists) {
|
|
|
|
deferred.resolve(true);
|
|
|
|
} else {
|
|
|
|
var err = new Error(errorMessage);
|
|
|
|
|
|
|
|
err.help = errorHelp;
|
|
|
|
deferred.reject(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
fileNames.forEach(function (fileName) {
|
|
|
|
deferreds.push(checkExist(location + fileName));
|
|
|
|
});
|
|
|
|
|
|
|
|
return when.all(deferreds);
|
|
|
|
}
|
|
|
|
|
2014-03-25 16:42:52 +04:00
|
|
|
function ghostStartMessages() {
|
|
|
|
// 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 a supported version".green
|
|
|
|
);
|
2014-02-20 07:22:02 +04:00
|
|
|
|
2014-03-25 16:42:52 +04:00
|
|
|
process.exit(0);
|
|
|
|
}
|
2014-02-20 07:22:02 +04:00
|
|
|
|
2014-03-25 16:42:52 +04:00
|
|
|
// Startup & Shutdown messages
|
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
|
|
console.log(
|
|
|
|
"Ghost is running...".green,
|
|
|
|
"\nYour blog is now available on",
|
|
|
|
config().url,
|
|
|
|
"\nCtrl+C to shut down".grey
|
|
|
|
);
|
2014-02-20 07:22:02 +04:00
|
|
|
|
2014-03-25 16:42:52 +04:00
|
|
|
// ensure that Ghost exits correctly on Ctrl+C
|
|
|
|
process.on('SIGINT', function () {
|
2014-02-20 07:22:02 +04:00
|
|
|
console.log(
|
2014-03-25 16:42:52 +04:00
|
|
|
"\nGhost has shut down".red,
|
|
|
|
"\nYour blog is now offline"
|
2014-02-20 07:22:02 +04:00
|
|
|
);
|
2014-03-25 16:42:52 +04:00
|
|
|
process.exit(0);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.log(
|
|
|
|
("Ghost is running in " + process.env.NODE_ENV + "...").green,
|
|
|
|
"\nListening on",
|
2014-02-20 07:22:02 +04:00
|
|
|
config.getSocket() || config().server.host + ':' + config().server.port,
|
2014-03-25 16:42:52 +04:00
|
|
|
"\nUrl configured as:",
|
|
|
|
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"
|
2014-02-20 07:22:02 +04:00
|
|
|
);
|
2014-03-25 16:42:52 +04:00
|
|
|
process.exit(0);
|
|
|
|
});
|
|
|
|
}
|
2014-02-20 07:22:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ## Initializes the ghost application.
|
2013-11-20 06:43:55 +04:00
|
|
|
// Sets up the express server instance.
|
2014-01-21 12:45:27 +04:00
|
|
|
// Instantiates the ghost singleton, helpers, routes, middleware, and apps.
|
2013-11-20 06:43:55 +04:00
|
|
|
// Finally it starts the http server.
|
2014-02-20 07:22:02 +04:00
|
|
|
function init(server) {
|
2013-12-31 03:13:25 +04:00
|
|
|
// create a hash for cache busting assets
|
2014-01-07 21:19:14 +04:00
|
|
|
var assetHash = (crypto.createHash('md5').update(packageInfo.version + Date.now()).digest('hex')).substring(0, 10);
|
2013-12-31 03:13:25 +04:00
|
|
|
|
2014-02-20 07:22:02 +04:00
|
|
|
// If no express instance is passed in
|
|
|
|
// then create our own
|
|
|
|
if (!server) {
|
|
|
|
server = express();
|
|
|
|
}
|
|
|
|
|
2013-11-28 06:45:01 +04:00
|
|
|
// Set up Polygot instance on the require module
|
|
|
|
Polyglot.instance = new Polyglot();
|
|
|
|
|
2013-12-06 18:13:15 +04:00
|
|
|
// ### Initialisation
|
2014-02-20 07:22:02 +04:00
|
|
|
// The server and its dependencies require a populated config
|
|
|
|
// It returns a promise that is resolved when the application
|
|
|
|
// has finished starting up.
|
2014-01-03 00:27:09 +04:00
|
|
|
|
2014-02-20 07:22:02 +04:00
|
|
|
// Make sure javascript files have been built via grunt concat
|
|
|
|
return builtFilesExist().then(function () {
|
|
|
|
// Initialise the models
|
|
|
|
return models.init();
|
|
|
|
}).then(function () {
|
2013-12-06 18:13:15 +04:00
|
|
|
// Populate any missing default settings
|
|
|
|
return models.Settings.populateDefaults();
|
|
|
|
}).then(function () {
|
|
|
|
// Initialize the settings cache
|
|
|
|
return api.init();
|
2014-05-07 04:49:25 +04:00
|
|
|
}).then(function () {
|
|
|
|
// Initialize the permissions actions and objects
|
|
|
|
// NOTE: Must be done before the config.theme.update and initDbHashAndFirstRun calls
|
|
|
|
return permissions.init();
|
2013-12-06 18:13:15 +04:00
|
|
|
}).then(function () {
|
2013-12-10 08:41:58 +04:00
|
|
|
// We must pass the api.settings object
|
2013-12-06 18:13:15 +04:00
|
|
|
// into this method due to circular dependencies.
|
2013-12-26 16:15:10 +04:00
|
|
|
return config.theme.update(api.settings, config().url);
|
2013-12-06 18:13:15 +04:00
|
|
|
}).then(function () {
|
|
|
|
return when.join(
|
|
|
|
// Check for or initialise a dbHash.
|
|
|
|
initDbHashAndFirstRun(),
|
2014-02-20 07:22:02 +04:00
|
|
|
// Initialize mail
|
|
|
|
mailer.init(),
|
|
|
|
// Initialize apps
|
|
|
|
apps.init()
|
2013-12-06 18:13:15 +04:00
|
|
|
);
|
2014-01-15 03:14:44 +04:00
|
|
|
}).then(function () {
|
2014-02-20 07:22:02 +04:00
|
|
|
var adminHbs = hbs.create(),
|
|
|
|
deferred = when.defer();
|
2013-09-17 04:40:59 +04:00
|
|
|
|
2013-11-20 06:43:55 +04:00
|
|
|
// ##Configuration
|
2013-09-17 04:40:59 +04:00
|
|
|
|
2013-11-20 06:43:55 +04:00
|
|
|
// return the correct mime type for woff filess
|
|
|
|
express['static'].mime.define({'application/font-woff': ['woff']});
|
2013-11-12 10:03:25 +04:00
|
|
|
|
2014-05-04 22:32:37 +04:00
|
|
|
// enabled gzip compression by default
|
|
|
|
if (config().server.compress !== false) {
|
|
|
|
server.use(compress());
|
|
|
|
}
|
|
|
|
|
2013-12-02 03:31:55 +04:00
|
|
|
// ## View engine
|
|
|
|
// set the view engine
|
|
|
|
server.set('view engine', 'hbs');
|
|
|
|
|
|
|
|
// Create a hbs instance for admin and init view engine
|
2014-01-05 10:40:53 +04:00
|
|
|
server.set('admin view engine', adminHbs.express3({partialsDir: config().paths.adminViews + 'partials'}));
|
2013-12-02 03:31:55 +04:00
|
|
|
|
|
|
|
// Load helpers
|
2013-12-31 03:13:25 +04:00
|
|
|
helpers.loadCoreHelpers(adminHbs, assetHash);
|
2013-12-02 03:31:55 +04:00
|
|
|
|
2013-11-20 06:43:55 +04:00
|
|
|
// ## Middleware
|
2013-12-06 18:13:15 +04:00
|
|
|
middleware(server, dbHash);
|
2013-11-12 10:03:25 +04:00
|
|
|
|
2013-11-20 06:43:55 +04:00
|
|
|
// ## Routing
|
2013-11-12 10:03:25 +04:00
|
|
|
|
2013-11-20 06:43:55 +04:00
|
|
|
// Set up API routes
|
|
|
|
routes.api(server);
|
2013-09-17 04:40:59 +04:00
|
|
|
|
2013-11-20 06:43:55 +04:00
|
|
|
// Set up Admin routes
|
|
|
|
routes.admin(server);
|
2013-09-06 19:54:50 +04:00
|
|
|
|
2013-11-20 06:43:55 +04:00
|
|
|
// Set up Frontend routes
|
|
|
|
routes.frontend(server);
|
|
|
|
|
2014-02-20 07:22:02 +04:00
|
|
|
// Log all theme errors and warnings
|
|
|
|
_.each(config().paths.availableThemes._messages.errors, function (error) {
|
2014-03-23 21:22:05 +04:00
|
|
|
errors.logError(error.message, error.context, error.help);
|
2014-02-20 07:22:02 +04:00
|
|
|
});
|
2013-09-15 01:34:12 +04:00
|
|
|
|
2014-02-20 07:22:02 +04:00
|
|
|
_.each(config().paths.availableThemes._messages.warns, function (warn) {
|
2014-03-23 21:22:05 +04:00
|
|
|
errors.logWarn(warn.message, warn.context, warn.help);
|
2014-02-20 07:22:02 +04:00
|
|
|
});
|
2013-11-20 06:43:55 +04:00
|
|
|
|
2014-02-20 07:22:02 +04:00
|
|
|
// ## Start Ghost App
|
|
|
|
if (config.getSocket()) {
|
|
|
|
// Make sure the socket is gone before trying to create another
|
|
|
|
fs.unlink(config.getSocket(), function (err) {
|
|
|
|
/*jshint unused:false*/
|
2014-03-25 16:42:52 +04:00
|
|
|
httpServer = server.listen(
|
|
|
|
config.getSocket()
|
2013-09-15 01:34:12 +04:00
|
|
|
);
|
2014-02-20 07:22:02 +04:00
|
|
|
fs.chmod(config.getSocket(), '0660');
|
2014-02-09 01:16:58 +04:00
|
|
|
});
|
2013-11-20 06:43:55 +04:00
|
|
|
|
2014-02-20 07:22:02 +04:00
|
|
|
} else {
|
2014-03-25 16:42:52 +04:00
|
|
|
httpServer = server.listen(
|
2014-02-20 07:22:02 +04:00
|
|
|
config().server.port,
|
2014-03-25 16:42:52 +04:00
|
|
|
config().server.host
|
2014-02-20 07:22:02 +04:00
|
|
|
);
|
|
|
|
}
|
2013-11-23 21:54:47 +04:00
|
|
|
|
2014-03-25 16:42:52 +04:00
|
|
|
httpServer.on('listening', function () {
|
|
|
|
ghostStartMessages();
|
|
|
|
deferred.resolve(httpServer);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2014-02-20 07:22:02 +04:00
|
|
|
return deferred.promise;
|
|
|
|
});
|
2013-11-23 21:54:47 +04:00
|
|
|
}
|
|
|
|
|
2013-11-17 22:40:26 +04:00
|
|
|
module.exports = init;
|