Skip to content

Commit

Permalink
feat: create health checks controller when configuring health checks
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jun 16, 2024
1 parent 75b0cf9 commit f494c52
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions commands/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ export default class Configure extends BaseCommand {
flags: this.parsed.flags,
entity: this.app.generators.createEntity('health'),
})
await codemods.makeUsingStub(stubsRoot, 'make/health/controller.stub', {
flags: this.parsed.flags,
entity: this.app.generators.createEntity('health_checks'),
})
}

/**
Expand Down
21 changes: 21 additions & 0 deletions stubs/make/health/controller.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{{#var controllerName = generators.controllerName(entity.name, false)}}
{{#var controllerFileName = generators.controllerFileName(entity.name, false)}}
{{{
exports({
to: app.httpControllersPath(entity.path, controllerFileName)
})
}}}
import { healthChecks } from '#start/health'
import type { HttpContext } from '@adonisjs/core/http'

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

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

return response.serviceUnavailable(report)
}
}
9 changes: 9 additions & 0 deletions tests/commands/configure.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,12 +432,21 @@ test.group('Configure command | health checks', (group) => {
message: 'green(DONE:) create start/health.ts',
stream: 'stdout',
},
{
message: 'green(DONE:) create app/controllers/health_checks_controller.ts',
stream: 'stdout',
},
])

await assert.fileContains('start/health.ts', [
'new DiskSpaceHealthCheck()',
'new MemoryHeapHealthCheck()',
'export const healthChecks = ',
])
await assert.fileContains('app/controllers/health_checks_controller.ts', [
`import { healthChecks } from '#start/health'`,
'const report = await healthChecks.run()',
'export default class HealthChecksController',
])
})
})

0 comments on commit f494c52

Please sign in to comment.