From 621cfd98663d2f4a9ae824107bf338c92078578b Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Thu, 25 Nov 2021 14:29:17 +0000 Subject: [PATCH] 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 --- core/server/web/admin/app.js | 4 ++-- .../web/admin/middleware/redirect-admin-urls.js | 15 +++++++++++++++ test/unit/server/web/admin/middleware.test.js | 2 +- 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 core/server/web/admin/middleware/redirect-admin-urls.js diff --git a/core/server/web/admin/app.js b/core/server/web/admin/app.js index 93e106c021..6f56ba58eb 100644 --- a/core/server/web/admin/app.js +++ b/core/server/web/admin/app.js @@ -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')); diff --git a/core/server/web/admin/middleware/redirect-admin-urls.js b/core/server/web/admin/middleware/redirect-admin-urls.js new file mode 100644 index 0000000000..097c0d784a --- /dev/null +++ b/core/server/web/admin/middleware/redirect-admin-urls.js @@ -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; diff --git a/test/unit/server/web/admin/middleware.test.js b/test/unit/server/web/admin/middleware.test.js index d9cf8b0cd8..00dc587845 100644 --- a/test/unit/server/web/admin/middleware.test.js +++ b/test/unit/server/web/admin/middleware.test.js @@ -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 () {