index.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. require __DIR__ . '/../vendor/autoload.php';
  3. $path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
  4. $file = __DIR__ . $path;
  5. if (php_sapi_name() === 'cli-server' && is_file($file)) {
  6. return false;
  7. }
  8. if (class_exists(Dotenv\Dotenv::class) && file_exists(__DIR__ . '/../.env')) {
  9. Dotenv\Dotenv::createImmutable(
  10. dirname(__DIR__),
  11. null,
  12. true
  13. )->safeLoad();
  14. }
  15. error_reporting(E_ALL);
  16. ini_set('display_errors', 1); // Para depuração
  17. ini_set('display_startup_errors', 1);
  18. use FrameworkX\App;
  19. use Middlewares\HmacAuthMiddleware;
  20. use Middlewares\JWTAuthMiddleware;
  21. use Middlewares\CorsControl;
  22. use Psr\Http\Message\ServerRequestInterface;
  23. use React\Http\Message\Response;
  24. $app = new App();
  25. // Instancia os middlewares
  26. $authHmac = new HmacAuthMiddleware();
  27. $authJwt = new JWTAuthMiddleware();
  28. $cors = new CorsControl();
  29. // Função para envolver rotas com CORS
  30. $withCors = function ($handler) use ($cors) {
  31. return function (ServerRequestInterface $request) use ($handler, $cors) {
  32. return $cors($request, $handler);
  33. };
  34. };
  35. // Função para lidar com requisições OPTIONS
  36. $handleOptions = function (ServerRequestInterface $request) {
  37. $corsHeaders = [
  38. 'Access-Control-Allow-Origin' => '*',
  39. 'Access-Control-Allow-Methods' => '*',
  40. 'Access-Control-Allow-Headers' => '*'
  41. ];
  42. return new Response(200, $corsHeaders);
  43. };
  44. // Rotas com CORS aplicado
  45. $app->get('/hmachelloworld', $withCors($authHmac), \Controllers\HelloController::class);
  46. $app->options('/hmachelloworld', $handleOptions);
  47. $app->get('/jwthelloworld', $withCors($authJwt), \Controllers\HelloController::class);
  48. $app->options('/jwthelloworld', $handleOptions);
  49. //Rotas User
  50. $app->post('/login', $withCors(\Controllers\LoginController::class));
  51. $app->options('/login', $handleOptions);
  52. $app->post('/register', $withCors(\Controllers\RegisterController::class));
  53. $app->options('/register', $handleOptions);
  54. //Rotas Category
  55. $app->get('/category', $withCors(\Controllers\CategoryController::class));
  56. $app->post('/category', $withCors(\Controllers\CategoryController::class));
  57. $app->options('/category', $handleOptions);
  58. $app->post('/category/delete', $withCors(\Controllers\CategoryController::class));
  59. $app->options('/category/delete', $handleOptions);
  60. $app->post('/category/add-product', $withCors(\Controllers\CategoryController::class));
  61. $app->options('/category/add-product', $handleOptions);
  62. //Rotas Product
  63. $app->get('/product', $withCors(\Controllers\ProductController::class));
  64. $app->post('/product', $withCors(\Controllers\ProductController::class));
  65. $app->options('/product', $handleOptions);
  66. $app->run();