2015-05-28 16:16:09 +01:00
|
|
|
// # Bootup
|
|
|
|
// This file needs serious love & refactoring
|
|
|
|
|
2013-09-06 16:54:50 +01:00
|
|
|
// Module dependencies
|
2016-07-15 18:22:41 +02:00
|
|
|
var express = require('express'),
|
|
|
|
_ = require('lodash'),
|
|
|
|
uuid = require('node-uuid'),
|
|
|
|
Promise = require('bluebird'),
|
|
|
|
i18n = require('./i18n'),
|
|
|
|
api = require('./api'),
|
|
|
|
config = require('./config'),
|
|
|
|
errors = require('./errors'),
|
|
|
|
middleware = require('./middleware'),
|
|
|
|
migrations = require('./data/migration'),
|
|
|
|
versioning = require('./data/schema/versioning'),
|
|
|
|
models = require('./models'),
|
2013-12-30 23:13:25 +00:00
|
|
|
permissions = require('./permissions'),
|
2016-07-15 18:22:41 +02:00
|
|
|
apps = require('./apps'),
|
|
|
|
sitemap = require('./data/xml/sitemap'),
|
|
|
|
xmlrpc = require('./data/xml/xmlrpc'),
|
|
|
|
slack = require('./data/slack'),
|
2014-09-19 16:17:58 +00:00
|
|
|
GhostServer = require('./ghost-server'),
|
2016-07-15 18:22:41 +02:00
|
|
|
scheduling = require('./scheduling'),
|
2015-10-06 15:36:56 +02:00
|
|
|
validateThemes = require('./utils/validate-themes'),
|
2013-12-06 09:13:15 -05:00
|
|
|
dbHash;
|
2013-09-06 16:54:50 +01:00
|
|
|
|
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 13:41:19 +01: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 11:16:06 +01: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 13:41:19 +01:00
|
|
|
return api.settings.edit({settings: [{key: 'dbHash', value: initHash}]}, {context: {internal: true}})
|
|
|
|
.then(function (response) {
|
|
|
|
dbHash = response.settings[0].value;
|
|
|
|
return dbHash;
|
2015-08-26 15:10:23 +01:00
|
|
|
// Use `then` here to do 'first run' actions
|
|
|
|
});
|
2013-12-16 11:16:06 +01:00
|
|
|
}
|
2014-05-04 11:32:37 -07:00
|
|
|
|
2014-04-27 18:28:50 -05:00
|
|
|
return dbHash;
|
2013-12-06 09:13:15 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-05-28 16:16:09 +01:00
|
|
|
// ## Initialise Ghost
|
|
|
|
// Sets up the express server instances, runs init on a bunch of stuff, configures views, helpers, routes and more
|
2014-08-19 12:36:46 -04:00
|
|
|
// Finally it returns an instance of GhostServer
|
2014-08-23 12:19:13 -04:00
|
|
|
function init(options) {
|
2016-07-15 18:22:41 +02:00
|
|
|
options = options || {};
|
|
|
|
|
2016-05-19 13:49:22 +02:00
|
|
|
var ghostServer = null;
|
|
|
|
|
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 20:27:09 +00:00
|
|
|
|
2015-11-12 13:29:45 +01:00
|
|
|
// Initialize Internationalization
|
|
|
|
i18n.init();
|
|
|
|
|
2014-08-23 12:19:13 -04:00
|
|
|
// Load our config.js file from the local file system.
|
|
|
|
return config.load(options.config).then(function () {
|
2014-09-03 00:27:37 -04:00
|
|
|
return config.checkDeprecated();
|
2014-08-23 12:19:13 -04:00
|
|
|
}).then(function () {
|
2016-03-01 22:42:01 -06:00
|
|
|
models.init();
|
2014-05-15 22:29:42 -04:00
|
|
|
}).then(function () {
|
2016-07-15 18:22:41 +02:00
|
|
|
return versioning.getDatabaseVersion()
|
|
|
|
.then(function (currentVersion) {
|
2016-07-25 20:28:35 +01:00
|
|
|
var maintenanceState = config.maintenance.enabled || false;
|
2016-07-15 18:22:41 +02:00
|
|
|
config.maintenance.enabled = true;
|
|
|
|
|
|
|
|
migrations.update({
|
|
|
|
fromVersion: currentVersion,
|
|
|
|
toVersion: versioning.getNewestDatabaseVersion(),
|
|
|
|
forceMigration: process.env.FORCE_MIGRATION
|
|
|
|
}).then(function () {
|
2016-07-25 20:28:35 +01:00
|
|
|
config.maintenance.enabled = maintenanceState;
|
2016-07-15 18:22:41 +02:00
|
|
|
}).catch(function (err) {
|
|
|
|
errors.logErrorAndExit(err, err.context, err.help);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
if (err instanceof errors.DatabaseNotPopulated) {
|
|
|
|
return migrations.populate();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.reject(err);
|
|
|
|
});
|
2014-05-15 22:29:42 -04:00
|
|
|
}).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
|
2014-09-02 23:15:15 -04:00
|
|
|
// NOTE: Must be done before initDbHashAndFirstRun calls
|
2014-05-06 19:49:25 -05:00
|
|
|
return permissions.init();
|
2013-12-06 09:13:15 -05:00
|
|
|
}).then(function () {
|
2014-08-17 06:17:23 +00: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 apps
|
2014-10-27 19:41:18 -05:00
|
|
|
apps.init(),
|
|
|
|
// Initialize sitemaps
|
2015-03-24 22:23:23 +02:00
|
|
|
sitemap.init(),
|
|
|
|
// Initialize xmrpc ping
|
2016-06-05 12:22:11 +01:00
|
|
|
xmlrpc.listen(),
|
2016-03-29 10:40:44 +02:00
|
|
|
// Initialize slack ping
|
2016-06-05 12:22:11 +01:00
|
|
|
slack.listen()
|
2013-12-06 09:13:15 -05:00
|
|
|
);
|
2014-01-14 17:14:44 -06:00
|
|
|
}).then(function () {
|
2016-05-14 20:02:45 +02:00
|
|
|
// Get reference to an express app instance.
|
|
|
|
var parentApp = express();
|
2013-12-01 18:31:55 -05:00
|
|
|
|
2014-04-11 23:46:15 -04:00
|
|
|
// ## Middleware and Routing
|
2016-05-14 20:02:45 +02:00
|
|
|
middleware(parentApp);
|
2013-11-12 01:03:25 -05:00
|
|
|
|
2014-02-19 22:22:02 -05:00
|
|
|
// Log all theme errors and warnings
|
2015-10-06 15:36:56 +02:00
|
|
|
validateThemes(config.paths.themePath)
|
|
|
|
.catch(function (result) {
|
|
|
|
// TODO: change `result` to something better
|
|
|
|
result.errors.forEach(function (err) {
|
|
|
|
errors.logError(err.message, err.context, err.help);
|
|
|
|
});
|
2013-09-14 16:34:12 -05:00
|
|
|
|
2015-10-06 15:36:56 +02:00
|
|
|
result.warnings.forEach(function (warn) {
|
|
|
|
errors.logWarn(warn.message, warn.context, warn.help);
|
|
|
|
});
|
|
|
|
});
|
2013-11-19 21:43:55 -05:00
|
|
|
|
2016-05-14 20:02:45 +02:00
|
|
|
return new GhostServer(parentApp);
|
2016-05-19 13:49:22 +02:00
|
|
|
}).then(function (_ghostServer) {
|
|
|
|
ghostServer = _ghostServer;
|
|
|
|
|
|
|
|
// scheduling can trigger api requests, that's why we initialize the module after the ghost server creation
|
|
|
|
// scheduling module can create x schedulers with different adapters
|
|
|
|
return scheduling.init(_.extend(config.scheduling, {apiUrl: config.url + config.urlFor('api')}));
|
|
|
|
}).then(function () {
|
|
|
|
return ghostServer;
|
2014-02-19 22:22:02 -05:00
|
|
|
});
|
2013-11-23 17:54:47 +00:00
|
|
|
}
|
|
|
|
|
2013-11-17 18:40:26 +00:00
|
|
|
module.exports = init;
|