2015-05-28 18:16:09 +03:00
|
|
|
// # Bootup
|
|
|
|
// This file needs serious love & refactoring
|
|
|
|
|
2016-09-14 17:50:17 +03: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 19:54:50 +04:00
|
|
|
// Module dependencies
|
2016-07-15 19:22:41 +03: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'),
|
2016-09-20 16:44:07 +03:00
|
|
|
db = require('./data/schema'),
|
2016-07-15 19:22:41 +03:00
|
|
|
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'),
|
2016-09-30 14:45:59 +03:00
|
|
|
auth = require('./auth'),
|
2016-07-15 19:22:41 +03:00
|
|
|
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'),
|
2016-09-09 13:18:09 +03:00
|
|
|
readDirectory = require('./utils/read-directory'),
|
2016-09-09 13:23:47 +03:00
|
|
|
utils = require('./utils'),
|
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-09-30 14:45:59 +03:00
|
|
|
var ghostServer, parentApp;
|
2016-05-19 14:49:22 +03: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
|
|
|
|
2015-11-12 15:29:45 +03:00
|
|
|
// Initialize Internationalization
|
|
|
|
i18n.init();
|
|
|
|
|
2016-09-13 23:24:57 +03:00
|
|
|
return readDirectory(config.getContentPath('apps')).then(function loadThemes(result) {
|
2016-09-13 22:38:16 +03:00
|
|
|
config.set('paths:availableApps', result);
|
2016-09-09 13:10:43 +03:00
|
|
|
return api.themes.loadThemes();
|
2014-08-23 20:19:13 +04:00
|
|
|
}).then(function () {
|
2016-03-02 07:42:01 +03:00
|
|
|
models.init();
|
2016-09-20 16:44:07 +03:00
|
|
|
// @TODO: this is temporary, replace migrations with a warning if a DB exists
|
|
|
|
return db.bootUp();
|
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(),
|
2015-03-24 23:23:23 +03:00
|
|
|
// 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.
|
2016-09-30 14:45:59 +03:00
|
|
|
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
|
2016-09-13 23:24:57 +03:00
|
|
|
validateThemes(config.getContentPath('themes'))
|
2015-10-06 16:36:56 +03: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-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-09-30 14:45:59 +03:00
|
|
|
return auth.init(config.get('auth'))
|
|
|
|
.then(function (response) {
|
|
|
|
parentApp.use(response.auth);
|
|
|
|
});
|
|
|
|
}).then(function () {
|
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
|
2016-09-13 22:38:16 +03:00
|
|
|
return scheduling.init({
|
|
|
|
active: config.get('scheduling').active,
|
2016-09-13 23:24:57 +03:00
|
|
|
apiUrl: utils.url.apiUrl(),
|
|
|
|
internalPath: config.get('paths').internalSchedulingPath,
|
|
|
|
contentPath: config.getContentPath('scheduling')
|
2016-09-13 22:38:16 +03:00
|
|
|
});
|
2016-05-19 14:49:22 +03:00
|
|
|
}).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;
|