55 lines
1.6 KiB
JavaScript
55 lines
1.6 KiB
JavaScript
import { ensureConnection } from '../../db/store.js';
|
|
|
|
/**
|
|
* Registers a database connectivity health check route.
|
|
* @param {import('express').Application} app - Express app
|
|
* @param {string} [basePath=''] - Optional URL prefix for all routes in this module
|
|
*/
|
|
export function register(app, basePath = '') {
|
|
/**
|
|
* @openapi
|
|
* /health:
|
|
* get:
|
|
* tags: [Health]
|
|
* summary: Database health check
|
|
* description: Verifies the database client can establish a connection.
|
|
* security: []
|
|
* x-markdown: health.md
|
|
* responses:
|
|
* 200:
|
|
* description: Database reachable
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* ok:
|
|
* type: boolean
|
|
* example: true
|
|
* 503:
|
|
* description: Database unreachable
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* ok:
|
|
* type: boolean
|
|
* example: false
|
|
* error:
|
|
* type: string
|
|
*/
|
|
// #region GET /health
|
|
|
|
app.get(`${basePath}/health`, async (_request, response) => {
|
|
try {
|
|
await ensureConnection();
|
|
response.status(200).json({ ok: true });
|
|
} catch (error) {
|
|
response.status(503).json({ ok: false, error: error.message });
|
|
}
|
|
});
|
|
|
|
// #endregion
|
|
}
|