|
@@ -17,20 +17,52 @@ if (class_exists(Dotenv\Dotenv::class) && file_exists(__DIR__ . '/../.env')) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
error_reporting(E_ALL);
|
|
error_reporting(E_ALL);
|
|
|
|
|
+ini_set('display_errors', 1); // Para depuração
|
|
|
|
|
+ini_set('display_startup_errors', 1);
|
|
|
|
|
|
|
|
use FrameworkX\App;
|
|
use FrameworkX\App;
|
|
|
use Middlewares\HmacAuthMiddleware;
|
|
use Middlewares\HmacAuthMiddleware;
|
|
|
use Middlewares\JWTAuthMiddleware;
|
|
use Middlewares\JWTAuthMiddleware;
|
|
|
|
|
+use Middlewares\CorsControl;
|
|
|
|
|
+use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
|
+use React\Http\Message\Response;
|
|
|
|
|
|
|
|
$app = new App();
|
|
$app = new App();
|
|
|
|
|
+
|
|
|
|
|
+// Instancia os middlewares
|
|
|
$authHmac = new HmacAuthMiddleware();
|
|
$authHmac = new HmacAuthMiddleware();
|
|
|
$authJwt = new JWTAuthMiddleware();
|
|
$authJwt = new JWTAuthMiddleware();
|
|
|
|
|
+$cors = new CorsControl();
|
|
|
|
|
+
|
|
|
|
|
+// 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 com requisições OPTIONS
|
|
|
|
|
+$handleOptions = function (ServerRequestInterface $request) {
|
|
|
|
|
+ $corsHeaders = [
|
|
|
|
|
+ 'Access-Control-Allow-Origin' => '*',
|
|
|
|
|
+ 'Access-Control-Allow-Methods' => '*',
|
|
|
|
|
+ 'Access-Control-Allow-Headers' => '*'
|
|
|
|
|
+ ];
|
|
|
|
|
+ return new Response(200, $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->get('/hmachelloworld', $authHmac,\Controllers\HelloController::class);
|
|
|
|
|
-$app->get('/jwthelloworld', $authJwt,\Controllers\HelloController::class);
|
|
|
|
|
|
|
+$app->post('/login', $withCors(\Controllers\LoginController::class));
|
|
|
|
|
+$app->options('/login', $handleOptions);
|
|
|
|
|
|
|
|
-$app->post('/login', \Controllers\LoginController::class);
|
|
|
|
|
-$app->post('/register', \Controllers\RegisterController::class);
|
|
|
|
|
|
|
+$app->post('/register', $withCors(\Controllers\RegisterController::class));
|
|
|
|
|
+$app->options('/register', $handleOptions);
|
|
|
|
|
|
|
|
$app->get('/category', \Controllers\CategoryController::class);
|
|
$app->get('/category', \Controllers\CategoryController::class);
|
|
|
$app->post('/category', \Controllers\CategoryController::class);
|
|
$app->post('/category', \Controllers\CategoryController::class);
|