Skip to content

Commit a3f9543

Browse files
committed
Tweaker routing ++
1 parent 0b6b998 commit a3f9543

21 files changed

+1353
-263
lines changed

common/package-lock.json

+1,004-49
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/package.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
"private": true,
44
"version": "0.0.0",
55
"type": "module",
6-
"peerDependencies": {
6+
"devDependencies": {
77
"@navikt/ds-react": "5",
8+
"@types/express": "4.17.21",
89
"express": "4",
910
"lru-cache": "11",
1011
"react": "18"
11-
},
12-
"devDependencies": {
13-
"@types/express": "4.17.21"
1412
}
1513
}

legacy-archive/server/src/routing/errorHandlers.ts common/src/server/routing/errorHandlers.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { ErrorRequestHandler, Express, RequestHandler } from 'express';
1+
import { ErrorRequestHandler, RequestHandler, Router } from 'express';
22

3-
export const setupErrorHandlers = (expressApp: Express) => {
3+
export const setupErrorHandlers = (router: Router) => {
44
const notFoundHandler: RequestHandler = (req, res, _) => {
55
res.status(404).send('Not found');
66
};
@@ -21,7 +21,7 @@ export const setupErrorHandlers = (expressApp: Express) => {
2121
return res.status(statusCode).end();
2222
};
2323

24-
expressApp.use('*', notFoundHandler);
24+
router.use('*', notFoundHandler);
2525

26-
expressApp.use(serverErrorHandler);
26+
router.use(serverErrorHandler);
2727
};

common/src/server/routing/internal.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Router } from 'express';
2+
3+
export const setupNaisProbeHandlers = (router: Router) => {
4+
router.get('/internal/isAlive', (_, res) => res.status(200).json({ message: 'I am alive!' }));
5+
router.get('/internal/isReady', (_, res) => res.status(200).json({ message: 'I am ready!' }));
6+
};

common/src/server/ssr/initRenderer.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import express, { Express } from 'express';
1+
import express, { Router } from 'express';
22
import path from 'path';
33
import { createServer } from 'vite';
44
import { AppHtmlRenderer, devRenderer, HtmlRenderer, prodRenderer } from './htmlRenderer';
55

66
const assetsDir = path.resolve(process.cwd(), 'dist', 'client');
77

88
type Props = {
9-
expressApp: Express;
9+
router: Router;
1010
appHtmlRenderer: AppHtmlRenderer;
1111
ssrModulePath: string;
1212
appBaseBath: string;
1313
};
1414

1515
export const buildHtmlRenderer = async ({
16-
expressApp,
16+
router,
1717
appHtmlRenderer,
1818
ssrModulePath,
1919
appBaseBath,
@@ -28,14 +28,14 @@ export const buildHtmlRenderer = async ({
2828
base: appBaseBath,
2929
});
3030

31-
expressApp.use(vite.middlewares);
31+
router.use(vite.middlewares);
3232

3333
return devRenderer(vite, ssrModulePath);
3434
}
3535

3636
console.log(`Configuring site renderer for production mode - Using assets dir ${assetsDir}`);
3737

38-
expressApp.use(
38+
router.use(
3939
'/',
4040
express.static(assetsDir, {
4141
maxAge: '1y',

common/src/server/utils/validateEnv.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import process from 'process';
22

3-
export const validateEnv = async (expectedVars: string[]) => {
3+
export const validateEnv = async (expectedVars: readonly string[] | string[]) => {
44
const isValid = expectedVars.every((key) => {
55
if (process.env[key]) {
66
return true;

legacy-archive/server/src/cms/CmsArchiveSite.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import express, { Express, Response, Router } from 'express';
33
import { CmsArchiveContentService } from './CmsArchiveContentService';
44
import { parseNumberParam, parseQueryParamsList } from '../utils/queryParams';
55
import mime from 'mime';
6-
import { HtmlRenderer } from '../ssr/htmlRenderer';
76
import { transformQueryToContentSearchParams } from '../opensearch/queries/contentSearch';
87
import { CmsArchiveCategoriesService } from './CmsArchiveCategoriesService';
98
import { cspMiddleware } from '../routing/csp';
@@ -12,6 +11,7 @@ import { PdfGenerator } from '../pdf/PdfGenerator';
1211
import { Browser } from 'puppeteer';
1312
import { DOWNLOAD_COOKIE_NAME } from '../../../shared/downloadCookie';
1413
import { LegacyArchiveSiteConfig } from '@common/shared/siteConfigs';
14+
import { HtmlRenderer } from '@common/server/ssr/htmlRenderer';
1515

1616
type ContructorProps = {
1717
config: LegacyArchiveSiteConfig;

legacy-archive/server/src/routing/internal.ts

-10
This file was deleted.

legacy-archive/server/src/routing/site.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import { render } from '../_ssr-dist/main-server';
55
import puppeteer from 'puppeteer';
66
import { legacyArchiveConfigs } from '@common/shared/siteConfigs';
77
import { buildHtmlRenderer } from '@common/server/ssr/initRenderer';
8+
import { setupErrorHandlers } from '@common/server/routing/errorHandlers';
89

910
export const setupSites = async (expressApp: Express) => {
1011
const opensearchClent = new CmsArchiveOpenSearchClient();
1112

1213
const htmlRenderer = await buildHtmlRenderer({
13-
expressApp,
14+
router: expressApp,
1415
appHtmlRenderer: render,
1516
appBaseBath: process.env.APP_BASEPATH,
1617
ssrModulePath: '/client/main-server.tsx',
@@ -35,4 +36,6 @@ export const setupSites = async (expressApp: Express) => {
3536
expressApp.get('/', (req, res) => {
3637
return res.redirect(legacyArchiveConfigs[0].baseUrl);
3738
});
39+
40+
setupErrorHandlers(expressApp);
3841
};

legacy-archive/server/src/server.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
import express from 'express';
22
import compression from 'compression';
3-
import { setupErrorHandlers } from './routing/errorHandlers';
4-
import { setupInternalRoutes } from './routing/internal';
53
import { setupSites } from './routing/site';
64
import { validateEnv } from '@common/server/utils/validateEnv';
5+
import { setupNaisProbeHandlers } from '@common/server/routing/internal';
76

8-
const { APP_PORT } = process.env;
9-
10-
validateEnv([
7+
const expectedEnv = [
118
'NODE_ENV',
129
'APP_PORT',
1310
'APP_BASEPATH',
1411
'APP_ORIGIN',
1512
'OPEN_SEARCH_URI',
1613
'OPEN_SEARCH_USERNAME',
1714
'OPEN_SEARCH_PASSWORD',
18-
])
15+
] as const;
16+
17+
validateEnv(expectedEnv)
1918
.then(async () => {
2019
const app = express().use(compression(), express.json());
2120

22-
setupInternalRoutes(app);
21+
setupNaisProbeHandlers(app);
2322
await setupSites(app);
24-
setupErrorHandlers(app);
2523

2624
return app;
2725
})
@@ -30,6 +28,8 @@ validateEnv([
3028
throw e;
3129
})
3230
.then((app) => {
31+
const { APP_PORT } = process.env;
32+
3333
const server = app.listen(APP_PORT, () => {
3434
console.log(`Server starting on port ${APP_PORT}`);
3535
});

legacy-archive/vite.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default defineConfig(({ mode }) => {
1717
: []),
1818
],
1919
ssr: {
20-
// Externalizing certain libraries seems to cause crashes due to invalid imports in the SSR bundle
20+
// Externalizing certain libraries causes SSR crashes due to invalid imports in the SSR bundle
2121
noExternal: [
2222
'@babel/runtime',
2323
'@navikt/aksel-icons',

0 commit comments

Comments
 (0)