2015-05-28 10:16:09 -05:00
|
|
|
// # Bootup
|
|
|
|
// This file needs serious love & refactoring
|
|
|
|
|
2016-09-14 09:50:17 -05:00
|
|
|
/**
|
|
|
|
* make sure overrides get's called first!
|
|
|
|
* - keeping the overrides require here works for installing Ghost as npm!
|
|
|
|
*
|
|
|
|
* the call order is the following:
|
|
|
|
* - root index requires core module
|
|
|
|
* - core index requires server
|
|
|
|
* - overrides is the first package to load
|
|
|
|
*/
|
|
|
|
require('./overrides');
|
|
|
|
|
2013-09-06 10:54:50 -05:00
|
|
|
// Module dependencies
|
2016-07-15 11:22:41 -05:00
|
|
|
var express = require('express'),
|
|
|
|
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 18:13:25 -05:00
|
|
|
permissions = require('./permissions'),
|
2016-07-15 11:22:41 -05:00
|
|
|
apps = require('./apps'),
|
|
|
|
xmlrpc = require('./data/xml/xmlrpc'),
|
|
|
|
slack = require('./data/slack'),
|
2014-09-19 11:17:58 -05:00
|
|
|
GhostServer = require('./ghost-server'),
|
2016-07-15 11:22:41 -05:00
|
|
|
scheduling = require('./scheduling'),
|
2015-10-06 08:36:56 -05:00
|
|
|
validateThemes = require('./utils/validate-themes'),
|
2016-09-09 05:18:09 -05:00
|
|
|
readDirectory = require('./utils/read-directory'),
|
2016-09-09 05:23:47 -05:00
|
|
|
utils = require('./utils'),
|
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 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;
|
2015-08-26 09:10:23 -05:00
|
|
|
// Use `then` here to do 'first run' actions
|
|
|
|
});
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-05-28 10:16:09 -05: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 11:36:46 -05:00
|
|
|
// Finally it returns an instance of GhostServer
|
2014-08-23 11:19:13 -05:00
|
|
|
function init(options) {
|
2016-07-15 11:22:41 -05:00
|
|
|
options = options || {};
|
|
|
|
|
2016-05-19 06:49:22 -05: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 15:27:09 -05:00
|
|
|
|
2015-11-12 07:29:45 -05:00
|
|
|
// Initialize Internationalization
|
|
|
|
i18n.init();
|
|
|
|
|
2016-09-13 14:38:16 -05:00
|
|
|
return readDirectory(config.get('paths').appPath).then(function loadThemes(result) {
|
|
|
|
config.set('paths:availableApps', result);
|
2016-09-09 05:10:43 -05:00
|
|
|
return api.themes.loadThemes();
|
2014-08-23 11:19:13 -05:00
|
|
|
}).then(function () {
|
2016-03-01 23:42:01 -05:00
|
|
|
models.init();
|
2014-05-15 21:29:42 -05:00
|
|
|
}).then(function () {
|
2016-07-15 11:22:41 -05:00
|
|
|
return versioning.getDatabaseVersion()
|
|
|
|
.then(function (currentVersion) {
|
2016-07-26 03:56:07 -05:00
|
|
|
var response = migrations.update.isDatabaseOutOfDate({
|
2016-07-15 11:22:41 -05:00
|
|
|
fromVersion: currentVersion,
|
|
|
|
toVersion: versioning.getNewestDatabaseVersion(),
|
|
|
|
forceMigration: process.env.FORCE_MIGRATION
|
2016-07-26 03:56:07 -05:00
|
|
|
}), maintenanceState;
|
|
|
|
|
|
|
|
if (response.migrate === true) {
|
2016-09-13 10:41:14 -05:00
|
|
|
maintenanceState = config.get('maintenance').enabled || false;
|
|
|
|
config.set('maintenance:enabled', true);
|
2016-07-26 03:56:07 -05:00
|
|
|
|
|
|
|
migrations.update.execute({
|
|
|
|
fromVersion: currentVersion,
|
|
|
|
toVersion: versioning.getNewestDatabaseVersion(),
|
|
|
|
forceMigration: process.env.FORCE_MIGRATION
|
|
|
|
}).then(function () {
|
2016-09-13 10:41:14 -05:00
|
|
|
config.set('maintenance:enabled', maintenanceState);
|
2016-07-26 03:56:07 -05:00
|
|
|
}).catch(function (err) {
|
2016-09-06 06:16:32 -05:00
|
|
|
if (!err) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-26 03:56:07 -05:00
|
|
|
errors.logErrorAndExit(err, err.context, err.help);
|
|
|
|
});
|
|
|
|
} else if (response.error) {
|
|
|
|
return Promise.reject(response.error);
|
|
|
|
}
|
2016-07-15 11:22:41 -05:00
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
if (err instanceof errors.DatabaseNotPopulated) {
|
|
|
|
return migrations.populate();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.reject(err);
|
|
|
|
});
|
2014-05-15 21:29:42 -05: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 22:15:15 -05: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 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 apps
|
2014-10-27 19:41:18 -05:00
|
|
|
apps.init(),
|
2015-03-24 15:23:23 -05:00
|
|
|
// Initialize xmrpc ping
|
2016-06-05 06:22:11 -05:00
|
|
|
xmlrpc.listen(),
|
2016-03-29 03:40:44 -05:00
|
|
|
// Initialize slack ping
|
2016-06-05 06:22:11 -05:00
|
|
|
slack.listen()
|
2013-12-06 09:13:15 -05:00
|
|
|
);
|
2014-01-14 18:14:44 -05:00
|
|
|
}).then(function () {
|
2016-05-14 13:02:45 -05:00
|
|
|
// Get reference to an express app instance.
|
|
|
|
var parentApp = express();
|
2013-12-01 18:31:55 -05:00
|
|
|
|
2014-04-11 22:46:15 -05:00
|
|
|
// ## Middleware and Routing
|
2016-05-14 13:02:45 -05: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
|
2016-09-13 10:41:14 -05:00
|
|
|
validateThemes(config.get('paths').themePath)
|
2015-10-06 08:36:56 -05:00
|
|
|
.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 08:36:56 -05: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 13:02:45 -05:00
|
|
|
return new GhostServer(parentApp);
|
2016-05-19 06:49:22 -05: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
|
2016-09-13 14:38:16 -05:00
|
|
|
return scheduling.init({
|
|
|
|
active: config.get('scheduling').active,
|
|
|
|
path: config.get('paths').schedulingPath,
|
|
|
|
apiUrl: utils.url.apiUrl()
|
|
|
|
});
|
2016-05-19 06:49:22 -05:00
|
|
|
}).then(function () {
|
|
|
|
return ghostServer;
|
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;
|