2013-09-06 10:54:50 -05:00
|
|
|
// Module dependencies
|
2013-12-30 18:13:25 -05:00
|
|
|
var crypto = require('crypto'),
|
|
|
|
express = require('express'),
|
|
|
|
hbs = require('express-hbs'),
|
2014-05-04 13:32:37 -05:00
|
|
|
compress = require('compression'),
|
2013-12-30 18:13:25 -05:00
|
|
|
fs = require('fs'),
|
|
|
|
uuid = require('node-uuid'),
|
2014-02-05 03:40:30 -05:00
|
|
|
_ = require('lodash'),
|
2014-08-17 01:17:23 -05:00
|
|
|
Promise = require('bluebird'),
|
2013-12-30 18:13:25 -05:00
|
|
|
|
|
|
|
api = require('./api'),
|
|
|
|
config = require('./config'),
|
2014-05-09 05:11:29 -05:00
|
|
|
errors = require('./errors'),
|
2013-12-30 18:13:25 -05:00
|
|
|
helpers = require('./helpers'),
|
|
|
|
mailer = require('./mail'),
|
|
|
|
middleware = require('./middleware'),
|
2014-05-15 21:29:42 -05:00
|
|
|
migrations = require('./data/migration'),
|
2013-12-30 18:13:25 -05:00
|
|
|
models = require('./models'),
|
|
|
|
permissions = require('./permissions'),
|
2014-01-21 03:45:27 -05:00
|
|
|
apps = require('./apps'),
|
2013-12-30 18:13:25 -05:00
|
|
|
packageInfo = require('../../package.json'),
|
2014-08-19 11:36:46 -05:00
|
|
|
GhostServer = require('./GhostServer'),
|
2013-12-30 18:13:25 -05:00
|
|
|
|
2013-09-06 10:54:50 -05:00
|
|
|
// Variables
|
2013-12-06 09:13:15 -05:00
|
|
|
dbHash;
|
2013-09-06 10:54:50 -05:00
|
|
|
|
2013-12-06 09:13:15 -05: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 09:33:21 -05:00
|
|
|
'<strong>' + config.url + '</strong>.',
|
2014-08-15 09:46:24 -05:00
|
|
|
'See <a href="http://support.ghost.org/" target="_blank">http://support.ghost.org</a> for instructions.'
|
2013-12-06 09:13:15 -05:00
|
|
|
];
|
|
|
|
|
2014-06-16 00:24:19 -05:00
|
|
|
return api.notifications.add({ notifications: [{
|
2013-12-06 09:13:15 -05:00
|
|
|
type: 'info',
|
2014-04-28 15:58:18 -05:00
|
|
|
message: firstRunMessage.join(' ')
|
2014-07-17 03:48:39 -05:00
|
|
|
}] }, {context: {internal: true}});
|
2013-12-06 09:13:15 -05: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 07:41:19 -05:00
|
|
|
return api.settings.read({key: 'dbHash', context: {internal: true}}).then(function (response) {
|
2014-04-27 18:28:50 -05:00
|
|
|
var hash = response.settings[0].value,
|
|
|
|
initHash;
|
|
|
|
|
|
|
|
dbHash = hash;
|
2013-12-16 05:16:06 -05:00
|
|
|
|
|
|
|
if (dbHash === null) {
|
2014-04-27 18:28:50 -05: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 07:41:19 -05: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 05:16:06 -05:00
|
|
|
}
|
2014-05-04 13:32:37 -05:00
|
|
|
|
2014-04-27 18:28:50 -05:00
|
|
|
return dbHash;
|
2013-12-06 09:13:15 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-01-14 18:14:44 -05:00
|
|
|
// Checks for the existence of the "built" javascript files from grunt concat.
|
2014-02-08 16:16:58 -05:00
|
|
|
// Returns a promise that will be resolved if all files exist or rejected if
|
2014-01-14 18:14:44 -05:00
|
|
|
// any are missing.
|
|
|
|
function builtFilesExist() {
|
|
|
|
var deferreds = [],
|
2014-07-17 09:33:21 -05:00
|
|
|
location = config.paths.builtScriptPath,
|
2014-01-14 18:14:44 -05:00
|
|
|
|
2014-07-17 17:34:21 -05:00
|
|
|
fileNames = process.env.NODE_ENV === 'production' ?
|
|
|
|
helpers.scriptFiles.production : helpers.scriptFiles.development;
|
2014-01-14 18:14:44 -05:00
|
|
|
|
|
|
|
function checkExist(fileName) {
|
2014-08-17 01:17:23 -05:00
|
|
|
var errorMessage = "Javascript files have not been built.",
|
2014-01-14 18:14:44 -05:00
|
|
|
errorHelp = "\nPlease read the getting started instructions at:" +
|
|
|
|
"\nhttps://github.com/TryGhost/Ghost#getting-started-guide-for-developers";
|
|
|
|
|
2014-08-17 01:17:23 -05:00
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
fs.exists(fileName, function (exists) {
|
|
|
|
if (exists) {
|
|
|
|
resolve(true);
|
|
|
|
} else {
|
|
|
|
var err = new Error(errorMessage);
|
|
|
|
|
|
|
|
err.help = errorHelp;
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
});
|
2014-01-14 18:14:44 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fileNames.forEach(function (fileName) {
|
|
|
|
deferreds.push(checkExist(location + fileName));
|
|
|
|
});
|
|
|
|
|
2014-08-17 01:17:23 -05:00
|
|
|
return Promise.all(deferreds);
|
2014-01-14 18:14:44 -05:00
|
|
|
}
|
|
|
|
|
2014-06-08 07:39:28 -05: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 00:24:19 -05:00
|
|
|
api.notifications.add({ notifications: [{
|
2014-06-08 07:39:28 -05: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 09:46:24 -05:00
|
|
|
"See <a href=\"http://support.ghost.org/mail\" target=\"_blank\">http://support.ghost.org/mail</a> for instructions"
|
2014-06-08 07:39:28 -05:00
|
|
|
].join(' ')
|
2014-07-17 03:48:39 -05:00
|
|
|
}] }, {context: {internal: true}});
|
2014-06-08 07:39:28 -05:00
|
|
|
}
|
|
|
|
if (mailer.state && mailer.state.emailDisabled) {
|
2014-06-16 00:24:19 -05:00
|
|
|
api.notifications.add({ notifications: [{
|
2014-06-08 07:39:28 -05:00
|
|
|
type: 'warn',
|
|
|
|
message: [
|
|
|
|
"Ghost is currently unable to send e-mail.",
|
2014-08-15 09:20:52 -05:00
|
|
|
"See <a href=\"http://support.ghost.org/mail\" target=\"_blank\">http://support.ghost.org/mail</a> for instructions"
|
2014-06-08 07:39:28 -05:00
|
|
|
].join(' ')
|
2014-07-17 03:48:39 -05:00
|
|
|
}] }, {context: {internal: true}});
|
2014-06-08 07:39:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-19 22:22:02 -05:00
|
|
|
// ## Initializes the ghost application.
|
2013-11-19 21:43:55 -05:00
|
|
|
// Sets up the express server instance.
|
2014-01-21 03:45:27 -05:00
|
|
|
// Instantiates the ghost singleton, helpers, routes, middleware, and apps.
|
2014-08-19 11:36:46 -05:00
|
|
|
// Finally it returns an instance of GhostServer
|
2014-02-19 22:22:02 -05:00
|
|
|
function init(server) {
|
2013-12-30 18:13:25 -05:00
|
|
|
// create a hash for cache busting assets
|
2014-01-07 12:19:14 -05:00
|
|
|
var assetHash = (crypto.createHash('md5').update(packageInfo.version + Date.now()).digest('hex')).substring(0, 10);
|
2013-12-30 18:13:25 -05:00
|
|
|
|
2014-02-19 22:22:02 -05:00
|
|
|
// If no express instance is passed in
|
|
|
|
// then create our own
|
|
|
|
if (!server) {
|
|
|
|
server = express();
|
|
|
|
}
|
|
|
|
|
2013-12-06 09:13:15 -05:00
|
|
|
// ### Initialisation
|
2014-02-19 22:22:02 -05: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-02 15:27:09 -05:00
|
|
|
|
2014-02-19 22:22:02 -05:00
|
|
|
// Make sure javascript files have been built via grunt concat
|
|
|
|
return builtFilesExist().then(function () {
|
|
|
|
// Initialise the models
|
|
|
|
return models.init();
|
2014-05-15 21:29:42 -05:00
|
|
|
}).then(function () {
|
|
|
|
// Initialize migrations
|
|
|
|
return migrations.init();
|
|
|
|
}).then(function () {
|
|
|
|
// Populate any missing default settings
|
|
|
|
return models.Settings.populateDefaults();
|
2013-12-06 09:13:15 -05:00
|
|
|
}).then(function () {
|
|
|
|
// Initialize the settings cache
|
|
|
|
return api.init();
|
2014-05-06 19:49:25 -05: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 09:13:15 -05:00
|
|
|
}).then(function () {
|
2013-12-09 23:41:58 -05:00
|
|
|
// We must pass the api.settings object
|
2013-12-06 09:13:15 -05:00
|
|
|
// into this method due to circular dependencies.
|
2014-07-17 09:33:21 -05:00
|
|
|
return config.theme.update(api.settings, config.url);
|
2013-12-06 09:13:15 -05:00
|
|
|
}).then(function () {
|
2014-08-17 01:17:23 -05:00
|
|
|
return Promise.join(
|
2013-12-06 09:13:15 -05:00
|
|
|
// Check for or initialise a dbHash.
|
|
|
|
initDbHashAndFirstRun(),
|
2014-02-19 22:22:02 -05:00
|
|
|
// Initialize mail
|
|
|
|
mailer.init(),
|
|
|
|
// Initialize apps
|
|
|
|
apps.init()
|
2013-12-06 09:13:15 -05:00
|
|
|
);
|
2014-01-14 18:14:44 -05:00
|
|
|
}).then(function () {
|
2014-08-19 11:36:46 -05:00
|
|
|
var adminHbs = hbs.create();
|
2013-09-16 19:40:59 -05:00
|
|
|
|
2014-06-08 07:39:28 -05:00
|
|
|
// Output necessary notifications on init
|
|
|
|
initNotifications();
|
2013-11-19 21:43:55 -05:00
|
|
|
// ##Configuration
|
2013-09-16 19:40:59 -05:00
|
|
|
|
2013-11-19 21:43:55 -05:00
|
|
|
// return the correct mime type for woff filess
|
|
|
|
express['static'].mime.define({'application/font-woff': ['woff']});
|
2013-11-12 01:03:25 -05:00
|
|
|
|
2014-05-04 13:32:37 -05:00
|
|
|
// enabled gzip compression by default
|
2014-07-17 09:33:21 -05:00
|
|
|
if (config.server.compress !== false) {
|
2014-05-04 13:32:37 -05:00
|
|
|
server.use(compress());
|
|
|
|
}
|
|
|
|
|
2013-12-01 18:31:55 -05:00
|
|
|
// ## View engine
|
|
|
|
// set the view engine
|
|
|
|
server.set('view engine', 'hbs');
|
|
|
|
|
|
|
|
// Create a hbs instance for admin and init view engine
|
2014-06-30 18:26:08 -05:00
|
|
|
server.set('admin view engine', adminHbs.express3({}));
|
2013-12-01 18:31:55 -05:00
|
|
|
|
|
|
|
// Load helpers
|
2013-12-30 18:13:25 -05:00
|
|
|
helpers.loadCoreHelpers(adminHbs, assetHash);
|
2013-12-01 18:31:55 -05:00
|
|
|
|
2014-04-11 22:46:15 -05:00
|
|
|
// ## Middleware and Routing
|
2013-12-06 09:13:15 -05:00
|
|
|
middleware(server, dbHash);
|
2013-11-12 01:03:25 -05:00
|
|
|
|
2014-02-19 22:22:02 -05:00
|
|
|
// Log all theme errors and warnings
|
2014-07-17 09:33:21 -05:00
|
|
|
_.each(config.paths.availableThemes._messages.errors, function (error) {
|
2014-03-23 12:22:05 -05:00
|
|
|
errors.logError(error.message, error.context, error.help);
|
2014-02-19 22:22:02 -05:00
|
|
|
});
|
2013-09-14 16:34:12 -05:00
|
|
|
|
2014-07-17 09:33:21 -05:00
|
|
|
_.each(config.paths.availableThemes._messages.warns, function (warn) {
|
2014-03-23 12:22:05 -05:00
|
|
|
errors.logWarn(warn.message, warn.context, warn.help);
|
2014-02-19 22:22:02 -05:00
|
|
|
});
|
2013-11-19 21:43:55 -05:00
|
|
|
|
2014-08-19 11:36:46 -05:00
|
|
|
return new GhostServer(server);
|
2014-02-19 22:22:02 -05:00
|
|
|
});
|
2013-11-23 12:54:47 -05:00
|
|
|
}
|
|
|
|
|
2013-11-17 13:40:26 -05:00
|
|
|
module.exports = init;
|