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'),
|
2014-05-16 06:29:42 +04:00
|
|
|
migrations = require('./data/migration'),
|
2013-12-31 03:13:25 +04:00
|
|
|
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
|
|
|
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',
|
2014-07-17 18:33:21 +04:00
|
|
|
'<strong>' + config.url + '</strong>.',
|
2014-08-15 18:46:24 +04:00
|
|
|
'See <a href="http://support.ghost.org/" target="_blank">http://support.ghost.org</a> for instructions.'
|
2013-12-06 18:13:15 +04:00
|
|
|
];
|
|
|
|
|
2014-06-16 09:24:19 +04:00
|
|
|
return api.notifications.add({ notifications: [{
|
2013-12-06 18:13:15 +04:00
|
|
|
type: 'info',
|
2014-04-29 00:58:18 +04:00
|
|
|
message: firstRunMessage.join(' ')
|
2014-07-17 12:48:39 +04:00
|
|
|
}] }, {context: {internal: true}});
|
2013-12-06 18:13:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function initDbHashAndFirstRun() {
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
return api.settings.read({key: 'dbHash', context: {internal: true}}).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();
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
return api.settings.edit({settings: [{key: 'dbHash', value: initHash}]}, {context: {internal: true}})
|
|
|
|
.then(function (response) {
|
|
|
|
dbHash = response.settings[0].value;
|
|
|
|
return dbHash;
|
|
|
|
}).then(doFirstRun);
|
2013-12-16 14:16:06 +04:00
|
|
|
}
|
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-07-17 18:33:21 +04:00
|
|
|
location = config.paths.builtScriptPath,
|
2014-01-15 03:14:44 +04:00
|
|
|
|
2014-07-18 02:34:21 +04:00
|
|
|
fileNames = process.env.NODE_ENV === 'production' ?
|
|
|
|
helpers.scriptFiles.production : helpers.scriptFiles.development;
|
2014-01-15 03:14:44 +04:00
|
|
|
|
|
|
|
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",
|
2014-07-17 18:33:21 +04:00
|
|
|
config.url,
|
2014-03-25 16:42:52 +04:00
|
|
|
"\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
|
2014-06-07 19:05:33 +04:00
|
|
|
process.removeAllListeners('SIGINT').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-07-17 18:33:21 +04:00
|
|
|
config.getSocket() || config.server.host + ':' + config.server.port,
|
2014-03-25 16:42:52 +04:00
|
|
|
"\nUrl configured as:",
|
2014-07-17 18:33:21 +04:00
|
|
|
config.url,
|
2014-03-25 16:42:52 +04:00
|
|
|
"\nCtrl+C to shut down".grey
|
|
|
|
);
|
|
|
|
// ensure that Ghost exits correctly on Ctrl+C
|
2014-06-07 19:05:33 +04:00
|
|
|
process.removeAllListeners('SIGINT').on('SIGINT', function () {
|
2014-03-25 16:42:52 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-06-08 16:39:28 +04:00
|
|
|
|
|
|
|
// This is run after every initialization is done, right before starting server.
|
|
|
|
// Its main purpose is to move adding notifications here, so none of the submodules
|
|
|
|
// should need to include api, which previously resulted in circular dependencies.
|
|
|
|
// This is also a "one central repository" of adding startup notifications in case
|
|
|
|
// in the future apps will want to hook into here
|
|
|
|
function initNotifications() {
|
|
|
|
if (mailer.state && mailer.state.usingSendmail) {
|
2014-06-16 09:24:19 +04:00
|
|
|
api.notifications.add({ notifications: [{
|
2014-06-08 16:39:28 +04:00
|
|
|
type: 'info',
|
|
|
|
message: [
|
|
|
|
"Ghost is attempting to use your server's <b>sendmail</b> to send e-mail.",
|
|
|
|
"It is recommended that you explicitly configure an e-mail service,",
|
2014-08-15 18:46:24 +04:00
|
|
|
"See <a href=\"http://support.ghost.org/mail\" target=\"_blank\">http://support.ghost.org/mail</a> for instructions"
|
2014-06-08 16:39:28 +04:00
|
|
|
].join(' ')
|
2014-07-17 12:48:39 +04:00
|
|
|
}] }, {context: {internal: true}});
|
2014-06-08 16:39:28 +04:00
|
|
|
}
|
|
|
|
if (mailer.state && mailer.state.emailDisabled) {
|
2014-06-16 09:24:19 +04:00
|
|
|
api.notifications.add({ notifications: [{
|
2014-06-08 16:39:28 +04:00
|
|
|
type: 'warn',
|
|
|
|
message: [
|
|
|
|
"Ghost is currently unable to send e-mail.",
|
2014-08-15 18:20:52 +04:00
|
|
|
"See <a href=\"http://support.ghost.org/mail\" target=\"_blank\">http://support.ghost.org/mail</a> for instructions"
|
2014-06-08 16:39:28 +04:00
|
|
|
].join(' ')
|
2014-07-17 12:48:39 +04:00
|
|
|
}] }, {context: {internal: true}});
|
2014-06-08 16:39:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
2014-05-16 06:29:42 +04:00
|
|
|
}).then(function () {
|
|
|
|
// Initialize migrations
|
|
|
|
return migrations.init();
|
|
|
|
}).then(function () {
|
|
|
|
// Populate any missing default settings
|
|
|
|
return models.Settings.populateDefaults();
|
2013-12-06 18:13:15 +04:00
|
|
|
}).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.
|
2014-07-17 18:33:21 +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
|
|
|
|
2014-06-08 16:39:28 +04:00
|
|
|
// Output necessary notifications on init
|
|
|
|
initNotifications();
|
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
|
2014-07-17 18:33:21 +04:00
|
|
|
if (config.server.compress !== false) {
|
2014-05-04 22:32:37 +04:00
|
|
|
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-07-01 03:26:08 +04:00
|
|
|
server.set('admin view engine', adminHbs.express3({}));
|
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
|
|
|
|
2014-04-12 07:46:15 +04:00
|
|
|
// ## Middleware and Routing
|
2013-12-06 18:13:15 +04:00
|
|
|
middleware(server, dbHash);
|
2013-11-12 10:03:25 +04:00
|
|
|
|
2014-02-20 07:22:02 +04:00
|
|
|
// Log all theme errors and warnings
|
2014-07-17 18:33:21 +04:00
|
|
|
_.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-07-17 18:33:21 +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
|
2014-06-04 17:44:59 +04:00
|
|
|
try {
|
|
|
|
fs.unlinkSync(config.getSocket());
|
|
|
|
} catch (e) {
|
|
|
|
// We can ignore this.
|
|
|
|
}
|
|
|
|
|
|
|
|
httpServer = server.listen(
|
|
|
|
config.getSocket()
|
|
|
|
);
|
|
|
|
fs.chmod(config.getSocket(), '0660');
|
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-07-17 18:33:21 +04:00
|
|
|
config.server.port,
|
|
|
|
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;
|