From 78ee88e094c337ef2e9fbbac46f6d37b20cee999 Mon Sep 17 00:00:00 2001 From: Eric Eastwood Date: Wed, 19 Apr 2023 15:09:51 -0500 Subject: [PATCH] Add route identifiers for easy metric reporting (#173) Pre-requisite for https://github.com/matrix-org/matrix-public-archive/issues/162 and https://github.com/matrix-org/matrix-public-archive/issues/148 --- .../content-security-policy-middleware.js | 0 .../middleware/identify-route-middleware.js | 11 ++++++++++ .../prevent-clickjacking-middleware.js | 0 ...ect-archive-url-if-bad-sigil-middleware.js | 0 .../timeout-middleware.js | 0 server/routes/install-routes.js | 20 ++++++++++++++----- server/routes/room-directory-routes.js | 2 ++ server/routes/room-routes.js | 9 +++++++-- 8 files changed, 35 insertions(+), 7 deletions(-) rename server/{routes => middleware}/content-security-policy-middleware.js (100%) create mode 100644 server/middleware/identify-route-middleware.js rename server/{routes => middleware}/prevent-clickjacking-middleware.js (100%) rename server/{routes => middleware}/redirect-to-correct-archive-url-if-bad-sigil-middleware.js (100%) rename server/{routes => middleware}/timeout-middleware.js (100%) diff --git a/server/routes/content-security-policy-middleware.js b/server/middleware/content-security-policy-middleware.js similarity index 100% rename from server/routes/content-security-policy-middleware.js rename to server/middleware/content-security-policy-middleware.js diff --git a/server/middleware/identify-route-middleware.js b/server/middleware/identify-route-middleware.js new file mode 100644 index 0000000..ec37e1a --- /dev/null +++ b/server/middleware/identify-route-middleware.js @@ -0,0 +1,11 @@ +'use strict'; + +// via https://gitlab.com/gitterHQ/env/-/blob/2a184afea5dc5f4db478d651b3597001474ee932/lib/middlewares/identify-route.js +function identifyRouteMiddleware(routeName) { + return function identifyRoute(req, res, next) { + req.routeIdentifier = routeName; + next(); + }; +} + +module.exports = identifyRouteMiddleware; diff --git a/server/routes/prevent-clickjacking-middleware.js b/server/middleware/prevent-clickjacking-middleware.js similarity index 100% rename from server/routes/prevent-clickjacking-middleware.js rename to server/middleware/prevent-clickjacking-middleware.js diff --git a/server/routes/redirect-to-correct-archive-url-if-bad-sigil-middleware.js b/server/middleware/redirect-to-correct-archive-url-if-bad-sigil-middleware.js similarity index 100% rename from server/routes/redirect-to-correct-archive-url-if-bad-sigil-middleware.js rename to server/middleware/redirect-to-correct-archive-url-if-bad-sigil-middleware.js diff --git a/server/routes/timeout-middleware.js b/server/middleware/timeout-middleware.js similarity index 100% rename from server/routes/timeout-middleware.js rename to server/middleware/timeout-middleware.js diff --git a/server/routes/install-routes.js b/server/routes/install-routes.js index 8b43512..b147aa3 100644 --- a/server/routes/install-routes.js +++ b/server/routes/install-routes.js @@ -7,10 +7,11 @@ const asyncHandler = require('../lib/express-async-handler'); const { handleTracingMiddleware } = require('../tracing/tracing-middleware'); const getVersionTags = require('../lib/get-version-tags'); -const preventClickjackingMiddleware = require('./prevent-clickjacking-middleware'); -const contentSecurityPolicyMiddleware = require('./content-security-policy-middleware'); +const preventClickjackingMiddleware = require('../middleware/prevent-clickjacking-middleware'); +const contentSecurityPolicyMiddleware = require('../middleware/content-security-policy-middleware'); +const identifyRoute = require('../middleware/identify-route-middleware'); const clientSideRoomAliasHashRedirectRoute = require('./client-side-room-alias-hash-redirect-route'); -const redirectToCorrectArchiveUrlIfBadSigil = require('./redirect-to-correct-archive-url-if-bad-sigil-middleware'); +const redirectToCorrectArchiveUrlIfBadSigil = require('../middleware/redirect-to-correct-archive-url-if-bad-sigil-middleware'); function installRoutes(app) { app.use(handleTracingMiddleware); @@ -21,6 +22,7 @@ function installRoutes(app) { let healthCheckResponse; app.get( '/health-check', + identifyRoute('health-check'), asyncHandler(async function (req, res) { if (!healthCheckResponse) { const versionTags = await getVersionTags(); @@ -71,11 +73,19 @@ function installRoutes(app) { // Since everything after the hash (`#`) won't make it to the server, let's serve a 404 // page that will potentially redirect them to the correct place if they tried // `/r/#room-alias:server/date/2022/10/27` -> `/r/room-alias:server/date/2022/10/27` - app.use('/:entityDescriptor(r|roomid)', clientSideRoomAliasHashRedirectRoute); + app.get( + '/:entityDescriptor(r|roomid)', + identifyRoute('client-side-room-alias-hash-redirect'), + clientSideRoomAliasHashRedirectRoute + ); // Correct any honest mistakes: If someone accidentally put the sigil in the URL, then // redirect them to the correct URL without the sigil to the correct path above. - app.use('/:roomIdOrAliasDirty', redirectToCorrectArchiveUrlIfBadSigil); + app.get( + '/:roomIdOrAliasDirty', + identifyRoute('redirect-to-correct-archive-url-if-bad-sigil'), + redirectToCorrectArchiveUrlIfBadSigil + ); } module.exports = installRoutes; diff --git a/server/routes/room-directory-routes.js b/server/routes/room-directory-routes.js index 8aa9f60..e5f4f5c 100644 --- a/server/routes/room-directory-routes.js +++ b/server/routes/room-directory-routes.js @@ -6,6 +6,7 @@ const urlJoin = require('url-join'); const express = require('express'); const asyncHandler = require('../lib/express-async-handler'); +const identifyRoute = require('../middleware/identify-route-middleware'); const fetchPublicRooms = require('../lib/matrix-utils/fetch-public-rooms'); const renderHydrogenVmRenderScriptToPageHtml = require('../hydrogen-render/render-hydrogen-vm-render-script-to-page-html'); const setHeadersToPreloadAssets = require('../lib/set-headers-to-preload-assets'); @@ -29,6 +30,7 @@ const router = express.Router({ router.get( '/', + identifyRoute('app-room-directory-index'), asyncHandler(async function (req, res) { const paginationToken = req.query.page; const searchTerm = req.query.search; diff --git a/server/routes/room-routes.js b/server/routes/room-routes.js index e296764..932dfbd 100644 --- a/server/routes/room-routes.js +++ b/server/routes/room-routes.js @@ -7,8 +7,9 @@ const express = require('express'); const asyncHandler = require('../lib/express-async-handler'); const StatusError = require('../lib/status-error'); -const timeoutMiddleware = require('./timeout-middleware'); -const redirectToCorrectArchiveUrlIfBadSigil = require('./redirect-to-correct-archive-url-if-bad-sigil-middleware'); +const timeoutMiddleware = require('../middleware/timeout-middleware'); +const redirectToCorrectArchiveUrlIfBadSigil = require('../middleware/redirect-to-correct-archive-url-if-bad-sigil-middleware'); +const identifyRoute = require('../middleware/identify-route-middleware'); const { HTTPResponseError } = require('../lib/fetch-endpoint'); const parseViaServersFromUserInput = require('../lib/parse-via-servers-from-user-input'); @@ -167,6 +168,7 @@ router.use(redirectToCorrectArchiveUrlIfBadSigil); router.get( '/', + identifyRoute('app-archive-room-index'), asyncHandler(async function (req, res) { const roomIdOrAlias = getRoomIdOrAliasFromReq(req); @@ -208,6 +210,7 @@ router.get( router.get( '/event/:eventId', + identifyRoute('app-archive-room-event'), asyncHandler(async function (req, res) { // TODO: Fetch event to get `origin_server_ts` and redirect to // /!roomId/2022/01/01?at=$eventId @@ -217,6 +220,7 @@ router.get( router.get( '/jump', + identifyRoute('app-archive-room-jump'), // eslint-disable-next-line max-statements, complexity asyncHandler(async function (req, res) { const roomIdOrAlias = getRoomIdOrAliasFromReq(req); @@ -726,6 +730,7 @@ router.get( // `path-to-regex` bug where the `?` wasn't attaching to the capture group, see // https://github.com/pillarjs/path-to-regexp/issues/287 '/date/:yyyy(\\d{4})/:mm(\\d{2})/:dd(\\d{2}):time(T\\d\\d?:\\d\\d?((:\\d\\d?)?))?', + identifyRoute('app-archive-room-date'), timeoutMiddleware, // eslint-disable-next-line max-statements, complexity asyncHandler(async function (req, res) {