Skip to content

Commit

Permalink
Add v4.24.0
Browse files Browse the repository at this point in the history
  • Loading branch information
RadoslavGatev committed Nov 26, 2021
1 parent 86c40d6 commit 0aef52d
Show file tree
Hide file tree
Showing 50 changed files with 1,179 additions and 768 deletions.
13 changes: 12 additions & 1 deletion core/app.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
const sentry = require('./shared/sentry');
const express = require('./shared/express');
const config = require('./shared/config');
const urlService = require('./server/services/url');

const fs = require('fs');
const path = require('path');

const isMaintenanceModeEnabled = (req) => {
if (req.app.get('maintenance') || config.get('maintenance').enabled || !urlService.hasFinished()) {
return true;
}

return false;
};

// We never want middleware functions to be anonymous
const maintenanceMiddleware = (req, res, next) => {
if (!req.app.get('maintenance')) {
if (!isMaintenanceModeEnabled(req)) {
return next();
}

res.set({
'Cache-Control': 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'
});
Expand Down
48 changes: 31 additions & 17 deletions core/boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ async function initDatabase({config, logging}) {
* @param {object} options.ghostServer
* @param {object} options.config
* @param {object} options.bootLogger
* @param {boolean} options.frontend
*/
async function initCore({ghostServer, config, bootLogger}) {
async function initCore({ghostServer, config, bootLogger, frontend}) {
debug('Begin: initCore');

// URL Utils is a bit slow, put it here so the timing is visible separate from models
Expand All @@ -95,12 +96,13 @@ async function initCore({ghostServer, config, bootLogger}) {
debug('Begin: Url Service');
const urlService = require('./server/services/url');
// Note: there is no await here, we do not wait for the url service to finish
// We can return, but the site will remain in (the shared, not global) maintenance mode until this finishes
// This is managed on request: https://github.com/TryGhost/Ghost/blob/main/core/server/web/shared/middleware/maintenance.js#L13
// We can return, but the site will remain in maintenance mode until this finishes
// This is managed on request: https://github.com/TryGhost/Ghost/blob/main/core/app.js#L10
urlService.init({
onFinished: () => {
bootLogger.log('URL Service Ready');
}
},
urlCache: !frontend // hacky parameter to make the cache initialization kick in as we can't initialize labs before the boot
});
debug('End: Url Service');

Expand Down Expand Up @@ -129,6 +131,11 @@ async function initServicesForFrontend() {
await routeSettings.init();
debug('End: Routing Settings');

debug('Begin: Redirects');
const customRedirects = require('./server/services/redirects');
await customRedirects.init(),
debug('End: Redirects');

debug('Begin: Themes');
// customThemSettingsService.api must be initialized before any theme activation occurs
const customThemeSettingsService = require('./server/services/custom-theme-settings');
Expand All @@ -149,20 +156,20 @@ async function initFrontend() {
const helperService = require('./frontend/services/helpers');
await helperService.init();

const cardAssetService = require('./frontend/services/card-assets');
await cardAssetService.init();

debug('End: initFrontend');
}

/**
* At the moment we load our express apps all in one go, they require themselves and are co-located
* What we want is to be able to optionally load various components and mount them
* So eventually this function should go away
* @param {Object} options
* @param {Boolean} options.backend
* @param {Boolean} options.frontend
*/
async function initExpressApps() {
async function initExpressApps(options) {
debug('Begin: initExpressApps');
const parentApp = require('./server/web/parent/app')();
const parentApp = require('./server/web/parent/app')(options);
debug('End: initExpressApps');
return parentApp;
}
Expand All @@ -178,6 +185,7 @@ async function initDynamicRouting() {
const routing = require('./frontend/services/routing');
const routeSettingsService = require('./server/services/route-settings');
const bridge = require('./bridge');
bridge.init();

// We pass the frontend API version + the dynamic routes here, so that the frontend services are slightly less tightly-coupled
const apiVersion = bridge.getFrontendApiVersion();
Expand Down Expand Up @@ -219,7 +227,6 @@ async function initServices({config}) {
const appService = require('./frontend/services/apps');
const limits = require('./server/services/limits');
const scheduling = require('./server/adapters/scheduling');
const customRedirects = require('./server/services/redirects');

const urlUtils = require('./shared/url-utils');

Expand All @@ -233,7 +240,6 @@ async function initServices({config}) {
await offers.init();

await Promise.all([
customRedirects.init(),
members.init(),
permissions.init(),
xmlrpc.listen(),
Expand Down Expand Up @@ -271,7 +277,7 @@ async function initBackgroundServices({config}) {
themeService.loadInactiveThemes();

// we don't want to kick off background services that will interfere with tests
if (process.env.NODE_ENV.match(/^testing/)) {
if (process.env.NODE_ENV.startsWith('test')) {
return;
}

Expand All @@ -297,7 +303,7 @@ async function initBackgroundServices({config}) {
* @returns {Promise<object>} ghostServer
*/
async function bootGhost() {
async function bootGhost({backend = true, frontend = true} = {}) {
// Metrics
const startTime = Date.now();
debug('Begin Boot');
Expand Down Expand Up @@ -346,6 +352,7 @@ async function bootGhost() {
// Step 2 - Start server with minimal app in global maintenance mode
debug('Begin: load server + minimal app');
const rootApp = require('./app');

const GhostServer = require('./server/ghost-server');
ghostServer = new GhostServer({url: config.getSiteUrl()});
await ghostServer.start(rootApp);
Expand All @@ -360,11 +367,18 @@ async function bootGhost() {

// Step 4 - Load Ghost with all its services
debug('Begin: Load Ghost Services & Apps');
await initCore({ghostServer, config, bootLogger});
await initCore({ghostServer, config, bootLogger, frontend});
await initServicesForFrontend();
await initFrontend();
const ghostApp = await initExpressApps();
await initDynamicRouting();

if (frontend) {
await initFrontend();
}
const ghostApp = await initExpressApps({frontend, backend});

if (frontend) {
await initDynamicRouting();
}

await initServices({config});
debug('End: Load Ghost Services & Apps');

Expand Down
20 changes: 10 additions & 10 deletions core/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const config = require('./shared/config');
const logging = require('@tryghost/logging');
const tpl = require('@tryghost/tpl');
const themeEngine = require('./frontend/services/theme-engine');
const cardAssetService = require('./frontend/services/card-assets');
const routerManager = require('./frontend/services/routing').routerManager;
const settingsCache = require('./shared/settings-cache');

Expand All @@ -27,7 +28,7 @@ const messages = {
};

class Bridge {
constructor() {
init() {
/**
* When locale changes, we reload theme translations
* @deprecated: the term "lang" was deprecated in favor of "locale" publicly in 4.0
Expand All @@ -49,7 +50,7 @@ class Bridge {
return themeEngine.getActive();
}

activateTheme(loadedTheme, checkedTheme) {
async activateTheme(loadedTheme, checkedTheme) {
let settings = {
locale: settingsCache.get('lang')
};
Expand All @@ -67,8 +68,12 @@ class Bridge {

if (previousGhostAPI !== undefined && (previousGhostAPI !== currentGhostAPI)) {
events.emit('services.themes.api.changed');
this.reloadFrontend();
await this.reloadFrontend();
}

const cardAssetConfig = this.getCardAssetConfig();
debug('reload card assets config', cardAssetConfig);
await cardAssetService.load(cardAssetConfig);
} catch (err) {
logging.error(new errors.InternalServerError({
message: tpl(messages.activateFailed, {theme: loadedTheme.name}),
Expand All @@ -93,17 +98,12 @@ class Bridge {
}
}

reloadFrontend() {
async reloadFrontend() {
const apiVersion = this.getFrontendApiVersion();
const cardAssetConfig = this.getCardAssetConfig();

debug('reload card assets config', cardAssetConfig);
const cardAssetService = require('./frontend/services/card-assets');
cardAssetService.load(cardAssetConfig);

debug('reload frontend', apiVersion);
const siteApp = require('./frontend/web/site');
siteApp.reload({apiVersion});
await siteApp.reload({apiVersion});
}
}

Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Loading

0 comments on commit 0aef52d

Please sign in to comment.