import { securityAccess } from '@activepieces/server-shared'
import { GetSystemHealthChecksResponse, PrincipalType } from '@activepieces/shared'
import { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox'
import { StatusCodes } from 'http-status-codes'
import { healthStatusService } from './health.service'
export const healthModule: FastifyPluginAsyncTypebox = async (app) => {
await app.register(healthController, { prefix: '/v1/health' })
}
const healthController: FastifyPluginAsyncTypebox = async (app) => {
app.get(
'/',
{
config: {
security: securityAccess.public(),
},
},
async (_request, reply) => {
const isHealthy = healthStatusService(app.log).isHealthy()
if (!isHealthy) {
await reply.status(StatusCodes.SERVICE_UNAVAILABLE).send({ status: 'Unhealthy' })
return
}
await reply.status(StatusCodes.OK).send({ status: 'Healthy' })
},
),
app.get('/system', GetSystemHealthChecks, async (_request, reply) => {
await reply.status(StatusCodes.OK).send(await healthStatusService(app.log).getSystemHealthChecks())
})
}
const GetSystemHealthChecks = {
config: {
security: securityAccess.platformAdminOnly([PrincipalType.USER]),
},
response: {
200: {
description: 'System health checks',
type: GetSystemHealthChecksResponse,
},
},
}