Cleaned comments in boot.js

- Cleaned up some of the comments
- Added proper method signatures where appropriate
- Split initDynamicRouting out from initServices, to make that clearer to read
This commit is contained in:
Hannah Wolfe 2021-06-28 10:04:59 +01:00
parent 218df4b5a4
commit 081d3b3a38
No known key found for this signature in database
GPG Key ID: 9F8C7532D0A6BA55

View File

@ -4,15 +4,18 @@
// - This file is FULL of debug statements so we can see timings for the various steps because the boot needs to be as fast as possible
// - As we manage to break the codebase down into distinct components for e.g. the frontend, their boot logic can be offloaded to them
// - app.js is separate as the first example of each component having it's own app.js file colocated with it, instead of inside of server/web
// IMPORTANT: The only global requires here should be overrides + debug so we can monitor timings with DEBUG=ghost:boot* node ghost
//
// IMPORTANT:
// ----------
// The only global requires here should be overrides + debug so we can monitor timings with DEBUG = ghost: boot * node ghost
require('./server/overrides');
const debug = require('@tryghost/debug')('boot');
// END OF GLOBAL REQUIRES
/**
* Helper class to create consistent log messages
*/class BootLogger {
*/
class BootLogger {
constructor(logging, startTime) {
this.logging = logging;
this.startTime = startTime;
@ -25,6 +28,7 @@ const debug = require('@tryghost/debug')('boot');
/**
* Helper function to handle sending server ready notifications
* @param {string} [error]
*/
function notifyServerReady(error) {
const notify = require('./server/notify');
@ -42,6 +46,10 @@ function notifyServerReady(error) {
* Get the Database into a ready state
* - DatabaseStateManager handles doing all this for us
* - Passing logging makes it output state messages
*
* @param {object} options
* @param {object} options.config
* @param {object} options.logging
*/
async function initDatabase({config, logging}) {
const DatabaseStateManager = require('./server/data/db/state-manager');
@ -52,6 +60,8 @@ async function initDatabase({config, logging}) {
/**
* Core is intended to be all the bits of Ghost that are fundamental and we can't do anything without them!
* (There's more to do to make this true)
* @param {object} options
* @param {object} options.ghostServer
*/
async function initCore({ghostServer}) {
debug('Begin: initCore');
@ -120,21 +130,13 @@ async function initExpressApps() {
}
/**
* Services are components that make up part of Ghost and need initialising on boot
* These services should all be part of core, frontend services should be loaded with the frontend
* We are working towards this being a service loader, with the ability to make certain services optional
* Dynamic routing is generated from the routes.yaml file, which is part of the settings service
* When Ghost's DB and core are loaded, we can access this file and call routing.bootstrap.start
* However this _must_ happen after the express Apps are loaded, hence why this is here and not in initFrontend
* Routing is currently tightly coupled between the frontend and backend
*/
async function initServices({config}) {
debug('Begin: initServices');
const defaultApiVersion = config.get('api:versions:default');
debug(`Default API Version: ${defaultApiVersion}`);
async function initDynamicRouting() {
debug('Begin: Dynamic Routing');
// Dynamic routing is generated from the routes.yaml file, which is part of the settings service
// When Ghost's DB and core are loaded, we can access this file and call routing.bootstrap.start
// However this _must_ happen after the express Apps are loaded, hence why this is here and not in initFrontend
// Routing is currently tightly coupled between the frontend and backend
const routing = require('./frontend/services/routing');
const bridge = require('./bridge');
// We pass the frontend API version here, so that the frontend services are slightly less tightly-coupled
@ -144,6 +146,21 @@ async function initServices({config}) {
const getRoutesHash = () => frontendSettings.getCurrentHash('routes');
await settings.syncRoutesHash(getRoutesHash);
debug('End: Dynamic Routing');
}
/**
* Services are components that make up part of Ghost and need initialising on boot
* These services should all be part of core, frontend services should be loaded with the frontend
* We are working towards this being a service loader, with the ability to make certain services optional
*
* @param {object} options
* @param {object} options.config
*/
async function initServices({config}) {
debug('Begin: initServices');
const defaultApiVersion = config.get('api:versions:default');
debug(`Default API Version: ${defaultApiVersion}`);
debug('Begin: Services');
const members = require('./server/services/members');
@ -187,7 +204,10 @@ async function initServices({config}) {
/**
* Kick off recurring jobs and background services
* These are things that happen on boot, but we don't need to wait for them to finish
* Later, this might be a service hook
* Later, this might be a service hook:q
* @param {object} options
* @param {object} options.config
*/
async function initBackgroundServices({config}) {
debug('Begin: initBackgroundServices');
@ -220,6 +240,8 @@ async function initBackgroundServices({config}) {
*
* - This function is written with async/await so you can read, line by line, what happens on boot
* - All the functions above handle init/boot logic for a single component
* @returns {Promise<object>} ghostServer
*/
async function bootGhost() {
// Metrics
@ -294,7 +316,8 @@ async function bootGhost() {
debug('Begin: Load Ghost Services & Apps');
await initCore({ghostServer});
await initFrontend();
const ghostApp = await initExpressApps({});
const ghostApp = await initExpressApps();
await initDynamicRouting();
await initServices({config});
debug('End: Load Ghost Services & Apps');