2015-05-28 18:16:09 +03:00
|
|
|
// # Bootup
|
|
|
|
// This file needs serious love & refactoring
|
|
|
|
|
2013-09-06 19:54:50 +04:00
|
|
|
// Module dependencies
|
2016-07-15 19:22:41 +03: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-31 03:13:25 +04:00
|
|
|
permissions = require('./permissions'),
|
2016-07-15 19:22:41 +03:00
|
|
|
apps = require('./apps'),
|
|
|
|
sitemap = require('./data/xml/sitemap'),
|
|
|
|
xmlrpc = require('./data/xml/xmlrpc'),
|
|
|
|
slack = require('./data/slack'),
|
2014-09-19 20:17:58 +04:00
|
|
|
GhostServer = require('./ghost-server'),
|
2016-07-15 19:22:41 +03:00
|
|
|
scheduling = require('./scheduling'),
|
2015-10-06 16:36:56 +03:00
|
|
|
validateThemes = require('./utils/validate-themes'),
|
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 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;
|
2015-08-26 17:10:23 +03:00
|
|
|
// Use `then` here to do 'first run' actions
|
|
|
|
});
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-05-28 18:16:09 +03: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 20:36:46 +04:00
|
|
|
// Finally it returns an instance of GhostServer
|
2014-08-23 20:19:13 +04:00
|
|
|
function init(options) {
|
2016-07-15 19:22:41 +03:00
|
|
|
options = options || {};
|
|
|
|
|
2016-05-19 14:49:22 +03:00
|
|
|
var ghostServer = null;
|
|
|
|
|
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
|
|
|
|
2015-11-12 15:29:45 +03:00
|
|
|
// Initialize Internationalization
|
|
|
|
i18n.init();
|
|
|
|
|
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 () {
|
2014-09-03 08:27:37 +04:00
|
|
|
return config.checkDeprecated();
|
2014-08-23 20:19:13 +04:00
|
|
|
}).then(function () {
|
2016-03-02 07:42:01 +03:00
|
|
|
models.init();
|
2014-05-16 06:29:42 +04:00
|
|
|
}).then(function () {
|
2016-07-15 19:22:41 +03:00
|
|
|
return versioning.getDatabaseVersion()
|
|
|
|
.then(function (currentVersion) {
|
2016-07-26 11:56:07 +03:00
|
|
|
var response = migrations.update.isDatabaseOutOfDate({
|
2016-07-15 19:22:41 +03:00
|
|
|
fromVersion: currentVersion,
|
|
|
|
toVersion: versioning.getNewestDatabaseVersion(),
|
|
|
|
forceMigration: process.env.FORCE_MIGRATION
|
2016-07-26 11:56:07 +03:00
|
|
|
}), maintenanceState;
|
|
|
|
|
|
|
|
if (response.migrate === true) {
|
|
|
|
maintenanceState = config.maintenance.enabled || false;
|
|
|
|
config.maintenance.enabled = true;
|
|
|
|
|
|
|
|
migrations.update.execute({
|
|
|
|
fromVersion: currentVersion,
|
|
|
|
toVersion: versioning.getNewestDatabaseVersion(),
|
|
|
|
forceMigration: process.env.FORCE_MIGRATION
|
|
|
|
}).then(function () {
|
|
|
|
config.maintenance.enabled = maintenanceState;
|
|
|
|
}).catch(function (err) {
|
|
|
|
errors.logErrorAndExit(err, err.context, err.help);
|
|
|
|
});
|
|
|
|
} else if (response.error) {
|
|
|
|
return Promise.reject(response.error);
|
|
|
|
}
|
2016-07-15 19:22:41 +03:00
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
if (err instanceof errors.DatabaseNotPopulated) {
|
|
|
|
return migrations.populate();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.reject(err);
|
|
|
|
});
|
2014-05-16 06:29:42 +04:00
|
|
|
}).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
|
2014-09-03 07:15:15 +04:00
|
|
|
// NOTE: Must be done before initDbHashAndFirstRun calls
|
2014-05-07 04:49:25 +04:00
|
|
|
return permissions.init();
|
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 apps
|
2014-10-28 03:41:18 +03:00
|
|
|
apps.init(),
|
|
|
|
// Initialize sitemaps
|
2015-03-24 23:23:23 +03:00
|
|
|
sitemap.init(),
|
|
|
|
// Initialize xmrpc ping
|
2016-06-05 14:22:11 +03:00
|
|
|
xmlrpc.listen(),
|
2016-03-29 11:40:44 +03:00
|
|
|
// Initialize slack ping
|
2016-06-05 14:22:11 +03:00
|
|
|
slack.listen()
|
2013-12-06 18:13:15 +04:00
|
|
|
);
|
2014-01-15 03:14:44 +04:00
|
|
|
}).then(function () {
|
2016-05-14 21:02:45 +03:00
|
|
|
// Get reference to an express app instance.
|
|
|
|
var parentApp = express();
|
2013-12-02 03:31:55 +04:00
|
|
|
|
2014-04-12 07:46:15 +04:00
|
|
|
// ## Middleware and Routing
|
2016-05-14 21:02:45 +03:00
|
|
|
middleware(parentApp);
|
2013-11-12 10:03:25 +04:00
|
|
|
|
2014-02-20 07:22:02 +04:00
|
|
|
// Log all theme errors and warnings
|
2015-10-06 16:36:56 +03: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-15 01:34:12 +04:00
|
|
|
|
2015-10-06 16:36:56 +03:00
|
|
|
result.warnings.forEach(function (warn) {
|
|
|
|
errors.logWarn(warn.message, warn.context, warn.help);
|
|
|
|
});
|
|
|
|
});
|
2013-11-20 06:43:55 +04:00
|
|
|
|
2016-05-14 21:02:45 +03:00
|
|
|
return new GhostServer(parentApp);
|
2016-05-19 14:49:22 +03: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-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;
|