2016-09-14 17:50:17 +03:00
|
|
|
/**
|
|
|
|
* make sure overrides get's called first!
|
2018-07-17 19:10:05 +03:00
|
|
|
* - keeping the overrides import here works for installing Ghost as npm!
|
2016-09-14 17:50:17 +03:00
|
|
|
*
|
|
|
|
* the call order is the following:
|
|
|
|
* - root index requires core module
|
|
|
|
* - core index requires server
|
|
|
|
* - overrides is the first package to load
|
|
|
|
*/
|
|
|
|
require('./overrides');
|
|
|
|
|
2018-07-17 19:10:05 +03:00
|
|
|
const debug = require('ghost-ignition').debug('boot:init');
|
|
|
|
const Promise = require('bluebird');
|
|
|
|
const config = require('./config');
|
|
|
|
const common = require('./lib/common');
|
|
|
|
const migrator = require('./data/db/migrator');
|
2019-06-18 16:13:55 +03:00
|
|
|
const urlUtils = require('./lib/url-utils');
|
2018-07-17 19:10:05 +03:00
|
|
|
let parentApp;
|
2015-11-12 15:29:45 +03:00
|
|
|
|
2018-07-17 19:10:05 +03:00
|
|
|
function initialiseServices() {
|
2018-08-06 18:18:59 +03:00
|
|
|
// CASE: When Ghost is ready with bootstrapping (db migrations etc.), we can trigger the router creation.
|
|
|
|
// Reason is that the routers access the routes.yaml, which shouldn't and doesn't have to be validated to
|
|
|
|
// start Ghost in maintenance mode.
|
2019-06-19 12:30:28 +03:00
|
|
|
const routing = require('../frontend/services/routing');
|
2018-08-06 18:18:59 +03:00
|
|
|
routing.bootstrap.start();
|
|
|
|
|
2018-07-17 19:10:05 +03:00
|
|
|
const permissions = require('./services/permissions'),
|
|
|
|
auth = require('./services/auth'),
|
|
|
|
apps = require('./services/apps'),
|
|
|
|
xmlrpc = require('./services/xmlrpc'),
|
|
|
|
slack = require('./services/slack'),
|
|
|
|
webhooks = require('./services/webhooks'),
|
|
|
|
scheduling = require('./adapters/scheduling');
|
2018-04-16 00:53:18 +03:00
|
|
|
|
2018-07-17 19:10:05 +03:00
|
|
|
debug('`initialiseServices` Start...');
|
2018-04-16 00:53:18 +03:00
|
|
|
|
2018-07-17 19:10:05 +03:00
|
|
|
return Promise.join(
|
2014-05-07 04:49:25 +04:00
|
|
|
// Initialize the permissions actions and objects
|
2018-07-17 19:10:05 +03:00
|
|
|
permissions.init(),
|
|
|
|
xmlrpc.listen(),
|
|
|
|
slack.listen(),
|
|
|
|
webhooks.listen(),
|
|
|
|
apps.init(),
|
|
|
|
scheduling.init({
|
|
|
|
schedulerUrl: config.get('scheduling').schedulerUrl,
|
|
|
|
active: config.get('scheduling').active,
|
2019-05-06 13:26:08 +03:00
|
|
|
// NOTE: When changing API version need to consider how to migrate custom scheduling adapters
|
|
|
|
// that rely on URL to lookup persisted scheduled records (jobs, etc.). Ref: https://github.com/TryGhost/Ghost/pull/10726#issuecomment-489557162
|
2019-06-18 16:13:55 +03:00
|
|
|
apiUrl: urlUtils.urlFor('api', {version: 'v0.1', versionType: 'content'}, true),
|
2018-07-17 19:10:05 +03:00
|
|
|
internalPath: config.get('paths').internalSchedulingPath,
|
|
|
|
contentPath: config.getContentPath('scheduling')
|
|
|
|
})
|
|
|
|
).then(function () {
|
|
|
|
debug('XMLRPC, Slack, Webhooks, Apps, Scheduling, Permissions done');
|
2016-10-06 15:27:35 +03:00
|
|
|
|
2017-08-22 14:23:23 +03:00
|
|
|
// Initialise analytics events
|
|
|
|
if (config.get('segment:key')) {
|
|
|
|
require('./analytics-events').init();
|
|
|
|
}
|
2017-03-02 22:50:58 +03:00
|
|
|
}).then(function () {
|
2017-09-18 15:01:58 +03:00
|
|
|
parentApp.use(auth.init());
|
2016-10-03 11:33:14 +03:00
|
|
|
debug('Auth done');
|
2017-09-18 15:01:58 +03:00
|
|
|
|
2018-07-17 19:10:05 +03:00
|
|
|
debug('...`initialiseServices` End');
|
2014-02-20 07:22:02 +04:00
|
|
|
});
|
2013-11-23 21:54:47 +04:00
|
|
|
}
|
|
|
|
|
2018-07-17 19:10:05 +03:00
|
|
|
/**
|
|
|
|
* - initialise models
|
|
|
|
* - initialise i18n
|
|
|
|
* - load all settings into settings cache (almost every component makes use of this cache)
|
|
|
|
* - load active theme
|
|
|
|
* - create our express apps (site, admin, api)
|
|
|
|
* - start the ghost server
|
|
|
|
* - enable maintenance mode if migrations are missing
|
|
|
|
*/
|
|
|
|
const minimalRequiredSetupToStartGhost = (dbState) => {
|
|
|
|
const settings = require('./services/settings');
|
|
|
|
const models = require('./models');
|
2019-06-25 19:33:56 +03:00
|
|
|
const frontendSettings = require('../frontend/services/settings');
|
2019-06-19 12:30:28 +03:00
|
|
|
const themes = require('../frontend/services/themes');
|
2018-07-17 19:10:05 +03:00
|
|
|
const GhostServer = require('./ghost-server');
|
|
|
|
|
|
|
|
let ghostServer;
|
|
|
|
|
|
|
|
// Initialize Ghost core internationalization
|
|
|
|
common.i18n.init();
|
|
|
|
debug('Default i18n done for core');
|
|
|
|
|
|
|
|
models.init();
|
|
|
|
debug('Models done');
|
|
|
|
|
|
|
|
return settings.init()
|
|
|
|
.then(() => {
|
|
|
|
debug('Settings done');
|
2019-06-25 19:33:56 +03:00
|
|
|
|
|
|
|
return frontendSettings.init();
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
debug('Frontend settings done');
|
2018-07-17 19:10:05 +03:00
|
|
|
return themes.init();
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
debug('Themes done');
|
|
|
|
|
|
|
|
parentApp = require('./web/parent-app')();
|
|
|
|
debug('Express Apps done');
|
|
|
|
|
|
|
|
return new GhostServer(parentApp);
|
|
|
|
})
|
|
|
|
.then((_ghostServer) => {
|
|
|
|
ghostServer = _ghostServer;
|
|
|
|
|
|
|
|
// CASE: all good or db was just initialised
|
|
|
|
if (dbState === 1 || dbState === 2) {
|
|
|
|
common.events.emit('db.ready');
|
|
|
|
|
|
|
|
return initialiseServices()
|
|
|
|
.then(() => {
|
|
|
|
return ghostServer;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// CASE: migrations required, put blog into maintenance mode
|
|
|
|
if (dbState === 4) {
|
|
|
|
common.logging.info('Blog is in maintenance mode.');
|
|
|
|
|
|
|
|
config.set('maintenance:enabled', true);
|
2018-07-27 16:19:39 +03:00
|
|
|
|
2018-07-17 19:10:05 +03:00
|
|
|
migrator.migrate()
|
|
|
|
.then(() => {
|
|
|
|
common.events.emit('db.ready');
|
|
|
|
return initialiseServices();
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
config.set('maintenance:enabled', false);
|
|
|
|
common.logging.info('Blog is out of maintenance mode.');
|
2018-07-27 16:19:39 +03:00
|
|
|
return GhostServer.announceServerStart();
|
2018-07-17 19:10:05 +03:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
2018-07-27 16:19:39 +03:00
|
|
|
return GhostServer.announceServerStopped(err)
|
|
|
|
.finally(() => {
|
|
|
|
common.logging.error(err);
|
2018-08-14 14:55:49 +03:00
|
|
|
setTimeout(() => {
|
|
|
|
process.exit(-1);
|
|
|
|
}, 100);
|
2018-07-17 19:10:05 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return ghostServer;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect to database.
|
|
|
|
* Check db state.
|
|
|
|
*/
|
|
|
|
const isDatabaseInitialisationRequired = () => {
|
|
|
|
const db = require('./data/db/connection');
|
|
|
|
let dbState;
|
|
|
|
|
|
|
|
return migrator.getState()
|
|
|
|
.then((state) => {
|
|
|
|
dbState = state;
|
|
|
|
|
|
|
|
// CASE: db initialisation required, wait till finished
|
|
|
|
if (dbState === 2) {
|
|
|
|
return migrator.dbInit();
|
|
|
|
}
|
|
|
|
|
|
|
|
// CASE: is db incompatible? e.g. you can't connect a 0.11 database with Ghost 1.0 or 2.0
|
|
|
|
if (dbState === 3) {
|
|
|
|
return migrator.isDbCompatible(db)
|
|
|
|
.then(() => {
|
|
|
|
dbState = 2;
|
|
|
|
return migrator.dbInit();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
return minimalRequiredSetupToStartGhost(dbState);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = isDatabaseInitialisationRequired;
|