2022-02-15 16:17:14 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const path = require('path');
|
2022-02-17 15:56:54 -07:00
|
|
|
const express = require('express');
|
2022-11-28 20:47:57 -07:00
|
|
|
const cors = require('cors');
|
2022-02-15 16:17:14 -07:00
|
|
|
const asyncHandler = require('../lib/express-async-handler');
|
|
|
|
|
2022-09-08 00:30:04 -06:00
|
|
|
const { handleTracingMiddleware } = require('../tracing/tracing-middleware');
|
|
|
|
const getVersionTags = require('../lib/get-version-tags');
|
2023-04-19 14:09:51 -06:00
|
|
|
const preventClickjackingMiddleware = require('../middleware/prevent-clickjacking-middleware');
|
|
|
|
const contentSecurityPolicyMiddleware = require('../middleware/content-security-policy-middleware');
|
|
|
|
const identifyRoute = require('../middleware/identify-route-middleware');
|
2022-10-27 23:32:24 -06:00
|
|
|
const clientSideRoomAliasHashRedirectRoute = require('./client-side-room-alias-hash-redirect-route');
|
2023-04-19 14:09:51 -06:00
|
|
|
const redirectToCorrectArchiveUrlIfBadSigil = require('../middleware/redirect-to-correct-archive-url-if-bad-sigil-middleware');
|
2022-02-15 16:17:14 -07:00
|
|
|
|
|
|
|
function installRoutes(app) {
|
2022-07-14 10:08:50 -06:00
|
|
|
app.use(handleTracingMiddleware);
|
2022-09-08 18:30:20 -06:00
|
|
|
app.use(preventClickjackingMiddleware);
|
2022-10-19 11:07:39 -06:00
|
|
|
app.use(contentSecurityPolicyMiddleware);
|
2022-11-28 20:47:57 -07:00
|
|
|
app.use(cors());
|
2022-07-14 10:08:50 -06:00
|
|
|
|
2022-09-08 00:30:04 -06:00
|
|
|
let healthCheckResponse;
|
2022-07-14 10:08:50 -06:00
|
|
|
app.get(
|
|
|
|
'/health-check',
|
2023-04-19 14:09:51 -06:00
|
|
|
identifyRoute('health-check'),
|
2022-07-14 10:08:50 -06:00
|
|
|
asyncHandler(async function (req, res) {
|
2022-09-08 00:30:04 -06:00
|
|
|
if (!healthCheckResponse) {
|
|
|
|
const versionTags = await getVersionTags();
|
|
|
|
const responseObject = {
|
|
|
|
ok: true,
|
|
|
|
...versionTags,
|
|
|
|
};
|
|
|
|
healthCheckResponse = JSON.stringify(responseObject, null, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
res.set('Content-Type', 'application/json');
|
|
|
|
res.send(healthCheckResponse);
|
2022-07-14 10:08:50 -06:00
|
|
|
})
|
|
|
|
);
|
2022-06-15 16:12:44 -06:00
|
|
|
|
2022-02-17 15:56:54 -07:00
|
|
|
// We have to disable no-missing-require lint because it doesn't take into
|
|
|
|
// account `package.json`. `exports`, see
|
|
|
|
// https://github.com/mysticatea/eslint-plugin-node/issues/255
|
2023-04-19 13:44:12 -06:00
|
|
|
app.use(
|
|
|
|
'/hydrogen-assets',
|
|
|
|
// eslint-disable-next-line node/no-missing-require
|
|
|
|
express.static(path.dirname(require.resolve('hydrogen-view-sdk/assets/main.js')))
|
|
|
|
);
|
2022-02-17 15:56:54 -07:00
|
|
|
|
2022-07-14 10:08:50 -06:00
|
|
|
app.get(
|
2022-09-08 00:30:04 -06:00
|
|
|
// This has to be at the root so that the font URL references resolve correctly
|
2023-04-19 13:44:12 -06:00
|
|
|
'/hydrogen-assets/hydrogen-styles.css',
|
2022-07-14 10:08:50 -06:00
|
|
|
asyncHandler(async function (req, res) {
|
|
|
|
res.set('Content-Type', 'text/css');
|
|
|
|
// We have to disable no-missing-require lint because it doesn't take into
|
|
|
|
// account `package.json`. `exports`, see
|
|
|
|
// https://github.com/mysticatea/eslint-plugin-node/issues/255
|
|
|
|
// eslint-disable-next-line node/no-missing-require
|
|
|
|
res.sendFile(require.resolve('hydrogen-view-sdk/assets/theme-element-light.css'));
|
|
|
|
})
|
|
|
|
);
|
2022-02-15 16:17:14 -07:00
|
|
|
|
2022-09-08 00:30:04 -06:00
|
|
|
// Our own archive app styles and scripts
|
|
|
|
app.use('/css', express.static(path.join(__dirname, '../../public/css')));
|
2022-10-18 00:30:26 -06:00
|
|
|
app.use('/img', express.static(path.join(__dirname, '../../public/img')));
|
2022-09-08 00:30:04 -06:00
|
|
|
app.use('/js', express.static(path.join(__dirname, '../../dist/')));
|
2022-02-15 16:17:14 -07:00
|
|
|
|
2022-09-08 00:30:04 -06:00
|
|
|
app.use('/', require('./room-directory-routes'));
|
2022-02-15 16:17:14 -07:00
|
|
|
|
2022-10-27 00:09:13 -06:00
|
|
|
// For room aliases (/r) or room ID's (/roomid)
|
|
|
|
app.use('/:entityDescriptor(r|roomid)/:roomIdOrAliasDirty', require('./room-routes'));
|
|
|
|
|
2022-10-27 23:32:24 -06:00
|
|
|
// 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`
|
2023-04-19 14:09:51 -06:00
|
|
|
app.get(
|
|
|
|
'/:entityDescriptor(r|roomid)',
|
|
|
|
identifyRoute('client-side-room-alias-hash-redirect'),
|
|
|
|
clientSideRoomAliasHashRedirectRoute
|
|
|
|
);
|
2022-10-27 23:32:24 -06:00
|
|
|
|
2022-10-27 00:09:13 -06:00
|
|
|
// 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.
|
2023-04-19 14:09:51 -06:00
|
|
|
app.get(
|
|
|
|
'/:roomIdOrAliasDirty',
|
|
|
|
identifyRoute('redirect-to-correct-archive-url-if-bad-sigil'),
|
|
|
|
redirectToCorrectArchiveUrlIfBadSigil
|
|
|
|
);
|
2022-02-15 16:17:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = installRoutes;
|