Skip to content

Commit

Permalink
feat: health check
Browse files Browse the repository at this point in the history
  • Loading branch information
hugoponthieu committed Dec 4, 2024
1 parent 430ae37 commit 1cb8f80
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 8 deletions.
2 changes: 1 addition & 1 deletion healthcheck/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ RUN npm ci
FROM base AS production-deps
WORKDIR /app
ADD package.json package-lock.json ./
RUN npm ci --omit=dev
RUN npm ci

# Build stage
FROM base AS build
Expand Down
2 changes: 1 addition & 1 deletion healthcheck/adonisrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default defineConfig({
},
() => import('@adonisjs/core/providers/vinejs_provider'),
() => import('@adonisjs/cors/cors_provider'),
() => import('@adonisjs/lucid/database_provider')
() => import('@adonisjs/lucid/database_provider'),
],

/*
Expand Down
14 changes: 14 additions & 0 deletions healthcheck/app/controllers/health_checks_controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { healthChecks } from '#start/health'
import type { HttpContext } from '@adonisjs/core/http'

export default class HealthChecksController {
async handle({ response }: HttpContext) {
const report = await healthChecks.run()

if (report.isHealthy) {
return response.ok(report)
}

return response.serviceUnavailable(report)
}
}
10 changes: 6 additions & 4 deletions healthcheck/app/controllers/healthchecks_controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { healthChecks } from '#start/health'
import type { HttpContext } from '@adonisjs/core/http'

export default class HealthchecksController {
index({ response }: HttpContext) {
return response.status(200).json({
status: 'ok',
})
async index({ response }: HttpContext) {
const health = await healthChecks.run()
const check = health.checks[0]
check.status
return response.send(health.checks)
}
}
2 changes: 1 addition & 1 deletion healthcheck/start/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ export default await Env.create(new URL('../', import.meta.url), {
DB_PORT: Env.schema.number(),
DB_USER: Env.schema.string(),
DB_PASSWORD: Env.schema.string.optional(),
DB_DATABASE: Env.schema.string()
DB_DATABASE: Env.schema.string(),
})
5 changes: 5 additions & 0 deletions healthcheck/start/health.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { HealthChecks } from '@adonisjs/core/health'
import { DbCheck } from '@adonisjs/lucid/database'
import db from '@adonisjs/lucid/services/db'

export const healthChecks = new HealthChecks().register([new DbCheck(db.connection())])
2 changes: 1 addition & 1 deletion healthcheck/start/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ router.get('/', async () => {
}
})

router.get('/healthcheck', [HealthchecksController, 'index'])
router.get('/health', [HealthchecksController, 'index'])

0 comments on commit 1cb8f80

Please sign in to comment.