Bringing local and remote repository in line with one another and adding Github Actions to publish package
This commit is contained in:
parent
28db152b87
commit
74997cb676
8 changed files with 479 additions and 0 deletions
49
src/middlewares/HealthCheckMiddleware.ts
Normal file
49
src/middlewares/HealthCheckMiddleware.ts
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
import { Request, Response, NextFunction, RequestHandler } from 'express';
|
||||
|
||||
export enum HealthCheckStatus {
|
||||
OK = 'ok',
|
||||
ERROR = 'error'
|
||||
}
|
||||
|
||||
export class HealthCheckMiddleware {
|
||||
public static readonly HEALTH_CHECK_ENDPOINT = '/.well-known/health-check';
|
||||
|
||||
private static status: HealthCheckStatus;
|
||||
|
||||
constructor(initialStatus: HealthCheckStatus = HealthCheckStatus.OK) {
|
||||
HealthCheckMiddleware.status = initialStatus;
|
||||
}
|
||||
|
||||
static setStatus(status: HealthCheckStatus) {
|
||||
HealthCheckMiddleware.status = status;
|
||||
}
|
||||
|
||||
middleware(req: Request, res: Response, next: NextFunction) {
|
||||
if(req.path === HealthCheckMiddleware.HEALTH_CHECK_ENDPOINT) {
|
||||
switch(HealthCheckMiddleware.status) {
|
||||
case HealthCheckStatus.OK:
|
||||
return res.status(200).setHeader('Content-Type', 'application/health+json').json({ status: 'ok' });
|
||||
break;
|
||||
case HealthCheckStatus.ERROR:
|
||||
return res.status(500).setHeader('Content-Type', 'application/health+json').json({ status: 'error' });
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
next();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Middleware function to create a health check endpoint.
|
||||
*
|
||||
* This attempts to comply with the [Health Check Response Format for HTTP APIs](https://datatracker.ietf.org/doc/html/draft-inadarei-api-health-check-06) proposed standard.
|
||||
*
|
||||
* @param initialStatus The initial status of the health check.
|
||||
* @returns The middleware function.
|
||||
*/
|
||||
export function healthCheckMiddleware(initialStatus: HealthCheckStatus = HealthCheckStatus.OK) {
|
||||
const instance = new HealthCheckMiddleware(initialStatus);
|
||||
|
||||
return instance.middleware.bind(instance);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue