1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00

refactor: add OpenAPI schema to health-check controller (#1732)

* refactor: add OpenAPI schema to health-check controller

* refactor: address PR comments

* add type to health-check-schema

* fix: update snap
This commit is contained in:
Nuno Góis 2022-06-20 11:22:41 +01:00 committed by GitHub
parent e06459ac79
commit 123991d28f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 110 additions and 17 deletions

View File

@ -18,6 +18,7 @@ import { featureTypesSchema } from './spec/feature-types-schema';
import { featureVariantsSchema } from './spec/feature-variants-schema';
import { featuresSchema } from './spec/features-schema';
import { feedbackSchema } from './spec/feedback-schema';
import { healthCheckSchema } from './spec/health-check-schema';
import { healthOverviewSchema } from './spec/health-overview-schema';
import { healthReportSchema } from './spec/health-report-schema';
import { legalValueSchema } from './spec/legal-value-schema';
@ -70,6 +71,7 @@ export const schemas = {
featureVariantsSchema,
featuresSchema,
feedbackSchema,
healthCheckSchema,
healthOverviewSchema,
healthReportSchema,
legalValueSchema,
@ -112,17 +114,15 @@ export interface JsonSchemaProps {
components: object;
}
export interface AdminApiOperation
interface ApiOperation<Tag = 'client' | 'admin' | 'other'>
extends Omit<OpenAPIV3.OperationObject, 'tags'> {
operationId: string;
tags: ['admin'];
tags: [Tag];
}
export interface ClientApiOperation
extends Omit<OpenAPIV3.OperationObject, 'tags'> {
operationId: string;
tags: ['client'];
}
export type AdminApiOperation = ApiOperation<'admin'>;
export type ClientApiOperation = ApiOperation<'client'>;
export type OtherApiOperation = ApiOperation<'other'>;
export const createRequestSchema = (
schemaName: string,

View File

@ -0,0 +1,17 @@
import { FromSchema } from 'json-schema-to-ts';
export const healthCheckSchema = {
$id: '#/components/schemas/healthCheckSchema',
type: 'object',
additionalProperties: false,
required: ['health'],
properties: {
health: {
type: 'string',
enum: ['GOOD', 'BAD'],
},
},
components: {},
} as const;
export type HealthCheckSchema = FromSchema<typeof healthCheckSchema>;

View File

@ -3,33 +3,60 @@ import { IUnleashConfig } from '../types/option';
import { IUnleashServices } from '../types/services';
import { Logger } from '../logger';
import HealthService from '../services/health-service';
import { OpenApiService } from '../services/openapi-service';
const Controller = require('./controller');
import Controller from './controller';
import { NONE } from '../types/permissions';
import { createResponseSchema } from '../openapi';
import { HealthCheckSchema } from '../openapi/spec/health-check-schema';
class HealthCheckController extends Controller {
export class HealthCheckController extends Controller {
private logger: Logger;
private openApiService: OpenApiService;
private healthService: HealthService;
constructor(
config: IUnleashConfig,
{ healthService }: Pick<IUnleashServices, 'healthService'>,
{
healthService,
openApiService,
}: Pick<IUnleashServices, 'healthService' | 'openApiService'>,
) {
super(config);
this.logger = config.getLogger('health-check.js');
this.openApiService = openApiService;
this.healthService = healthService;
this.get('/', (req, res) => this.index(req, res));
this.route({
method: 'get',
path: '',
handler: this.getHealth,
permission: NONE,
middleware: [
openApiService.validPath({
tags: ['other'],
operationId: 'getHealth',
responses: {
200: createResponseSchema('healthCheckSchema'),
500: createResponseSchema('healthCheckSchema'),
},
}),
],
});
}
async index(req: Request, res: Response): Promise<void> {
async getHealth(
_: Request,
res: Response<HealthCheckSchema>,
): Promise<void> {
try {
await this.healthService.dbIsUp();
res.json({ health: 'GOOD' });
res.status(200).json({ health: 'GOOD' });
} catch (e) {
this.logger.error('Could not select from features, error was: ', e);
res.status(500).json({ health: 'BAD' });
}
}
}
export default HealthCheckController;
module.exports = HealthCheckController;

View File

@ -10,7 +10,7 @@ import LogoutController from './logout';
const AdminApi = require('./admin-api');
const ClientApi = require('./client-api');
const Controller = require('./controller');
const HealthCheckController = require('./health-check');
import { HealthCheckController } from './health-check';
class IndexRouter extends Controller {
constructor(config: IUnleashConfig, services: IUnleashServices) {
super(config);

View File

@ -4,6 +4,7 @@ import { IUnleashConfig } from '../types/option';
import {
AdminApiOperation,
ClientApiOperation,
OtherApiOperation,
createOpenApiSchema,
JsonSchemaProps,
removeJsonSchemaProps,
@ -30,7 +31,9 @@ export class OpenApiService {
);
}
validPath(op: AdminApiOperation | ClientApiOperation): RequestHandler {
validPath(
op: AdminApiOperation | ClientApiOperation | OtherApiOperation,
): RequestHandler {
return this.api.validPath(op);
}

View File

@ -580,6 +580,22 @@ Object {
"required": Array [],
"type": "object",
},
"healthCheckSchema": Object {
"additionalProperties": false,
"properties": Object {
"health": Object {
"enum": Array [
"GOOD",
"BAD",
],
"type": "string",
},
},
"required": Array [
"health",
],
"type": "object",
},
"healthOverviewSchema": Object {
"additionalProperties": false,
"properties": Object {
@ -3212,6 +3228,36 @@ Object {
],
},
},
"/health": Object {
"get": Object {
"operationId": "getHealth",
"responses": Object {
"200": Object {
"content": Object {
"application/json": Object {
"schema": Object {
"$ref": "#/components/schemas/healthCheckSchema",
},
},
},
"description": "healthCheckSchema",
},
"500": Object {
"content": Object {
"application/json": Object {
"schema": Object {
"$ref": "#/components/schemas/healthCheckSchema",
},
},
},
"description": "healthCheckSchema",
},
},
"tags": Array [
"other",
],
},
},
},
"security": Array [
Object {