src/Controller/APIManagement/ApiStatusController.php line 24

Open in your IDE?
  1. <?php
  2. // src/Controller/ApiStatusController.php
  3. namespace App\Controller\APIManagement;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Symfony\Contracts\HttpClient\HttpClientInterface;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. /**
  9.  * @Route("/api")
  10.  */
  11. class ApiStatusController extends AbstractController
  12. {
  13.     private $httpClient;
  14.     public function __construct(HttpClientInterface $httpClient)
  15.     {
  16.         $this->httpClient $httpClient;
  17.     }
  18.     /**
  19.      * @Route("/status", name="api_status", methods={"GET"})
  20.      */
  21.     public function checkApiStatus() : JsonResponse
  22.     {
  23.         try {
  24.             $response $this->httpClient->request('GET''http://127.0.0.1:8000/');
  25.             if ($response->getStatusCode() === 200) {
  26.                 return new JsonResponse(['status' => 'OK']);
  27.             } else {
  28.                 return new JsonResponse(['status' => 'KO''code' => $response->getStatusCode()]);
  29.             }
  30.         } catch (\Exception $e) {
  31.             return new JsonResponse(['status' => 'KO''error' => $e->getMessage()]);
  32.         }
  33.     }
  34. }