Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[templates/nextjs] Add healthz check #1207

Merged
merged 5 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ const nextConfig = {
source: '/layouts/system/:path*',
destination: `${jssConfig.sitecoreApiHost}/layouts/system/:path*`,
},
// healthz check
{
source: '/healthz',
destination: '/api/healthz',
}
];
},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { HealthcheckMiddleware } from '@sitecore-jss/sitecore-jss-nextjs/monitoring';

/**
* This Next.js API route is used to handle healthz check request.
* By default this is used only by Sitecore XM Cloud (when running as editing host),
* but could be used in other deployment scenarios.
*/

// Wire up the HealthcheckMiddleware handler
const handler = new HealthcheckMiddleware().getHandler();

export default handler;
1 change: 1 addition & 0 deletions packages/sitecore-jss-nextjs/monitoring.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './types/monitoring/index';
1 change: 1 addition & 0 deletions packages/sitecore-jss-nextjs/monitoring.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./dist/cjs/monitoring/index');
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { expect, use } from 'chai';
import { NextApiRequest, NextApiResponse } from 'next';
import { spy } from 'sinon';
import sinonChai from 'sinon-chai';
import { HealthcheckMiddleware } from './healthcheck-middleware';

use(sinonChai);

const mockRequest = () => {
return {
method: 'GET',
} as NextApiRequest;
};

const mockResponse = () => {
const res = {} as NextApiResponse;
res.status = spy(() => {
return res;
});
res.send = spy(() => {
return res;
});
return res;
};

describe('HealthcheckMiddleware', () => {
it('should handle request', async () => {
const req = mockRequest();
const res = mockResponse();

const middleware = new HealthcheckMiddleware();
const handler = middleware.getHandler();

await handler(req, res);

expect(res.status).to.have.been.calledOnceWith(200);
expect(res.send).to.have.been.calledOnceWith('Healthy');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { NextApiResponse, NextApiRequest } from 'next';

/**
* Middleware / handler for use in healthcheck Next.js API route (e.g. '/api/healthz').
*/
export class HealthcheckMiddleware {
/**
* Gets the Next.js API route handler
* @returns route handler
*/
public getHandler(): (req: NextApiRequest, res: NextApiResponse) => Promise<void> {
return this.handler;
}

private handler = async (_req: NextApiRequest, res: NextApiResponse): Promise<void> => {
res.status(200).send('Healthy');
};
}
1 change: 1 addition & 0 deletions packages/sitecore-jss-nextjs/src/monitoring/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { HealthcheckMiddleware } from './healthcheck-middleware';
1 change: 1 addition & 0 deletions packages/sitecore-jss-nextjs/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"dist",
"middleware.d.ts",
"editing.d.ts",
"monitoring.d.ts",
"src/tests/*",
"src/test-data/*",
"**/*.test.ts",
Expand Down