From e922b14419ba100dcefcb9ccf658b59b1a202f96 Mon Sep 17 00:00:00 2001 From: Matthieu Date: Fri, 6 Mar 2026 09:51:09 +0100 Subject: [PATCH] feat(api) : add /api/health endpoint for monitoring - Returns status, version, timestamp, PHP version, DB latency and memory usage - Accessible without authentication (PUBLIC_ACCESS) - Returns 200 when healthy, 503 when degraded (DB down) --- config/packages/security.yaml | 1 + src/Controller/HealthCheckController.php | 53 ++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 src/Controller/HealthCheckController.php diff --git a/config/packages/security.yaml b/config/packages/security.yaml index ae8772b..1753232 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -55,6 +55,7 @@ security: - { path: ^/api/admin, roles: ROLE_ADMIN } - { path: ^/api/docs, roles: PUBLIC_ACCESS } - { path: ^/api/test, roles: PUBLIC_ACCESS } + - { path: ^/api/health$, roles: PUBLIC_ACCESS } - { path: ^/docs, roles: PUBLIC_ACCESS } - { path: ^/contexts, roles: PUBLIC_ACCESS } - { path: ^/\.well-known, roles: PUBLIC_ACCESS } diff --git a/src/Controller/HealthCheckController.php b/src/Controller/HealthCheckController.php new file mode 100644 index 0000000..bae034c --- /dev/null +++ b/src/Controller/HealthCheckController.php @@ -0,0 +1,53 @@ +getParameter('kernel.project_dir').'/VERSION'; + if (file_exists($versionFile)) { + $version = trim(file_get_contents($versionFile)); + } + + $dbOk = false; + $dbLatency = null; + + try { + $start = hrtime(true); + $connection->executeQuery('SELECT 1'); + $dbLatency = round((hrtime(true) - $start) / 1e6, 1); + $dbOk = true; + } catch (Throwable) { + } + + $healthy = $dbOk; + + return $this->json([ + 'status' => $healthy ? 'ok' : 'degraded', + 'version' => $version, + 'timestamp' => new DateTimeImmutable()->format(DateTimeInterface::ATOM), + 'php' => PHP_VERSION, + 'checks' => [ + 'database' => [ + 'status' => $dbOk ? 'ok' : 'down', + 'latency_ms' => $dbLatency, + ], + ], + 'memory_mb' => round(memory_get_usage(true) / 1024 / 1024, 1), + ], $healthy ? 200 : 503); + } +}