2021-04-22 10:07:10 +02:00
|
|
|
import { Request, Response } from 'express';
|
|
|
|
import { IUnleashConfig } from '../types/option';
|
|
|
|
import { IUnleashServices } from '../types/services';
|
|
|
|
import { Logger } from '../logger';
|
|
|
|
import HealthService from '../services/health-service';
|
2022-06-20 12:22:41 +02:00
|
|
|
import { OpenApiService } from '../services/openapi-service';
|
2021-04-22 10:07:10 +02:00
|
|
|
|
2022-06-20 12:22:41 +02:00
|
|
|
import Controller from './controller';
|
|
|
|
import { NONE } from '../types/permissions';
|
2022-07-01 08:06:33 +02:00
|
|
|
import { createResponseSchema } from '../openapi/util/create-response-schema';
|
2022-06-20 12:22:41 +02:00
|
|
|
import { HealthCheckSchema } from '../openapi/spec/health-check-schema';
|
2021-04-22 10:07:10 +02:00
|
|
|
|
2022-06-20 12:22:41 +02:00
|
|
|
export class HealthCheckController extends Controller {
|
2021-04-22 10:07:10 +02:00
|
|
|
private logger: Logger;
|
|
|
|
|
2022-06-20 12:22:41 +02:00
|
|
|
private openApiService: OpenApiService;
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
private healthService: HealthService;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
config: IUnleashConfig,
|
2022-06-20 12:22:41 +02:00
|
|
|
{
|
|
|
|
healthService,
|
|
|
|
openApiService,
|
|
|
|
}: Pick<IUnleashServices, 'healthService' | 'openApiService'>,
|
2021-04-22 10:07:10 +02:00
|
|
|
) {
|
2021-08-13 10:36:19 +02:00
|
|
|
super(config);
|
2021-04-22 10:07:10 +02:00
|
|
|
this.logger = config.getLogger('health-check.js');
|
2022-06-20 12:22:41 +02:00
|
|
|
this.openApiService = openApiService;
|
2021-04-22 10:07:10 +02:00
|
|
|
this.healthService = healthService;
|
2022-06-20 12:22:41 +02:00
|
|
|
|
|
|
|
this.route({
|
|
|
|
method: 'get',
|
|
|
|
path: '',
|
|
|
|
handler: this.getHealth,
|
|
|
|
permission: NONE,
|
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
|
|
|
tags: ['other'],
|
|
|
|
operationId: 'getHealth',
|
|
|
|
responses: {
|
|
|
|
200: createResponseSchema('healthCheckSchema'),
|
|
|
|
500: createResponseSchema('healthCheckSchema'),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
2021-04-22 10:07:10 +02:00
|
|
|
}
|
|
|
|
|
2022-06-20 12:22:41 +02:00
|
|
|
async getHealth(
|
|
|
|
_: Request,
|
|
|
|
res: Response<HealthCheckSchema>,
|
|
|
|
): Promise<void> {
|
2021-04-22 10:07:10 +02:00
|
|
|
try {
|
|
|
|
await this.healthService.dbIsUp();
|
2022-06-20 12:22:41 +02:00
|
|
|
res.status(200).json({ health: 'GOOD' });
|
2021-04-22 10:07:10 +02:00
|
|
|
} catch (e) {
|
|
|
|
this.logger.error('Could not select from features, error was: ', e);
|
|
|
|
res.status(500).json({ health: 'BAD' });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|