mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-01 05:50:35 +03:00
4411f8254f
closes #4172, closes #6948, refs #7491, refs #7488, refs #7542, refs #7484 * 🎨 Co-locate all admin-related code in /admin - move all the admin related code from controllers, routes and helpers into a single location - add error handling middleware explicitly to adminApp - re-order blogApp middleware to ensure the shared middleware is mounted after the adminApp - TODO: rethink the structure of /admin, this should probably be an internal app * 💄 Group global middleware together - There are only a few pieces of middleware which are "global" - These are needed for the admin, blog and api - Everything else is only needed in one or two places * ✨ Introduce a separate blogApp - create a brand-new blogApp - mount all blog/theme only middleware etc onto blogApp - mount error handling on blogApp only * 🎨 Separate error handling for HTML & API JSON - split JSON and HTML error handling into separate functions - re-introduce a way to not output the stack for certain errors - add more tests around errors & an assertion framework for checking JSON Errors - TODO: better 404 handling for static assets Rationale: The API is very different to the blog/admin panel: - It is intended to only ever serve JSON, never HTML responses - It is intended to always serve JSON Meanwhile the blog and admin panel have no need for JSON errors, when an error happens on those pages, we should serve HTML pages which are nicely formatted with the error & using the correct template * 🐛 Fix checkSSL to work for subapps - in order to make this work on a sub app we need to use the pattern `req.originalUrl || req.url` * 🔥 Get rid of decide-is-admin (part 1/2) - delete decide-is-admin & tests - add two small functions to apiApp and adminApp to set res.isAdmin - mount checkSSL on all the apps - TODO: deduplicate the calls to checkSSL by making blogApp a subApp :D - PART 2/2: finish cleaning this up by removing it from where it's not needed and giving it a more specific name Rationale: Now that we have both an adminApp and an apiApp, we can temporarily replace this weird path-matching middleware with middleware that sets res.isAdmin for api & admin * 🎨 Wire up prettyURLs on all Apps - prettyURLs is needed for all requests - it cannot be global because it has to live after asset middleware, and before routing - this does not result in duplicate redirects, but does result in duplicate checks - TODO: resolve extra middleware in stack by making blogApp a sub app * ⏱ Add debug to API setup * 🎨 Rename blogApp -> parentApp in middleware * 🎨 Co-locate all blog-related code in /blog - Move all of the blogApp code from middleware/index.js to blog/app.js - Move routes/frontend.js to blog/routes.js - Remove the routes/index.js and routes folder, this is empty now! - @TODO is blog the best name for this? 🤔 - @TODO sort out the big hunk of asset-related mess - @TODO also separate out the concept of theme from blog * 🎉 Replace middleware index with server/app.js - The final piece of the puzzle! 🎉 🎈 🎂 - We no longer have our horrendous middleware/index.js - Instead, we have a set of app.js files, which all use a familiar pattern * 💄 Error handling fixups
142 lines
4.6 KiB
JavaScript
142 lines
4.6 KiB
JavaScript
// # Bootup
|
|
// This file needs serious love & refactoring
|
|
|
|
/**
|
|
* 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');
|
|
|
|
// Module dependencies
|
|
var debug = require('debug')('ghost:boot:init'),
|
|
uuid = require('node-uuid'),
|
|
Promise = require('bluebird'),
|
|
i18n = require('./i18n'),
|
|
api = require('./api'),
|
|
config = require('./config'),
|
|
db = require('./data/schema'),
|
|
models = require('./models'),
|
|
permissions = require('./permissions'),
|
|
apps = require('./apps'),
|
|
auth = require('./auth'),
|
|
xmlrpc = require('./data/xml/xmlrpc'),
|
|
slack = require('./data/slack'),
|
|
GhostServer = require('./ghost-server'),
|
|
scheduling = require('./scheduling'),
|
|
readDirectory = require('./utils/read-directory'),
|
|
utils = require('./utils'),
|
|
dbHash;
|
|
|
|
function initDbHashAndFirstRun() {
|
|
return api.settings.read({key: 'dbHash', context: {internal: true}}).then(function (response) {
|
|
var hash = response.settings[0].value,
|
|
initHash;
|
|
|
|
dbHash = hash;
|
|
|
|
if (dbHash === null) {
|
|
initHash = uuid.v4();
|
|
return api.settings.edit({settings: [{key: 'dbHash', value: initHash}]}, {context: {internal: true}})
|
|
.then(function (response) {
|
|
dbHash = response.settings[0].value;
|
|
return dbHash;
|
|
// Use `then` here to do 'first run' actions
|
|
});
|
|
}
|
|
|
|
return dbHash;
|
|
});
|
|
}
|
|
|
|
// ## Initialise Ghost
|
|
// Sets up the express server instances, runs init on a bunch of stuff, configures views, helpers, routes and more
|
|
// Finally it returns an instance of GhostServer
|
|
function init(options) {
|
|
debug('Init Start...');
|
|
options = options || {};
|
|
|
|
var ghostServer, parentApp;
|
|
|
|
// ### Initialisation
|
|
// The server and its dependencies require a populated config
|
|
// It returns a promise that is resolved when the application
|
|
// has finished starting up.
|
|
|
|
// Initialize Internationalization
|
|
i18n.init();
|
|
debug('I18n done');
|
|
|
|
return readDirectory(config.getContentPath('apps')).then(function loadThemes(result) {
|
|
config.set('paths:availableApps', result);
|
|
return api.themes.loadThemes();
|
|
}).then(function () {
|
|
debug('Themes & apps done');
|
|
|
|
models.init();
|
|
// @TODO: this is temporary, replace migrations with a warning if a DB exists
|
|
return db.bootUp();
|
|
}).then(function () {
|
|
// Populate any missing default settings
|
|
return models.Settings.populateDefaults();
|
|
}).then(function () {
|
|
debug('Models & database done');
|
|
// Initialize the settings cache
|
|
return api.init();
|
|
}).then(function () {
|
|
debug('API done');
|
|
// Initialize the permissions actions and objects
|
|
// NOTE: Must be done before initDbHashAndFirstRun calls
|
|
return permissions.init();
|
|
}).then(function () {
|
|
debug('Permissions done');
|
|
return Promise.join(
|
|
// Check for or initialise a dbHash.
|
|
initDbHashAndFirstRun(),
|
|
// Initialize apps
|
|
apps.init(),
|
|
// Initialize xmrpc ping
|
|
xmlrpc.listen(),
|
|
// Initialize slack ping
|
|
slack.listen()
|
|
);
|
|
}).then(function () {
|
|
debug('Apps, XMLRPC, Slack done');
|
|
|
|
// Setup our collection of express apps
|
|
parentApp = require('./app')();
|
|
|
|
debug('Express Apps done');
|
|
|
|
return auth.init(config.get('auth'))
|
|
.then(function (response) {
|
|
parentApp.use(response.auth);
|
|
});
|
|
}).then(function () {
|
|
debug('Auth done');
|
|
return new GhostServer(parentApp);
|
|
}).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
|
|
debug('Server done');
|
|
return scheduling.init({
|
|
active: config.get('scheduling').active,
|
|
apiUrl: utils.url.apiUrl(),
|
|
internalPath: config.get('paths').internalSchedulingPath,
|
|
contentPath: config.getContentPath('scheduling')
|
|
});
|
|
}).then(function () {
|
|
debug('Scheduling done');
|
|
debug('...Init End');
|
|
return ghostServer;
|
|
});
|
|
}
|
|
|
|
module.exports = init;
|