|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Controller; |
| 6 | + |
| 7 | +use App\Health\HealthChecker; |
| 8 | +use App\Health\HealthStatus; |
| 9 | +use Symfony\Component\HttpFoundation\JsonResponse; |
| 10 | +use Symfony\Component\HttpFoundation\Response; |
| 11 | +use Symfony\Component\Routing\Attribute\Route; |
| 12 | + |
| 13 | +/** |
| 14 | + * Health endpoints, in three tiers. |
| 15 | + * |
| 16 | + * /health/live Public. No dependencies at all. Says only that PHP-FPM is up |
| 17 | + * and routing works. It must never touch the database: a |
| 18 | + * liveness probe that fails during a database outage makes an |
| 19 | + * orchestrator restart a container that is not the problem. |
| 20 | + * |
| 21 | + * /health/ready Public, but opaque. Runs every check and answers 200 or 503 |
| 22 | + * with the aggregated status only. The status code is the |
| 23 | + * payload; which dependency failed is not disclosed. This is |
| 24 | + * the endpoint monitoring should watch. |
| 25 | + * |
| 26 | + * /health/detail Per-check results, timings, queue depth. Discloses internals |
| 27 | + * and MUST be protected at the edge — see the ITKBasicAuth |
| 28 | + * middleware on the nginx service in docker-compose.server.yml. |
| 29 | + * |
| 30 | + * Authentication deliberately happens in Traefik rather than in Symfony. Both |
| 31 | + * user providers in config/packages/security.yaml are Doctrine entity |
| 32 | + * providers, so an application-level firewall on these routes would fail to |
| 33 | + * authenticate during a database outage and answer 500 — precisely when the |
| 34 | + * endpoint needs to answer "the database is down". ^/health is therefore |
| 35 | + * excluded from the Symfony firewalls entirely. |
| 36 | + */ |
| 37 | +readonly class HealthController |
| 38 | +{ |
| 39 | + public function __construct( |
| 40 | + private HealthChecker $healthChecker, |
| 41 | + ) { |
| 42 | + } |
| 43 | + |
| 44 | + #[Route('/health/live', name: 'app_health_live', methods: ['GET'])] |
| 45 | + public function live(): JsonResponse |
| 46 | + { |
| 47 | + return $this->respond(['status' => HealthStatus::Ok->value], true); |
| 48 | + } |
| 49 | + |
| 50 | + #[Route('/health/ready', name: 'app_health_ready', methods: ['GET'])] |
| 51 | + public function ready(): JsonResponse |
| 52 | + { |
| 53 | + $healthy = $this->healthChecker->isHealthy($this->healthChecker->run()); |
| 54 | + |
| 55 | + return $this->respond( |
| 56 | + ['status' => $healthy ? HealthStatus::Ok->value : HealthStatus::Degraded->value], |
| 57 | + $healthy |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + #[Route('/health/detail', name: 'app_health_detail', methods: ['GET'])] |
| 62 | + public function detail(): JsonResponse |
| 63 | + { |
| 64 | + $results = $this->healthChecker->run(); |
| 65 | + $healthy = $this->healthChecker->isHealthy($results); |
| 66 | + |
| 67 | + $checks = []; |
| 68 | + foreach ($results as $result) { |
| 69 | + $checks[$result->name] = array_filter([ |
| 70 | + 'status' => $result->status->value, |
| 71 | + 'message' => $result->message, |
| 72 | + 'details' => $result->details, |
| 73 | + ], static fn (mixed $value): bool => null !== $value && [] !== $value); |
| 74 | + } |
| 75 | + |
| 76 | + return $this->respond([ |
| 77 | + 'status' => $healthy ? HealthStatus::Ok->value : HealthStatus::Degraded->value, |
| 78 | + 'checks' => $checks, |
| 79 | + ], $healthy); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @param array<string, mixed> $payload |
| 84 | + */ |
| 85 | + private function respond(array $payload, bool $healthy): JsonResponse |
| 86 | + { |
| 87 | + $response = new JsonResponse( |
| 88 | + $payload, |
| 89 | + $healthy ? Response::HTTP_OK : Response::HTTP_SERVICE_UNAVAILABLE |
| 90 | + ); |
| 91 | + |
| 92 | + // Health responses are cached inside HealthChecker, never by the client |
| 93 | + // or an intermediary. |
| 94 | + $response->headers->set('Cache-Control', 'no-store, private'); |
| 95 | + |
| 96 | + return $response; |
| 97 | + } |
| 98 | +} |
0 commit comments