safeLoad(); } error_reporting(E_ALL); use FrameworkX\App; use Middlewares\HmacAuthMiddleware; use Middlewares\JWTAuthMiddleware; use Middlewares\CorsMiddleware; use Psr\Http\Message\ServerRequestInterface; use React\Http\Message\Response; $app = new App(); // Instancia os middlewares $authHmac = new HmacAuthMiddleware(); $authJwt = new JWTAuthMiddleware(); $cors = new CorsMiddleware(); // Função para envolver rotas com CORS $withCors = function ($handler) use ($cors) { return function (ServerRequestInterface $request) use ($handler, $cors) { return $cors($request, $handler); }; }; // Função para lidar apenas com requisições OPTIONS $handleOptions = function (ServerRequestInterface $request) { $corsHeaders = [ 'Access-Control-Allow-Origin' => '*', // Permite qualquer origem 'Access-Control-Allow-Methods' => '*', // Permite qualquer método 'Access-Control-Allow-Headers' => '*', // Permite qualquer cabeçalho // Removido Access-Control-Allow-Credentials para evitar conflitos ]; return new Response(200, array_filter($corsHeaders)); }; // Rotas com CORS aplicado $app->get('/hmachelloworld', $withCors($authHmac), \Controllers\HelloController::class); $app->options('/hmachelloworld', $handleOptions); $app->get('/jwthelloworld', $withCors($authJwt), \Controllers\HelloController::class); $app->options('/jwthelloworld', $handleOptions); $app->post('/login', $withCors(\Controllers\LoginController::class)); $app->options('/login', $handleOptions); $app->post('/register', $withCors(\Controllers\RegisterController::class)); $app->options('/register', $handleOptions); $app->run();