Ghost/core/server/index.js

117 lines
3.8 KiB
JavaScript
Raw Normal View History

// # 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'),
🎨 ⏱ Cleanup / optimise the server.init() function (#7985) refs #2182 * 🔥 Remove unused options from server init - this is left over from old code and is now unused * 🎨 Move knex-migrator check to db health - Move complex check function into own module - Call module from server/index.js - This just improves the readability of server/index.js * 🔥 Remove old comments - These comments all make no sense now! * 🎨 ⏱ Move model init out of promise chain - Model.init() does not return a promise - Therefore, we can move it to the top of the init function, outside of the promise change - This should be a minor optimisation, and again improves readability /clarity of what's happening * ✨ ⁉️ Move DBHash init / first run to Settings model - this structure is left over from when we had code we executed on the first run of Ghost - the implementation used the API to initialise one setting before populateDefaults is called - this had lots of dependencies - the whole model, API, and permissions structure had to be initialised for it to work - the new implementation is simpler, it captures the dbHash getting initialised during populateDefaults() - it also adds an event, so we can do first-run code later if we really want to (or maybe apps can?!) - perhaps this is hiding behaviour, and there's a nicer way to do it, but populateDefaults seems like a sane place to populate a default setting 😁 * ⏱ Optimise require order so config is first - the first require to config will cause the files to be read etc - this ensures that it happens early, and isn't confusingly timed as part of loading a different module * 🎨 Simplify settings model changes
2017-02-17 19:44:34 +03:00
// Config should be first require, as it triggers the initial load of the config files
config = require('./config'),
🎨 ⏱ Cleanup / optimise the server.init() function (#7985) refs #2182 * 🔥 Remove unused options from server init - this is left over from old code and is now unused * 🎨 Move knex-migrator check to db health - Move complex check function into own module - Call module from server/index.js - This just improves the readability of server/index.js * 🔥 Remove old comments - These comments all make no sense now! * 🎨 ⏱ Move model init out of promise chain - Model.init() does not return a promise - Therefore, we can move it to the top of the init function, outside of the promise change - This should be a minor optimisation, and again improves readability /clarity of what's happening * ✨ ⁉️ Move DBHash init / first run to Settings model - this structure is left over from when we had code we executed on the first run of Ghost - the implementation used the API to initialise one setting before populateDefaults is called - this had lots of dependencies - the whole model, API, and permissions structure had to be initialised for it to work - the new implementation is simpler, it captures the dbHash getting initialised during populateDefaults() - it also adds an event, so we can do first-run code later if we really want to (or maybe apps can?!) - perhaps this is hiding behaviour, and there's a nicer way to do it, but populateDefaults seems like a sane place to populate a default setting 😁 * ⏱ Optimise require order so config is first - the first require to config will cause the files to be read etc - this ensures that it happens early, and isn't confusingly timed as part of loading a different module * 🎨 Simplify settings model changes
2017-02-17 19:44:34 +03:00
Promise = require('bluebird'),
logging = require('./logging'),
i18n = require('./i18n'),
models = require('./models'),
permissions = require('./permissions'),
apps = require('./apps'),
auth = require('./auth'),
🎨 ⏱ Cleanup / optimise the server.init() function (#7985) refs #2182 * 🔥 Remove unused options from server init - this is left over from old code and is now unused * 🎨 Move knex-migrator check to db health - Move complex check function into own module - Call module from server/index.js - This just improves the readability of server/index.js * 🔥 Remove old comments - These comments all make no sense now! * 🎨 ⏱ Move model init out of promise chain - Model.init() does not return a promise - Therefore, we can move it to the top of the init function, outside of the promise change - This should be a minor optimisation, and again improves readability /clarity of what's happening * ✨ ⁉️ Move DBHash init / first run to Settings model - this structure is left over from when we had code we executed on the first run of Ghost - the implementation used the API to initialise one setting before populateDefaults is called - this had lots of dependencies - the whole model, API, and permissions structure had to be initialised for it to work - the new implementation is simpler, it captures the dbHash getting initialised during populateDefaults() - it also adds an event, so we can do first-run code later if we really want to (or maybe apps can?!) - perhaps this is hiding behaviour, and there's a nicer way to do it, but populateDefaults seems like a sane place to populate a default setting 😁 * ⏱ Optimise require order so config is first - the first require to config will cause the files to be read etc - this ensures that it happens early, and isn't confusingly timed as part of loading a different module * 🎨 Simplify settings model changes
2017-02-17 19:44:34 +03:00
dbHealth = require('./data/db/health'),
xmlrpc = require('./data/xml/xmlrpc'),
slack = require('./data/slack'),
GhostServer = require('./ghost-server'),
scheduling = require('./adapters/scheduling'),
settings = require('./settings'),
settingsCache = require('./settings/cache'),
themes = require('./themes'),
🎨 ⏱ Cleanup / optimise the server.init() function (#7985) refs #2182 * 🔥 Remove unused options from server init - this is left over from old code and is now unused * 🎨 Move knex-migrator check to db health - Move complex check function into own module - Call module from server/index.js - This just improves the readability of server/index.js * 🔥 Remove old comments - These comments all make no sense now! * 🎨 ⏱ Move model init out of promise chain - Model.init() does not return a promise - Therefore, we can move it to the top of the init function, outside of the promise change - This should be a minor optimisation, and again improves readability /clarity of what's happening * ✨ ⁉️ Move DBHash init / first run to Settings model - this structure is left over from when we had code we executed on the first run of Ghost - the implementation used the API to initialise one setting before populateDefaults is called - this had lots of dependencies - the whole model, API, and permissions structure had to be initialised for it to work - the new implementation is simpler, it captures the dbHash getting initialised during populateDefaults() - it also adds an event, so we can do first-run code later if we really want to (or maybe apps can?!) - perhaps this is hiding behaviour, and there's a nicer way to do it, but populateDefaults seems like a sane place to populate a default setting 😁 * ⏱ Optimise require order so config is first - the first require to config will cause the files to be read etc - this ensures that it happens early, and isn't confusingly timed as part of loading a different module * 🎨 Simplify settings model changes
2017-02-17 19:44:34 +03:00
utils = require('./utils');
// ## Initialise Ghost
🎨 ⏱ Cleanup / optimise the server.init() function (#7985) refs #2182 * 🔥 Remove unused options from server init - this is left over from old code and is now unused * 🎨 Move knex-migrator check to db health - Move complex check function into own module - Call module from server/index.js - This just improves the readability of server/index.js * 🔥 Remove old comments - These comments all make no sense now! * 🎨 ⏱ Move model init out of promise chain - Model.init() does not return a promise - Therefore, we can move it to the top of the init function, outside of the promise change - This should be a minor optimisation, and again improves readability /clarity of what's happening * ✨ ⁉️ Move DBHash init / first run to Settings model - this structure is left over from when we had code we executed on the first run of Ghost - the implementation used the API to initialise one setting before populateDefaults is called - this had lots of dependencies - the whole model, API, and permissions structure had to be initialised for it to work - the new implementation is simpler, it captures the dbHash getting initialised during populateDefaults() - it also adds an event, so we can do first-run code later if we really want to (or maybe apps can?!) - perhaps this is hiding behaviour, and there's a nicer way to do it, but populateDefaults seems like a sane place to populate a default setting 😁 * ⏱ Optimise require order so config is first - the first require to config will cause the files to be read etc - this ensures that it happens early, and isn't confusingly timed as part of loading a different module * 🎨 Simplify settings model changes
2017-02-17 19:44:34 +03:00
function init() {
debug('Init Start...');
var ghostServer, parentApp;
// Initialize Internationalization
i18n.init();
debug('I18n done');
🎨 ⏱ Cleanup / optimise the server.init() function (#7985) refs #2182 * 🔥 Remove unused options from server init - this is left over from old code and is now unused * 🎨 Move knex-migrator check to db health - Move complex check function into own module - Call module from server/index.js - This just improves the readability of server/index.js * 🔥 Remove old comments - These comments all make no sense now! * 🎨 ⏱ Move model init out of promise chain - Model.init() does not return a promise - Therefore, we can move it to the top of the init function, outside of the promise change - This should be a minor optimisation, and again improves readability /clarity of what's happening * ✨ ⁉️ Move DBHash init / first run to Settings model - this structure is left over from when we had code we executed on the first run of Ghost - the implementation used the API to initialise one setting before populateDefaults is called - this had lots of dependencies - the whole model, API, and permissions structure had to be initialised for it to work - the new implementation is simpler, it captures the dbHash getting initialised during populateDefaults() - it also adds an event, so we can do first-run code later if we really want to (or maybe apps can?!) - perhaps this is hiding behaviour, and there's a nicer way to do it, but populateDefaults seems like a sane place to populate a default setting 😁 * ⏱ Optimise require order so config is first - the first require to config will cause the files to be read etc - this ensures that it happens early, and isn't confusingly timed as part of loading a different module * 🎨 Simplify settings model changes
2017-02-17 19:44:34 +03:00
models.init();
debug('models done');
return dbHealth.check().then(function () {
🎨 ⏱ Cleanup / optimise the server.init() function (#7985) refs #2182 * 🔥 Remove unused options from server init - this is left over from old code and is now unused * 🎨 Move knex-migrator check to db health - Move complex check function into own module - Call module from server/index.js - This just improves the readability of server/index.js * 🔥 Remove old comments - These comments all make no sense now! * 🎨 ⏱ Move model init out of promise chain - Model.init() does not return a promise - Therefore, we can move it to the top of the init function, outside of the promise change - This should be a minor optimisation, and again improves readability /clarity of what's happening * ✨ ⁉️ Move DBHash init / first run to Settings model - this structure is left over from when we had code we executed on the first run of Ghost - the implementation used the API to initialise one setting before populateDefaults is called - this had lots of dependencies - the whole model, API, and permissions structure had to be initialised for it to work - the new implementation is simpler, it captures the dbHash getting initialised during populateDefaults() - it also adds an event, so we can do first-run code later if we really want to (or maybe apps can?!) - perhaps this is hiding behaviour, and there's a nicer way to do it, but populateDefaults seems like a sane place to populate a default setting 😁 * ⏱ Optimise require order so config is first - the first require to config will cause the files to be read etc - this ensures that it happens early, and isn't confusingly timed as part of loading a different module * 🎨 Simplify settings model changes
2017-02-17 19:44:34 +03:00
debug('DB health check done');
// Populate any missing default settings
🎨 ⏱ Cleanup / optimise the server.init() function (#7985) refs #2182 * 🔥 Remove unused options from server init - this is left over from old code and is now unused * 🎨 Move knex-migrator check to db health - Move complex check function into own module - Call module from server/index.js - This just improves the readability of server/index.js * 🔥 Remove old comments - These comments all make no sense now! * 🎨 ⏱ Move model init out of promise chain - Model.init() does not return a promise - Therefore, we can move it to the top of the init function, outside of the promise change - This should be a minor optimisation, and again improves readability /clarity of what's happening * ✨ ⁉️ Move DBHash init / first run to Settings model - this structure is left over from when we had code we executed on the first run of Ghost - the implementation used the API to initialise one setting before populateDefaults is called - this had lots of dependencies - the whole model, API, and permissions structure had to be initialised for it to work - the new implementation is simpler, it captures the dbHash getting initialised during populateDefaults() - it also adds an event, so we can do first-run code later if we really want to (or maybe apps can?!) - perhaps this is hiding behaviour, and there's a nicer way to do it, but populateDefaults seems like a sane place to populate a default setting 😁 * ⏱ Optimise require order so config is first - the first require to config will cause the files to be read etc - this ensures that it happens early, and isn't confusingly timed as part of loading a different module * 🎨 Simplify settings model changes
2017-02-17 19:44:34 +03:00
// Refresh the API settings cache
return settings.init();
}).then(function () {
debug('Update settings cache done');
// Initialize the permissions actions and objects
return permissions.init();
}).then(function () {
debug('Permissions done');
return Promise.join(
themes.init(),
// Initialize apps
apps.init(),
// Initialize xmrpc ping
xmlrpc.listen(),
// Initialize slack ping
slack.listen()
);
}).then(function () {
debug('Apps, XMLRPC, Slack done');
🎉 🎨 ✨ Remove middleware/index.js (#7548) 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
2016-10-13 18:24:09 +03:00
// Setup our collection of express apps
parentApp = require('./app')();
🎉 🎨 ✨ Remove middleware/index.js (#7548) 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
2016-10-13 18:24:09 +03:00
debug('Express Apps done');
}).then(function () {
return auth.validation.validate({
authType: config.get('auth:type')
});
}).then(function () {
// runs asynchronous
auth.init({
authType: config.get('auth:type'),
ghostAuthUrl: config.get('auth:url'),
redirectUri: utils.url.urlFor('admin', true),
clientUri: utils.url.urlFor('home', true),
clientName: settingsCache.get('title'),
clientDescription: settingsCache.get('description')
}).then(function (response) {
parentApp.use(response.auth);
}).catch(function onAuthError(err) {
logging.error(err);
});
}).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({
schedulerUrl: config.get('scheduling').schedulerUrl,
active: config.get('scheduling').active,
🔥✨ remove forceAdminSSL and urlSSL, add admin url (#7937) * 🔥 kill apiUrl helper, use urlFor helper instead More consistency of creating urls. Creates an easier ability to add config changes. Attention: urlFor function is getting a little nesty, BUT that is for now wanted to make easier and centralised changes to the configs. The url util need's refactoring anyway. * 🔥 urlSSL Remove all urlSSL usages. Add TODO's for the next commit to re-add logic for deleted logic. e.g. - cors helper generated an array of url's to allow requests from the defined config url's -> will be replaced by the admin url if available - theme handler prefered the urlSSL in case it was defined -> will be replaced by using the urlFor helper to get the blog url (based on the request secure flag) The changes in this commit doesn't have to be right, but it helped going step by step. The next commit is the more interesting one. * 🔥 ✨ remove forceAdminSSL, add new admin url and adapt logic I wanted to remove the forceAdminSSL as separate commit, but was hard to realise. That's why both changes are in one commit: 1. remove forceAdminSSL 2. add admin.url option - fix TODO's from last commits - rewrite the ssl middleware! - create some private helper functions in the url helper to realise the changes - rename some wordings and functions e.g. base === blog (we have so much different wordings) - i would like to do more, but this would end in a non readable PR - this commit contains the most important changes to offer admin.url option * 🤖 adapt tests IMPORTANT - all changes in the routing tests were needed, because each routing test did not start the ghost server - they just required the ghost application, which resulted in a random server port - having a random server port results in a redirect, caused by the ssl/redirect middleware * 😎 rename check-ssl middleware * 🎨 fix theme-handler because of master rebase
2017-02-03 21:13:22 +03:00
apiUrl: utils.url.urlFor('api', true),
internalPath: config.get('paths').internalSchedulingPath,
contentPath: config.getContentPath('scheduling')
});
}).then(function () {
debug('Scheduling done');
debug('...Init End');
return ghostServer;
});
}
module.exports = init;