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'),
|
2014-02-05 12:40:30 +04:00
|
|
|
_ = require('lodash'),
|
2014-08-17 10:17:23 +04:00
|
|
|
Promise = require('bluebird'),
|
2013-12-31 03:13:25 +04:00
|
|
|
|
|
|
|
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'),
|
2014-08-19 20:36:46 +04:00
|
|
|
GhostServer = require('./GhostServer'),
|
2013-12-31 03:13:25 +04:00
|
|
|
|
2013-09-06 19:54:50 +04:00
|
|
|
// Variables
|
2013-12-06 18:13:15 +04:00
|
|
|
dbHash;
|
2013-09-06 19:54:50 +04:00
|
|
|
|
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) {
|
2014-08-17 10:17:23 +04:00
|
|
|
var errorMessage = "Javascript files have not been built.",
|
2014-01-15 03:14:44 +04:00
|
|
|
errorHelp = "\nPlease read the getting started instructions at:" +
|
|
|
|
"\nhttps://github.com/TryGhost/Ghost#getting-started-guide-for-developers";
|
|
|
|
|
2014-08-17 10:17:23 +04: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-15 03:14:44 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fileNames.forEach(function (fileName) {
|
|
|
|
deferreds.push(checkExist(location + fileName));
|
|
|
|
});
|
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.all(deferreds);
|
2014-01-15 03:14:44 +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.
|
2014-08-19 20:36:46 +04:00
|
|
|
// Finally it returns an instance of GhostServer
|
2014-08-23 20:19:13 +04:00
|
|
|
function init(options) {
|
|
|
|
// Get reference to an express app instance.
|
|
|
|
var server = options.app ? options.app : express(),
|
|
|
|
// create a hash for cache busting assets
|
|
|
|
assetHash = (crypto.createHash('md5').update(packageInfo.version + Date.now()).digest('hex')).substring(0, 10);
|
2014-02-20 07:22:02 +04:00
|
|
|
|
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-08-23 20:19:13 +04:00
|
|
|
// Load our config.js file from the local file system.
|
|
|
|
return config.load(options.config).then(function () {
|
|
|
|
// Make sure javascript files have been built via grunt concat
|
|
|
|
return builtFilesExist();
|
|
|
|
}).then(function () {
|
2014-02-20 07:22:02 +04:00
|
|
|
// 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 () {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.join(
|
2013-12-06 18:13:15 +04:00
|
|
|
// 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-08-19 20:36:46 +04:00
|
|
|
var adminHbs = hbs.create();
|
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-08-19 20:36:46 +04:00
|
|
|
return new GhostServer(server);
|
2014-02-20 07:22:02 +04:00
|
|
|
});
|
2013-11-23 21:54:47 +04:00
|
|
|
}
|
|
|
|
|
2013-11-17 22:40:26 +04:00
|
|
|
module.exports = init;
|