gacrux-lib-healthcheck
v1.0.0
Published
adds healthchecks to the service
Downloads
1
Readme
lib-healthcheck
adds healthchecks to the service
features
- adds healthcheck routes
- simple adding of healthchecks
install
npm install inversify reflect-metadata gacrux-lib-restify gacrux-lib-healthcheck --save
Usage
Step 1: Create a Healthcheck
First of all, we need to create a health check. For this, we create a new class that implements the interface BaseHealthcheck
.
Next up we need the injectable
decorator from inversify. Because we want to name our health check we import the Healthcheck
decorator from this library. This decorator requires a string. This string is used to name the Healthcheck.
It is also possible to inject some o
import { BaseHealthcheck, Healthcheck } from 'gacrux-lib-healthcheck';
import { injectable } from 'inversify';
import { MyService } from './services/myservice';
@injectable()
@Healthcheck('NameOfTheHealthcheck')
export class MyHealthcheck implements BaseHealthcheck {
// you can inject other stuff here
constructor(
@inject('MyService') private service: MyService
) { }
// this is called by the library
public execute(): Promise<boolean> {
// do something with service
return new Promise<boolean>((resolve) => resolve(true));
}
}
Step 2: Add the health check
For this, we create an instance if HealthcheckRouter
. The constructor requires an inversify container.
After creating that instance you are free to add new health checks. Each time the api is called it will check the whole container for new health checks.
IMPORTANT: Booting up restify MUST be the last thing you do, if not it can be that not all controller are added.
import { Container, injectable, interfaces } from 'inversify';
import { HealthcheckRouter } from 'gacrux-lib-healthcheck';
import { RestifyServer } from 'gacrux-lib-restify';
const container: interfaces.Container = new Container();
// create an instance of the router
const healthcheck: HealthcheckRouter = new HealthcheckRouter(container);
// the needed controller and services get added to the container
const server = new RestifyServer(container).build().listen(1337);
}
// you don´t need to create an instance of the class
health check.addHealthcheck(MyHealthcheck);