mirror of
https://github.com/Unleash/unleash.git
synced 2025-02-04 00:18:01 +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:
parent
e06459ac79
commit
123991d28f
@ -18,6 +18,7 @@ import { featureTypesSchema } from './spec/feature-types-schema';
|
|||||||
import { featureVariantsSchema } from './spec/feature-variants-schema';
|
import { featureVariantsSchema } from './spec/feature-variants-schema';
|
||||||
import { featuresSchema } from './spec/features-schema';
|
import { featuresSchema } from './spec/features-schema';
|
||||||
import { feedbackSchema } from './spec/feedback-schema';
|
import { feedbackSchema } from './spec/feedback-schema';
|
||||||
|
import { healthCheckSchema } from './spec/health-check-schema';
|
||||||
import { healthOverviewSchema } from './spec/health-overview-schema';
|
import { healthOverviewSchema } from './spec/health-overview-schema';
|
||||||
import { healthReportSchema } from './spec/health-report-schema';
|
import { healthReportSchema } from './spec/health-report-schema';
|
||||||
import { legalValueSchema } from './spec/legal-value-schema';
|
import { legalValueSchema } from './spec/legal-value-schema';
|
||||||
@ -70,6 +71,7 @@ export const schemas = {
|
|||||||
featureVariantsSchema,
|
featureVariantsSchema,
|
||||||
featuresSchema,
|
featuresSchema,
|
||||||
feedbackSchema,
|
feedbackSchema,
|
||||||
|
healthCheckSchema,
|
||||||
healthOverviewSchema,
|
healthOverviewSchema,
|
||||||
healthReportSchema,
|
healthReportSchema,
|
||||||
legalValueSchema,
|
legalValueSchema,
|
||||||
@ -112,17 +114,15 @@ export interface JsonSchemaProps {
|
|||||||
components: object;
|
components: object;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AdminApiOperation
|
interface ApiOperation<Tag = 'client' | 'admin' | 'other'>
|
||||||
extends Omit<OpenAPIV3.OperationObject, 'tags'> {
|
extends Omit<OpenAPIV3.OperationObject, 'tags'> {
|
||||||
operationId: string;
|
operationId: string;
|
||||||
tags: ['admin'];
|
tags: [Tag];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ClientApiOperation
|
export type AdminApiOperation = ApiOperation<'admin'>;
|
||||||
extends Omit<OpenAPIV3.OperationObject, 'tags'> {
|
export type ClientApiOperation = ApiOperation<'client'>;
|
||||||
operationId: string;
|
export type OtherApiOperation = ApiOperation<'other'>;
|
||||||
tags: ['client'];
|
|
||||||
}
|
|
||||||
|
|
||||||
export const createRequestSchema = (
|
export const createRequestSchema = (
|
||||||
schemaName: string,
|
schemaName: string,
|
||||||
|
17
src/lib/openapi/spec/health-check-schema.ts
Normal file
17
src/lib/openapi/spec/health-check-schema.ts
Normal 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>;
|
@ -3,33 +3,60 @@ import { IUnleashConfig } from '../types/option';
|
|||||||
import { IUnleashServices } from '../types/services';
|
import { IUnleashServices } from '../types/services';
|
||||||
import { Logger } from '../logger';
|
import { Logger } from '../logger';
|
||||||
import HealthService from '../services/health-service';
|
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 logger: Logger;
|
||||||
|
|
||||||
|
private openApiService: OpenApiService;
|
||||||
|
|
||||||
private healthService: HealthService;
|
private healthService: HealthService;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
config: IUnleashConfig,
|
config: IUnleashConfig,
|
||||||
{ healthService }: Pick<IUnleashServices, 'healthService'>,
|
{
|
||||||
|
healthService,
|
||||||
|
openApiService,
|
||||||
|
}: Pick<IUnleashServices, 'healthService' | 'openApiService'>,
|
||||||
) {
|
) {
|
||||||
super(config);
|
super(config);
|
||||||
this.logger = config.getLogger('health-check.js');
|
this.logger = config.getLogger('health-check.js');
|
||||||
|
this.openApiService = openApiService;
|
||||||
this.healthService = healthService;
|
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 {
|
try {
|
||||||
await this.healthService.dbIsUp();
|
await this.healthService.dbIsUp();
|
||||||
res.json({ health: 'GOOD' });
|
res.status(200).json({ health: 'GOOD' });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.logger.error('Could not select from features, error was: ', e);
|
this.logger.error('Could not select from features, error was: ', e);
|
||||||
res.status(500).json({ health: 'BAD' });
|
res.status(500).json({ health: 'BAD' });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export default HealthCheckController;
|
|
||||||
module.exports = HealthCheckController;
|
|
||||||
|
@ -10,7 +10,7 @@ import LogoutController from './logout';
|
|||||||
const AdminApi = require('./admin-api');
|
const AdminApi = require('./admin-api');
|
||||||
const ClientApi = require('./client-api');
|
const ClientApi = require('./client-api');
|
||||||
const Controller = require('./controller');
|
const Controller = require('./controller');
|
||||||
const HealthCheckController = require('./health-check');
|
import { HealthCheckController } from './health-check';
|
||||||
class IndexRouter extends Controller {
|
class IndexRouter extends Controller {
|
||||||
constructor(config: IUnleashConfig, services: IUnleashServices) {
|
constructor(config: IUnleashConfig, services: IUnleashServices) {
|
||||||
super(config);
|
super(config);
|
||||||
|
@ -4,6 +4,7 @@ import { IUnleashConfig } from '../types/option';
|
|||||||
import {
|
import {
|
||||||
AdminApiOperation,
|
AdminApiOperation,
|
||||||
ClientApiOperation,
|
ClientApiOperation,
|
||||||
|
OtherApiOperation,
|
||||||
createOpenApiSchema,
|
createOpenApiSchema,
|
||||||
JsonSchemaProps,
|
JsonSchemaProps,
|
||||||
removeJsonSchemaProps,
|
removeJsonSchemaProps,
|
||||||
@ -30,7 +31,9 @@ export class OpenApiService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
validPath(op: AdminApiOperation | ClientApiOperation): RequestHandler {
|
validPath(
|
||||||
|
op: AdminApiOperation | ClientApiOperation | OtherApiOperation,
|
||||||
|
): RequestHandler {
|
||||||
return this.api.validPath(op);
|
return this.api.validPath(op);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -580,6 +580,22 @@ Object {
|
|||||||
"required": Array [],
|
"required": Array [],
|
||||||
"type": "object",
|
"type": "object",
|
||||||
},
|
},
|
||||||
|
"healthCheckSchema": Object {
|
||||||
|
"additionalProperties": false,
|
||||||
|
"properties": Object {
|
||||||
|
"health": Object {
|
||||||
|
"enum": Array [
|
||||||
|
"GOOD",
|
||||||
|
"BAD",
|
||||||
|
],
|
||||||
|
"type": "string",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"required": Array [
|
||||||
|
"health",
|
||||||
|
],
|
||||||
|
"type": "object",
|
||||||
|
},
|
||||||
"healthOverviewSchema": Object {
|
"healthOverviewSchema": Object {
|
||||||
"additionalProperties": false,
|
"additionalProperties": false,
|
||||||
"properties": Object {
|
"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 [
|
"security": Array [
|
||||||
Object {
|
Object {
|
||||||
|
Loading…
Reference in New Issue
Block a user