mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05:00
Refactored maintenance app to be a class with DI
refs #12568 - DI pattern through constructor allows for very clear separation of concerns and makes it easy to extract the class into a separate module
This commit is contained in:
parent
7d9f056e9b
commit
244e2374d3
3 changed files with 66 additions and 52 deletions
|
@ -93,10 +93,6 @@ const initExpressApps = async () => {
|
|||
return parentApp;
|
||||
};
|
||||
|
||||
const initMaintenanceApp = () => {
|
||||
return require('./web/maintenance')();
|
||||
};
|
||||
|
||||
/**
|
||||
* - initialise models
|
||||
* - initialise i18n
|
||||
|
@ -142,7 +138,7 @@ const minimalRequiredSetupToStartGhost = async (dbState) => {
|
|||
config.set('maintenance:enabled', true);
|
||||
logging.info('Blog is in maintenance mode.');
|
||||
|
||||
ghostServer.rootApp = initMaintenanceApp();
|
||||
ghostServer.rootApp = require('./web/maintenance');
|
||||
|
||||
try {
|
||||
migrator.migrate()
|
||||
|
|
|
@ -1,13 +1,10 @@
|
|||
const hbs = require('express-hbs');
|
||||
const compress = require('compression');
|
||||
const debug = require('ghost-ignition').debug('web:maintenance');
|
||||
const constants = require('@tryghost/constants');
|
||||
const errors = require('@tryghost/errors');
|
||||
const {i18n} = require('../../lib/common');
|
||||
const logging = require('../../../shared/logging');
|
||||
const express = require('../../../shared/express');
|
||||
const config = require('../../../shared/config');
|
||||
const {servePublicFile} = require('../site/middleware');
|
||||
const {servePublicFile, serveFavicon} = require('../site/middleware');
|
||||
const MaintenanceApp = require('./maintenance-app');
|
||||
|
||||
const createHbsEngine = () => {
|
||||
const engine = hbs.create();
|
||||
|
@ -16,45 +13,13 @@ const createHbsEngine = () => {
|
|||
return engine.express4();
|
||||
};
|
||||
|
||||
module.exports = function setupMaintenanceApp() {
|
||||
debug('MaintenanceApp setup start');
|
||||
const app = express('maintenance');
|
||||
|
||||
// enabled gzip compression by defaulti
|
||||
if (config.get('compress') !== false) {
|
||||
app.use(compress());
|
||||
}
|
||||
|
||||
app.engine('hbs', createHbsEngine());
|
||||
app.set('view engine', 'hbs');
|
||||
app.set('views', config.get('paths').defaultViews);
|
||||
|
||||
// Serve stylesheets for default templates
|
||||
app.use(servePublicFile('public/ghost.css', 'text/css', constants.ONE_HOUR_S));
|
||||
app.use(servePublicFile('public/ghost.min.css', 'text/css', constants.ONE_YEAR_S));
|
||||
|
||||
// Serve images for default templates
|
||||
app.use(servePublicFile('public/404-ghost@2x.png', 'image/png', constants.ONE_HOUR_S));
|
||||
app.use(servePublicFile('public/404-ghost.png', 'image/png', constants.ONE_HOUR_S));
|
||||
|
||||
app.use('/', (req, res) => {
|
||||
const error = new errors.MaintenanceError({
|
||||
message: i18n.t('errors.general.maintenance')
|
||||
});
|
||||
|
||||
logging.error({req: req, res: res, err: error});
|
||||
|
||||
// never cache errors
|
||||
res.set({
|
||||
'Cache-Control': 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'
|
||||
});
|
||||
res.status(error.statusCode);
|
||||
res.render('error', error, (err, html) => {
|
||||
return res.send(html);
|
||||
});
|
||||
});
|
||||
|
||||
debug('MaintenanceApp setup end');
|
||||
|
||||
return app;
|
||||
};
|
||||
module.exports = new MaintenanceApp({
|
||||
logging,
|
||||
i18n,
|
||||
express,
|
||||
viewEngine: createHbsEngine(),
|
||||
compress: config.get('compress'),
|
||||
views: config.get('paths').defaultViews,
|
||||
servePublicFile,
|
||||
serveFavicon
|
||||
}).app;
|
||||
|
|
53
core/server/web/maintenance/maintenance-app.js
Normal file
53
core/server/web/maintenance/maintenance-app.js
Normal file
|
@ -0,0 +1,53 @@
|
|||
const compression = require('compression');
|
||||
const errors = require('@tryghost/errors');
|
||||
const constants = require('@tryghost/constants');
|
||||
const debug = require('ghost-ignition').debug('web:admin:app');
|
||||
|
||||
class MaintenanceApp {
|
||||
constructor({logging, i18n, express, viewEngine, compress, views, servePublicFile, serveFavicon}) {
|
||||
debug('MaintenanceApp setup start');
|
||||
const app = express('maintenance');
|
||||
|
||||
if (compress !== false) {
|
||||
app.use(compression());
|
||||
}
|
||||
|
||||
app.engine('hbs', viewEngine);
|
||||
app.set('view engine', 'hbs');
|
||||
app.set('views', views);
|
||||
|
||||
// Serve favicon.ico and favicon.png
|
||||
app.use(serveFavicon());
|
||||
|
||||
// Serve stylesheets for default templates
|
||||
app.use(servePublicFile('public/ghost.css', 'text/css', constants.ONE_HOUR_S));
|
||||
app.use(servePublicFile('public/ghost.min.css', 'text/css', constants.ONE_YEAR_S));
|
||||
|
||||
// Serve images for default templates
|
||||
app.use(servePublicFile('public/404-ghost@2x.png', 'image/png', constants.ONE_HOUR_S));
|
||||
app.use(servePublicFile('public/404-ghost.png', 'image/png', constants.ONE_HOUR_S));
|
||||
|
||||
app.use('/', (req, res) => {
|
||||
const error = new errors.MaintenanceError({
|
||||
message: i18n.t('errors.general.maintenance')
|
||||
});
|
||||
|
||||
logging.error({req: req, res: res, err: error});
|
||||
|
||||
res.render('error', error, (err, html) => {
|
||||
// never cache errors
|
||||
res.set({
|
||||
'Cache-Control': 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'
|
||||
});
|
||||
res.status(error.statusCode);
|
||||
return res.send(html);
|
||||
});
|
||||
});
|
||||
|
||||
debug('MaintenanceApp setup end');
|
||||
|
||||
this.app = app;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MaintenanceApp;
|
Loading…
Add table
Reference in a new issue