sfmok / health-check-provider

Provides structure for healthcheck endpoints in accordance with IETF's healthcheck draft RFC

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ostrolucky/health-check-provider

Provides structure for healthcheck endpoints in accordance with IETF's healthcheck draft RFC

About

Package provides endpoints which conform to draft 06 version of IETF's healthcheck RFC.

Installation

Install this package as a dependency using Composer.

composer require ostrolucky/health-check-provider

Usage

This library provides PSR-15 HTTP Server Request Handler, which guarantees compatibility with wide range of PHP frameworks. In case your framework does not natively support it, you can find a PSR bridge which supports it.

Example controller for Symfony framework

For this example, on top of standard symfony packages, you also need php-http/discovery and symfony/psr-http-message-bridge packages.

use Doctrine\DBAL\Connection;
use GuzzleHttp\Psr7\HttpFactory;
use Ostrolucky\HealthCheckProvider\DTO\CheckDetails;
use Ostrolucky\HealthCheckProvider\DTO\HealthResponse;
use Ostrolucky\HealthCheckProvider\HealthChecker\CallableHealthChecker;
use Ostrolucky\HealthCheckProvider\HealthChecker\DoctrineConnectionHealthChecker;
use Ostrolucky\HealthCheckProvider\HealthChecker\HttpHealthChecker;
use Ostrolucky\HealthCheckProvider\RequestHandler;
use Psr\Http\Client\ClientInterface;
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\Transport\Receiver\MessageCountAwareInterface;
use Symfony\Component\Messenger\Transport\TransportInterface;
use Symfony\Component\Routing\Annotation\Route;

class GetHealthCheckController extends AbstractController
{
    public function __construct(
        #[Autowire(service: 'messenger.transport.amqp_dc_user_update')]
        private MessageCountAwareInterface&TransportInterface $transport,
        private Connection $connection,
        private ClientInterface $httpClient,
    ) {}

    #[Route(path: '/api/health_check')]
    public function __invoke(Request $request): Response
    {
        $psr17Factory = new HttpFactory();
        $psrBridge = new HttpFoundationFactory();

        return $psrBridge->createResponse(
            (new RequestHandler(
                new HealthResponse(),
                [
                    new CallableHealthChecker(new CheckDetails('AMQP'), fn () => $this->transport->getMessageCount()),
                    new DoctrineConnectionHealthChecker(new CheckDetails('Database'), $this->connection),
                    new HttpHealthChecker(
                        new CheckDetails('External API'),
                        $this->httpClient,
                        new \GuzzleHttp\Psr7\Request('GET', 'https://www.google.com'),
                    ),
                ],
                $psr17Factory,
                $psr17Factory,
            ))
                ->handle((new PsrHttpFactory($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory))
                    ->createRequest($request)),
        );
    }
}

Contributing

Contributions are welcome! To contribute, please familiarize yourself with CONTRIBUTING.md.

Copyright and License

ostrolucky/health-check-provider is copyright © Gabriel Ostrolucký and licensed for use under the terms of the MIT License (MIT). Please see LICENSE for more information.

About

Provides structure for healthcheck endpoints in accordance with IETF's healthcheck draft RFC

License:MIT License


Languages

Language:PHP 100.0%