Phred/src/Http/Controllers/HealthController.php
Funky Waddle 54303282d7
Some checks failed
CI / PHP ${{ matrix.php }} (8.1) (push) Has been cancelled
CI / PHP ${{ matrix.php }} (8.2) (push) Has been cancelled
CI / PHP ${{ matrix.php }} (8.3) (push) Has been cancelled
Too many things
2026-01-06 11:02:05 -06:00

63 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Phred\Http\Controllers;
use Phred\Http\Contracts\ApiResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use OpenApi\Attributes as OA;
final class HealthController
{
use \Phred\Http\Support\ConditionalRequestTrait;
public function __construct(private ApiResponseFactoryInterface $factory) {}
#[OA\Get(
path: "/_phred/health",
summary: "Check framework health",
tags: ["System"],
responses: [
new OA\Response(
response: 200,
description: "System is healthy",
content: new OA\JsonContent(
properties: [
new OA\Property(property: "ok", type: "boolean"),
new OA\Property(property: "framework", type: "string")
],
type: "object"
)
)
]
)]
public function __invoke(Request $request): ResponseInterface
{
$type = $request->getQueryParams()['type'] ?? 'liveness';
$data = [
'ok' => true,
'framework' => 'Phred',
'type' => $type,
'timestamp' => time(),
];
// Readiness check could include DB connection check, etc.
if ($type === 'readiness') {
// Placeholder for actual checks
$data['checks'] = [
'database' => 'connected',
'storage' => 'writable',
];
}
$etag = $this->generateEtag($data);
if ($this->isFresh($request, $etag)) {
return (new \Nyholm\Psr7\Factory\Psr17Factory())->createResponse(304);
}
return $this->factory->fromArray($data, 200, ['ETag' => $etag]);
}
}