Ghost/core/app.js
Naz abcd715907 Fixed express app stacking
refs https://github.com/TryGhost/Toolbox/issues/152

- Because the root app module was initialized only once per runtime it caused all the express apps to stack on each other causing all sorts of strange behavior when trying to test redirects/vhost mounts etc. Lesson here: be very cautious of how the module is initialized, an explicit function is almost always a better way!
2021-12-06 21:28:53 +13:00

41 lines
1.1 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 = () => {
const app = express('root');
app.use(sentry.requestHandler);
app.enable('maintenance');
app.use(maintenanceMiddleware);
return app;
};
module.exports = rootApp;