Skip to content

Commit

Permalink
feat: add /healthz endpoint to httpServer resolve #69
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianwessel committed Mar 29, 2023
1 parent 86175d6 commit 3b28862
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
15 changes: 15 additions & 0 deletions packages/httpserver/src/HttpServerService.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,21 @@ export class HttpServerService extends Service<HttpServerConfig> {
reply.send(new UnhandledError().getErrorResponse())
}
})

if (this.config.enableHealthz) {
this.server.get('/healthz', async (_request, reply) => {
const isEventBridgeReady = await this.eventBridge.isReady()

reply.header('content-type', 'application/json; charset=utf-8')
if (isEventBridgeReady) {
reply.status(StatusCode.OK)
reply.send(new HandledError(StatusCode.OK).getErrorResponse())
return
}
reply.status(StatusCode.InternalServerError)
reply.send(new HandledError(StatusCode.InternalServerError).getErrorResponse())
})
}
}

addBeforeResponse(method: HTTPMethods, pattern: string, handler: BeforeResponseHook) {
Expand Down
1 change: 1 addition & 0 deletions packages/httpserver/src/config/getDefaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const getDefaultConfig = (): HttpServerConfig => {
domain: 'localhost',
enableHelmet: true,
enableCompress: true,
enableHealthz: true,
enableCors: false,
host: '',
logLevel: 'warn',
Expand Down
38 changes: 36 additions & 2 deletions packages/httpserver/src/routes/openapi/getOpenApiJson.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const getOpenApiJson = function (this: HttpServerService) {
const security = this.config.openApi?.security
const externalDocs = this.config.openApi?.externalDocs
const tags = this.config.openApi?.tags
const isHealthzEnabled = this.config.enableHealthz

const logger = this.logger.getChildLogger({
serviceName: this.info.serviceName,
Expand All @@ -51,6 +52,15 @@ export const getOpenApiJson = function (this: HttpServerService) {
securitySchema = Object.keys(components.securitySchemes).map((name) => ({ [name]: [] }))
}

const getErrorName = (code: StatusCode) =>
StatusCode[code]
.replace(/[A-Z]/g, (letter) => ` ${letter}`)
.replace(/^./, (str) => {
return str.toUpperCase()
})
.trim()
.replace(/^O K$/g, 'OK')

const getErrorResponseSchema = (code: StatusCode, message: string, schema?: SchemaObject) => {
return {
type: 'object',
Expand Down Expand Up @@ -225,8 +235,6 @@ export const getOpenApiJson = function (this: HttpServerService) {

const errorResponses: Record<number, unknown> = {}

const getErrorName = (code: StatusCode) => StatusCode[code].replace(/[A-Z]/g, (letter) => ` ${letter}`)

if (definition.openApi?.inputPayload) {
errorResponses[400] = {
description: getErrorName(400),
Expand Down Expand Up @@ -306,6 +314,32 @@ export const getOpenApiJson = function (this: HttpServerService) {
}
})

if (isHealthzEnabled) {
json.paths['/healthz'] = {
get: {
description: 'indicates if the http server service is healthy',
responses: {
200: {
description: 'http server service is up and running and successfully connected to event bridge',
content: {
'application/json': {
schema: getErrorResponseSchema(200, getErrorName(200)),
},
},
},
500: {
description: 'http server service is not up and running or not successfully connected to event bridge',
content: {
'application/json': {
schema: getErrorResponseSchema(500, getErrorName(500)),
},
},
},
},
},
}
}

reply.header('content-type', 'application/json; charset=utf-8')
return json
}
Expand Down
1 change: 1 addition & 0 deletions packages/httpserver/src/types/HttpServerConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type HttpServerConfig = {
cookieSecret?: string
apiMountPath?: string
enableHelmet?: boolean
enableHealthz?: boolean
helmetOptions?: FastifyHelmetOptions
enableCompress?: boolean
compressOptions?: FastifyCompressOptions
Expand Down

0 comments on commit 3b28862

Please sign in to comment.