<?php
// src/Controller/ApiStatusController.php
namespace App\Controller\APIManagement;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/api")
*/
class ApiStatusController extends AbstractController
{
private $httpClient;
public function __construct(HttpClientInterface $httpClient)
{
$this->httpClient = $httpClient;
}
/**
* @Route("/status", name="api_status", methods={"GET"})
*/
public function checkApiStatus() : JsonResponse
{
try {
$response = $this->httpClient->request('GET', 'http://127.0.0.1:8000/');
if ($response->getStatusCode() === 200) {
return new JsonResponse(['status' => 'OK']);
} else {
return new JsonResponse(['status' => 'KO', 'code' => $response->getStatusCode()]);
}
} catch (\Exception $e) {
return new JsonResponse(['status' => 'KO', 'error' => $e->getMessage()]);
}
}
}