From f5ecba5fcdcbc58dba73751048df47f2a9328084 Mon Sep 17 00:00:00 2001 From: yavorsk Date: Thu, 12 Sep 2024 12:39:22 +0300 Subject: [PATCH 1/6] the health check endpoint and middleware --- .../src/templates/node-xmcloud-proxy/src/index.ts | 6 ++++++ .../sitecore-jss-proxy/src/middleware/index.ts | 1 + .../monitoring/healthcheck-middleware.test.ts | 15 +++++++++++++++ .../monitoring/healthcheck-middleware.ts | 8 ++++++++ .../src/middleware/monitoring/index.ts | 1 + 5 files changed, 31 insertions(+) create mode 100644 packages/sitecore-jss-proxy/src/middleware/monitoring/healthcheck-middleware.test.ts create mode 100644 packages/sitecore-jss-proxy/src/middleware/monitoring/healthcheck-middleware.ts create mode 100644 packages/sitecore-jss-proxy/src/middleware/monitoring/index.ts diff --git a/packages/create-sitecore-jss/src/templates/node-xmcloud-proxy/src/index.ts b/packages/create-sitecore-jss/src/templates/node-xmcloud-proxy/src/index.ts index 6431f400f8..1a95b5584b 100644 --- a/packages/create-sitecore-jss/src/templates/node-xmcloud-proxy/src/index.ts +++ b/packages/create-sitecore-jss/src/templates/node-xmcloud-proxy/src/index.ts @@ -4,6 +4,7 @@ import compression from 'compression'; import { createProxyMiddleware } from 'http-proxy-middleware'; import { debug } from '@sitecore-jss/sitecore-jss'; import { editingRouter } from '@sitecore-jss/sitecore-jss-proxy'; +import { healthCheckMiddleware } from '@sitecore-jss/sitecore-jss-proxy'; import { config } from './config'; const server = express(); @@ -127,6 +128,11 @@ server.use( }) ); +/** + * The health check endpoint + */ +server.get('/api/healthz', healthCheckMiddleware()); + /** * Proxy editing requests through the editing router */ diff --git a/packages/sitecore-jss-proxy/src/middleware/index.ts b/packages/sitecore-jss-proxy/src/middleware/index.ts index a517e03e02..b3cce32353 100644 --- a/packages/sitecore-jss-proxy/src/middleware/index.ts +++ b/packages/sitecore-jss-proxy/src/middleware/index.ts @@ -2,3 +2,4 @@ // eslint-disable-next-line prettier/prettier export * as headlessProxy from './headless-ssr-proxy'; export { editingRouter } from './editing'; +export { healthCheckMiddleware } from './monitoring'; diff --git a/packages/sitecore-jss-proxy/src/middleware/monitoring/healthcheck-middleware.test.ts b/packages/sitecore-jss-proxy/src/middleware/monitoring/healthcheck-middleware.test.ts new file mode 100644 index 0000000000..22059c63a0 --- /dev/null +++ b/packages/sitecore-jss-proxy/src/middleware/monitoring/healthcheck-middleware.test.ts @@ -0,0 +1,15 @@ +import express from 'express'; +import request from 'supertest'; +import { healthCheckMiddleware } from './index'; + +describe('editingRouter - /editing/render', () => { + const app = express(); + + it('should handle request', async () => { + app.get('/api/halthz', healthCheckMiddleware()); + + request(app) + .get('/api/halthz') + .expect(200, 'Healthy'); + }); +}); diff --git a/packages/sitecore-jss-proxy/src/middleware/monitoring/healthcheck-middleware.ts b/packages/sitecore-jss-proxy/src/middleware/monitoring/healthcheck-middleware.ts new file mode 100644 index 0000000000..59b89fe3f1 --- /dev/null +++ b/packages/sitecore-jss-proxy/src/middleware/monitoring/healthcheck-middleware.ts @@ -0,0 +1,8 @@ +import { Request, Response } from 'express'; + +/** + * Middleware to handle health check requests + */ +export const healthCheckMiddleware = () => (_req: Request, res: Response): void => { + res.status(200).send('Healthy'); +}; diff --git a/packages/sitecore-jss-proxy/src/middleware/monitoring/index.ts b/packages/sitecore-jss-proxy/src/middleware/monitoring/index.ts new file mode 100644 index 0000000000..eb39226e9a --- /dev/null +++ b/packages/sitecore-jss-proxy/src/middleware/monitoring/index.ts @@ -0,0 +1 @@ +export { healthCheckMiddleware } from './healthcheck-middleware'; From a9d1957f731170d91d014d36cf1ac14780f61bef Mon Sep 17 00:00:00 2001 From: yavorsk Date: Thu, 12 Sep 2024 12:45:46 +0300 Subject: [PATCH 2/6] changelog update --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec06f0b97f..96cf53a426 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,6 +57,7 @@ Our versioning strategy is as follows: - `scTextEmptyFieldEditingTemplate` for _scText_ * `[sitecore-jss-angular]` `[templates/angular-xmcloud]` Render clientScripts / clientData. The new `sc-editing-scripts` component is exposed from `sitecore-jss-angular` package and required to be rendered on the page to enable Metadata Edit mode. ([#1924](https://github.com/Sitecore/jss/pull/1924)) * `[sitecore-jss]` GenericFieldValue model is updated to accept Date type ([#1916](https://github.com/Sitecore/jss/pull/1916)) +* `[template/node-xmcloud-proxy]` `[sitecore-jss-proxy]` Introduced /api/healthz endpoint ([#1928](https://github.com/Sitecore/jss/pull/1928)) ### 🛠 Breaking Change From c024a1464a4ecebc8d891a166c8fce288fd17e0e Mon Sep 17 00:00:00 2001 From: Yavor Krastev <4502045+yavorsk@users.noreply.github.com> Date: Thu, 12 Sep 2024 14:19:35 +0300 Subject: [PATCH 3/6] fix typo Co-authored-by: Illia Kovalenko <23364749+illiakovalenko@users.noreply.github.com> --- .../src/middleware/monitoring/healthcheck-middleware.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/sitecore-jss-proxy/src/middleware/monitoring/healthcheck-middleware.test.ts b/packages/sitecore-jss-proxy/src/middleware/monitoring/healthcheck-middleware.test.ts index 22059c63a0..d9f9274050 100644 --- a/packages/sitecore-jss-proxy/src/middleware/monitoring/healthcheck-middleware.test.ts +++ b/packages/sitecore-jss-proxy/src/middleware/monitoring/healthcheck-middleware.test.ts @@ -6,7 +6,7 @@ describe('editingRouter - /editing/render', () => { const app = express(); it('should handle request', async () => { - app.get('/api/halthz', healthCheckMiddleware()); + app.get('/api/healthz', healthCheckMiddleware()); request(app) .get('/api/halthz') From 52df96c02d35199938545a90124b739f975109b1 Mon Sep 17 00:00:00 2001 From: yavorsk Date: Thu, 12 Sep 2024 15:09:51 +0300 Subject: [PATCH 4/6] add healtcheck router to hide the endpoint path in proxy package --- .../src/templates/node-xmcloud-proxy/src/index.ts | 4 ++-- .../healthcheck-middleware.test.ts | 4 ++-- .../healthcheck-middleware.ts | 0 .../src/middleware/healtchcheck/index.test.ts | 15 +++++++++++++++ .../src/middleware/healtchcheck/index.ts | 14 ++++++++++++++ .../sitecore-jss-proxy/src/middleware/index.ts | 2 +- .../src/middleware/monitoring/index.ts | 1 - 7 files changed, 34 insertions(+), 6 deletions(-) rename packages/sitecore-jss-proxy/src/middleware/{monitoring => healtchcheck}/healthcheck-middleware.test.ts (68%) rename packages/sitecore-jss-proxy/src/middleware/{monitoring => healtchcheck}/healthcheck-middleware.ts (100%) create mode 100644 packages/sitecore-jss-proxy/src/middleware/healtchcheck/index.test.ts create mode 100644 packages/sitecore-jss-proxy/src/middleware/healtchcheck/index.ts delete mode 100644 packages/sitecore-jss-proxy/src/middleware/monitoring/index.ts diff --git a/packages/create-sitecore-jss/src/templates/node-xmcloud-proxy/src/index.ts b/packages/create-sitecore-jss/src/templates/node-xmcloud-proxy/src/index.ts index 1a95b5584b..65d1f3f8cf 100644 --- a/packages/create-sitecore-jss/src/templates/node-xmcloud-proxy/src/index.ts +++ b/packages/create-sitecore-jss/src/templates/node-xmcloud-proxy/src/index.ts @@ -4,7 +4,7 @@ import compression from 'compression'; import { createProxyMiddleware } from 'http-proxy-middleware'; import { debug } from '@sitecore-jss/sitecore-jss'; import { editingRouter } from '@sitecore-jss/sitecore-jss-proxy'; -import { healthCheckMiddleware } from '@sitecore-jss/sitecore-jss-proxy'; +import { healthCheck } from '@sitecore-jss/sitecore-jss-proxy'; import { config } from './config'; const server = express(); @@ -131,7 +131,7 @@ server.use( /** * The health check endpoint */ -server.get('/api/healthz', healthCheckMiddleware()); +server.use(healthCheck()); /** * Proxy editing requests through the editing router diff --git a/packages/sitecore-jss-proxy/src/middleware/monitoring/healthcheck-middleware.test.ts b/packages/sitecore-jss-proxy/src/middleware/healtchcheck/healthcheck-middleware.test.ts similarity index 68% rename from packages/sitecore-jss-proxy/src/middleware/monitoring/healthcheck-middleware.test.ts rename to packages/sitecore-jss-proxy/src/middleware/healtchcheck/healthcheck-middleware.test.ts index 22059c63a0..273b6aa3f4 100644 --- a/packages/sitecore-jss-proxy/src/middleware/monitoring/healthcheck-middleware.test.ts +++ b/packages/sitecore-jss-proxy/src/middleware/healtchcheck/healthcheck-middleware.test.ts @@ -1,8 +1,8 @@ import express from 'express'; import request from 'supertest'; -import { healthCheckMiddleware } from './index'; +import { healthCheckMiddleware } from './healthcheck-middleware'; -describe('editingRouter - /editing/render', () => { +describe('healthcheck middleware - /api/healthz', () => { const app = express(); it('should handle request', async () => { diff --git a/packages/sitecore-jss-proxy/src/middleware/monitoring/healthcheck-middleware.ts b/packages/sitecore-jss-proxy/src/middleware/healtchcheck/healthcheck-middleware.ts similarity index 100% rename from packages/sitecore-jss-proxy/src/middleware/monitoring/healthcheck-middleware.ts rename to packages/sitecore-jss-proxy/src/middleware/healtchcheck/healthcheck-middleware.ts diff --git a/packages/sitecore-jss-proxy/src/middleware/healtchcheck/index.test.ts b/packages/sitecore-jss-proxy/src/middleware/healtchcheck/index.test.ts new file mode 100644 index 0000000000..252b4928d1 --- /dev/null +++ b/packages/sitecore-jss-proxy/src/middleware/healtchcheck/index.test.ts @@ -0,0 +1,15 @@ +import express from 'express'; +import request from 'supertest'; +import { healthCheck } from './index'; + +describe('healthcheck router - /api/healthz', () => { + const app = express(); + + it('should handle request', async () => { + app.use(healthCheck()); + + request(app) + .get('/api/healthz') + .expect(200, 'Healthy'); + }); +}); diff --git a/packages/sitecore-jss-proxy/src/middleware/healtchcheck/index.ts b/packages/sitecore-jss-proxy/src/middleware/healtchcheck/index.ts new file mode 100644 index 0000000000..dff61fb6d2 --- /dev/null +++ b/packages/sitecore-jss-proxy/src/middleware/healtchcheck/index.ts @@ -0,0 +1,14 @@ +import { Router } from 'express'; +import { healthCheckMiddleware } from './healthcheck-middleware'; + +/** + * Creates a router for health check requests. + * @returns {Router} Editing router + */ +export const healthCheck = (): Router => { + const router = Router(); + + router.get('/api/healthz', healthCheckMiddleware()); + + return router; +}; diff --git a/packages/sitecore-jss-proxy/src/middleware/index.ts b/packages/sitecore-jss-proxy/src/middleware/index.ts index b3cce32353..8f8753d652 100644 --- a/packages/sitecore-jss-proxy/src/middleware/index.ts +++ b/packages/sitecore-jss-proxy/src/middleware/index.ts @@ -2,4 +2,4 @@ // eslint-disable-next-line prettier/prettier export * as headlessProxy from './headless-ssr-proxy'; export { editingRouter } from './editing'; -export { healthCheckMiddleware } from './monitoring'; +export { healthCheck } from './healtchcheck'; diff --git a/packages/sitecore-jss-proxy/src/middleware/monitoring/index.ts b/packages/sitecore-jss-proxy/src/middleware/monitoring/index.ts deleted file mode 100644 index eb39226e9a..0000000000 --- a/packages/sitecore-jss-proxy/src/middleware/monitoring/index.ts +++ /dev/null @@ -1 +0,0 @@ -export { healthCheckMiddleware } from './healthcheck-middleware'; From 6e1aa471a6119d090e3be4edd21852c4fe1e9c36 Mon Sep 17 00:00:00 2001 From: yavorsk Date: Thu, 12 Sep 2024 15:19:01 +0300 Subject: [PATCH 5/6] some fefactoring and typo fixes --- .../healtchcheck/healthcheck-middleware.test.ts | 15 --------------- .../healtchcheck/healthcheck-middleware.ts | 8 -------- .../{healtchcheck => healthcheck}/index.test.ts | 0 .../{healtchcheck => healthcheck}/index.ts | 10 ++++++++-- .../sitecore-jss-proxy/src/middleware/index.ts | 2 +- 5 files changed, 9 insertions(+), 26 deletions(-) delete mode 100644 packages/sitecore-jss-proxy/src/middleware/healtchcheck/healthcheck-middleware.test.ts delete mode 100644 packages/sitecore-jss-proxy/src/middleware/healtchcheck/healthcheck-middleware.ts rename packages/sitecore-jss-proxy/src/middleware/{healtchcheck => healthcheck}/index.test.ts (100%) rename packages/sitecore-jss-proxy/src/middleware/{healtchcheck => healthcheck}/index.ts (51%) diff --git a/packages/sitecore-jss-proxy/src/middleware/healtchcheck/healthcheck-middleware.test.ts b/packages/sitecore-jss-proxy/src/middleware/healtchcheck/healthcheck-middleware.test.ts deleted file mode 100644 index 5057e93c06..0000000000 --- a/packages/sitecore-jss-proxy/src/middleware/healtchcheck/healthcheck-middleware.test.ts +++ /dev/null @@ -1,15 +0,0 @@ -import express from 'express'; -import request from 'supertest'; -import { healthCheckMiddleware } from './healthcheck-middleware'; - -describe('healthcheck middleware - /api/healthz', () => { - const app = express(); - - it('should handle request', async () => { - app.get('/api/healthz', healthCheckMiddleware()); - - request(app) - .get('/api/halthz') - .expect(200, 'Healthy'); - }); -}); diff --git a/packages/sitecore-jss-proxy/src/middleware/healtchcheck/healthcheck-middleware.ts b/packages/sitecore-jss-proxy/src/middleware/healtchcheck/healthcheck-middleware.ts deleted file mode 100644 index 59b89fe3f1..0000000000 --- a/packages/sitecore-jss-proxy/src/middleware/healtchcheck/healthcheck-middleware.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { Request, Response } from 'express'; - -/** - * Middleware to handle health check requests - */ -export const healthCheckMiddleware = () => (_req: Request, res: Response): void => { - res.status(200).send('Healthy'); -}; diff --git a/packages/sitecore-jss-proxy/src/middleware/healtchcheck/index.test.ts b/packages/sitecore-jss-proxy/src/middleware/healthcheck/index.test.ts similarity index 100% rename from packages/sitecore-jss-proxy/src/middleware/healtchcheck/index.test.ts rename to packages/sitecore-jss-proxy/src/middleware/healthcheck/index.test.ts diff --git a/packages/sitecore-jss-proxy/src/middleware/healtchcheck/index.ts b/packages/sitecore-jss-proxy/src/middleware/healthcheck/index.ts similarity index 51% rename from packages/sitecore-jss-proxy/src/middleware/healtchcheck/index.ts rename to packages/sitecore-jss-proxy/src/middleware/healthcheck/index.ts index dff61fb6d2..f0b767ac2f 100644 --- a/packages/sitecore-jss-proxy/src/middleware/healtchcheck/index.ts +++ b/packages/sitecore-jss-proxy/src/middleware/healthcheck/index.ts @@ -1,5 +1,11 @@ -import { Router } from 'express'; -import { healthCheckMiddleware } from './healthcheck-middleware'; +import { Router, Request, Response } from 'express'; + +/** + * Middleware to handle health check requests + */ +const healthCheckMiddleware = () => (_req: Request, res: Response): void => { + res.status(200).send('Healthy'); +}; /** * Creates a router for health check requests. diff --git a/packages/sitecore-jss-proxy/src/middleware/index.ts b/packages/sitecore-jss-proxy/src/middleware/index.ts index 8f8753d652..4b614bde3c 100644 --- a/packages/sitecore-jss-proxy/src/middleware/index.ts +++ b/packages/sitecore-jss-proxy/src/middleware/index.ts @@ -2,4 +2,4 @@ // eslint-disable-next-line prettier/prettier export * as headlessProxy from './headless-ssr-proxy'; export { editingRouter } from './editing'; -export { healthCheck } from './healtchcheck'; +export { healthCheck } from './healthcheck'; From efc04d71a80cc05641212af03ac40b8d90c96710 Mon Sep 17 00:00:00 2001 From: yavorsk Date: Thu, 12 Sep 2024 16:48:37 +0300 Subject: [PATCH 6/6] refactoring --- .../src/middleware/healthcheck/index.ts | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/packages/sitecore-jss-proxy/src/middleware/healthcheck/index.ts b/packages/sitecore-jss-proxy/src/middleware/healthcheck/index.ts index f0b767ac2f..2ebbc0e04f 100644 --- a/packages/sitecore-jss-proxy/src/middleware/healthcheck/index.ts +++ b/packages/sitecore-jss-proxy/src/middleware/healthcheck/index.ts @@ -1,12 +1,5 @@ import { Router, Request, Response } from 'express'; -/** - * Middleware to handle health check requests - */ -const healthCheckMiddleware = () => (_req: Request, res: Response): void => { - res.status(200).send('Healthy'); -}; - /** * Creates a router for health check requests. * @returns {Router} Editing router @@ -14,7 +7,9 @@ const healthCheckMiddleware = () => (_req: Request, res: Response): void => { export const healthCheck = (): Router => { const router = Router(); - router.get('/api/healthz', healthCheckMiddleware()); + router.get('/api/healthz', (_req: Request, res: Response) => { + res.status(200).send('Healthy'); + }); return router; };