mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -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;
|
return parentApp;
|
||||||
};
|
};
|
||||||
|
|
||||||
const initMaintenanceApp = () => {
|
|
||||||
return require('./web/maintenance')();
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* - initialise models
|
* - initialise models
|
||||||
* - initialise i18n
|
* - initialise i18n
|
||||||
|
@ -142,7 +138,7 @@ const minimalRequiredSetupToStartGhost = async (dbState) => {
|
||||||
config.set('maintenance:enabled', true);
|
config.set('maintenance:enabled', true);
|
||||||
logging.info('Blog is in maintenance mode.');
|
logging.info('Blog is in maintenance mode.');
|
||||||
|
|
||||||
ghostServer.rootApp = initMaintenanceApp();
|
ghostServer.rootApp = require('./web/maintenance');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
migrator.migrate()
|
migrator.migrate()
|
||||||
|
|
|
@ -1,13 +1,10 @@
|
||||||
const hbs = require('express-hbs');
|
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 {i18n} = require('../../lib/common');
|
||||||
const logging = require('../../../shared/logging');
|
const logging = require('../../../shared/logging');
|
||||||
const express = require('../../../shared/express');
|
const express = require('../../../shared/express');
|
||||||
const config = require('../../../shared/config');
|
const config = require('../../../shared/config');
|
||||||
const {servePublicFile} = require('../site/middleware');
|
const {servePublicFile, serveFavicon} = require('../site/middleware');
|
||||||
|
const MaintenanceApp = require('./maintenance-app');
|
||||||
|
|
||||||
const createHbsEngine = () => {
|
const createHbsEngine = () => {
|
||||||
const engine = hbs.create();
|
const engine = hbs.create();
|
||||||
|
@ -16,45 +13,13 @@ const createHbsEngine = () => {
|
||||||
return engine.express4();
|
return engine.express4();
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = function setupMaintenanceApp() {
|
module.exports = new MaintenanceApp({
|
||||||
debug('MaintenanceApp setup start');
|
logging,
|
||||||
const app = express('maintenance');
|
i18n,
|
||||||
|
express,
|
||||||
// enabled gzip compression by defaulti
|
viewEngine: createHbsEngine(),
|
||||||
if (config.get('compress') !== false) {
|
compress: config.get('compress'),
|
||||||
app.use(compress());
|
views: config.get('paths').defaultViews,
|
||||||
}
|
servePublicFile,
|
||||||
|
serveFavicon
|
||||||
app.engine('hbs', createHbsEngine());
|
}).app;
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
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