Moved admin redirect middleware to named file

- moving this middleware because we're about to add a second piece of middleware
- it's easier to see what we have when each middleware is in its own file rather than in one big middleware.js file
This commit is contained in:
Hannah Wolfe 2021-11-25 14:29:17 +00:00
parent d92eebd74e
commit 621cfd9866
No known key found for this signature in database
GPG Key ID: AB586C3B5AE5C037
3 changed files with 18 additions and 3 deletions

View File

@ -5,7 +5,7 @@ const config = require('../../../shared/config');
const constants = require('@tryghost/constants');
const urlUtils = require('../../../shared/url-utils');
const shared = require('../shared');
const adminMiddleware = require('./middleware');
const redirectAdminUrls = require('./middleware/redirect-admin-urls');
module.exports = function setupAdminApp() {
debug('Admin setup start');
@ -39,7 +39,7 @@ module.exports = function setupAdminApp() {
adminApp.use(shared.middleware.cacheControl('private'));
// Special redirects for the admin (these should have their own cache-control headers)
adminApp.use(adminMiddleware);
adminApp.use(redirectAdminUrls);
// Finally, routing
adminApp.get('*', require('./controller'));

View File

@ -0,0 +1,15 @@
const urlUtils = require('../../../../shared/url-utils');
function redirectAdminUrls(req, res, next) {
const subdir = urlUtils.getSubdir();
const ghostPathRegex = new RegExp(`^${subdir}/ghost/(.+)`);
const ghostPathMatch = req.originalUrl.match(ghostPathRegex);
if (ghostPathMatch) {
return res.redirect(urlUtils.urlJoin(urlUtils.urlFor('admin'), '#', ghostPathMatch[1]));
}
next();
}
module.exports = redirectAdminUrls;

View File

@ -2,7 +2,7 @@ const should = require('should');
const sinon = require('sinon');
// Thing we are testing
const redirectAdminUrls = require('../../../../../core/server/web/admin/middleware')[0];
const redirectAdminUrls = require('../../../../../core/server/web/admin/middleware/redirect-admin-urls');
describe('Admin App', function () {
afterEach(function () {