Ghost/core/app.js
Hannah Wolfe f417c4c732
Merged our two maintenance middleware into one
- Reduced our maintenance middleware code down to the bare minimum!
  - We have an old maintenance middleware in place to handle when a site is forcibly put into maintenance mode, or the urlService hasn't finished booting
    - This maintenance middleware was mounted on every sub app, instead of globally for reasons I no longer remember
  - Recently, we introduced a new, static version of maintenence middleware to show during the boot process so we can get the server started earlier & not drop requests
    - This version has its own HTML template and doesn't depend on any of Ghost's error rendering code
  - To simplify and help with decoupling, this commit merges the two middleware, so that the new independent & static middleware renders its template for any one of the 3 possible maintenance modes
    - It only needs to exist in the top level app 🙌

TODO: move the maintenance middleware to its own file/package so it's not part of the app.js as that is weird
2021-11-24 11:27:18 +00:00

37 lines
1.0 KiB
JavaScript

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 (!isMaintenanceModeEnabled(req)) {
return next();
}
res.set({
'Cache-Control': 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'
});
res.writeHead(503, {'content-type': 'text/html'});
fs.createReadStream(path.resolve(__dirname, './server/views/maintenance.html')).pipe(res);
};
const rootApp = express('root');
rootApp.use(sentry.requestHandler);
rootApp.enable('maintenance');
rootApp.use(maintenanceMiddleware);
module.exports = rootApp;