2015-05-28 10:16:09 -05:00
|
|
|
// # Bootup
|
|
|
|
// This file needs serious love & refactoring
|
|
|
|
|
2013-09-06 10:54:50 -05:00
|
|
|
// Module dependencies
|
2014-10-10 09:54:07 -05:00
|
|
|
var express = require('express'),
|
2013-12-30 18:13:25 -05:00
|
|
|
uuid = require('node-uuid'),
|
2014-08-17 01:17:23 -05:00
|
|
|
Promise = require('bluebird'),
|
2015-07-17 23:07:42 -05:00
|
|
|
i18n = require('./i18n'),
|
2013-12-30 18:13:25 -05:00
|
|
|
api = require('./api'),
|
|
|
|
config = require('./config'),
|
2014-05-09 05:11:29 -05:00
|
|
|
errors = require('./errors'),
|
2013-12-30 18:13:25 -05:00
|
|
|
middleware = require('./middleware'),
|
2014-05-15 21:29:42 -05:00
|
|
|
migrations = require('./data/migration'),
|
2013-12-30 18:13:25 -05:00
|
|
|
models = require('./models'),
|
|
|
|
permissions = require('./permissions'),
|
2014-01-21 03:45:27 -05:00
|
|
|
apps = require('./apps'),
|
2015-03-24 15:23:23 -05:00
|
|
|
sitemap = require('./data/xml/sitemap'),
|
|
|
|
xmlrpc = require('./data/xml/xmlrpc'),
|
2016-03-29 03:40:44 -05:00
|
|
|
slack = require('./data/slack'),
|
2014-09-19 11:17:58 -05:00
|
|
|
GhostServer = require('./ghost-server'),
|
2015-10-06 08:36:56 -05:00
|
|
|
validateThemes = require('./utils/validate-themes'),
|
2013-12-30 18:13:25 -05:00
|
|
|
|
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) {
|
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();
|
|
|
|
|
2014-08-23 11:19:13 -05:00
|
|
|
// Load our config.js file from the local file system.
|
|
|
|
return config.load(options.config).then(function () {
|
2014-09-02 23:27:37 -05:00
|
|
|
return config.checkDeprecated();
|
2014-08-23 11:19:13 -05:00
|
|
|
}).then(function () {
|
2014-02-19 22:22:02 -05:00
|
|
|
// Initialise the models
|
2016-03-01 23:42:01 -05:00
|
|
|
models.init();
|
2014-05-15 21:29:42 -05:00
|
|
|
}).then(function () {
|
|
|
|
// Initialize migrations
|
|
|
|
return migrations.init();
|
|
|
|
}).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(),
|
|
|
|
// Initialize sitemaps
|
2015-03-24 15:23:23 -05:00
|
|
|
sitemap.init(),
|
|
|
|
// 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
|
2015-10-06 08:36:56 -05: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 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);
|
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;
|