From 8b875f7ddea667675608f24e68d94efe5bd2718b Mon Sep 17 00:00:00 2001 From: Abdou TOP Date: Thu, 29 Jan 2026 12:53:04 +0000 Subject: [PATCH] feat(health): implement health check route with status, uptime, and version --- api/health.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ api/router.ts | 5 +++++ 2 files changed, 47 insertions(+) create mode 100644 api/health.ts diff --git a/api/health.ts b/api/health.ts new file mode 100644 index 0000000..4c9fa4a --- /dev/null +++ b/api/health.ts @@ -0,0 +1,42 @@ +import { NUM, OBJ, STR } from '@01edu/api/validator' +import { route } from '@01edu/api/router' +import { now, startTime } from '@01edu/time' +import { CI_COMMIT_SHA } from '@01edu/api/env' + +/** + * Creates a health check route that returns the application's status, uptime, version, and instance ID. + * + * @example + * ```ts + * import { makeRouter, route } from '@01edu/router'; + * import { createHealthRoute } from '@01edu/api/health'; + * + * const routes = { + * 'GET/api/health': createHealthRoute(), + * }; + * + * const router = makeRouter(routes,{}); + * ``` + * + * @returns A route handler for the health check endpoint. + */ +export const createHealthRoute = () => { + return route({ + output: OBJ({ + status: STR(), + timestamp: STR(), + uptime: NUM(), + version: STR(), + instanceId: STR(), + }), + fn: () => { + return { + status: 'ok', + timestamp: new Date().toISOString(), + uptime: now() - startTime, + version: CI_COMMIT_SHA || 'unknown', + instanceId: startTime.toString(), + } + }, + }) +} diff --git a/api/router.ts b/api/router.ts index 583f4b2..7043519 100644 --- a/api/router.ts +++ b/api/router.ts @@ -25,6 +25,7 @@ import { respond, ResponseError } from './response.ts' import type { Sql } from '@01edu/types/db' import { createSqlDevRoute } from './dev.ts' import { createDocRoute } from './doc.ts' +import { createHealthRoute } from './health.ts' /** * Options for configuring the router. @@ -134,6 +135,10 @@ export const makeRouter = ( defs['GET/api/doc'] = createDocRoute(defs) } + if (!defs['GET/api/health']) { + defs['GET/api/health'] = createHealthRoute() + } + for (const key in defs) { const slashIndex = key.indexOf('/') const method = key.slice(0, slashIndex) as HttpMethod