app.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. use Illuminate\Foundation\Application;
  3. use Illuminate\Foundation\Configuration\Exceptions;
  4. use Illuminate\Foundation\Configuration\Middleware;
  5. use Illuminate\Http\Exceptions\PostTooLargeException;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Console\Scheduling\Schedule;
  8. use App\Http\Middleware\SetUserLanguage;
  9. use App\Http\Middleware\CheckPermission;
  10. use App\Http\Middleware\PerformanceMonitor;
  11. use Laravel\Sanctum\Http\Middleware\CheckForAnyAbility;
  12. use App\Tasks\DeleteExpiredTokens;
  13. return Application::configure(basePath: dirname(__DIR__))
  14. ->withRouting(
  15. web: __DIR__ . '/../routes/web.php',
  16. api: __DIR__ . '/../routes/api.php',
  17. commands: __DIR__ . '/../routes/console.php',
  18. health: '/up',
  19. )
  20. ->withMiddleware(function (Middleware $middleware) {
  21. // $middleware->statefulApi();
  22. $middleware->append([SetUserLanguage::class, PerformanceMonitor::class]);
  23. $middleware->alias([
  24. 'permission' => CheckPermission::class,
  25. 'ability' => CheckForAnyAbility::class,
  26. ]);
  27. })
  28. ->withSchedule(function (Schedule $schedule) {
  29. $schedule->call(new DeleteExpiredTokens)->everyMinute();
  30. })
  31. ->withExceptions(function (Exceptions $exceptions) {
  32. // Anexo maior que o post_max_size do PHP: responde em JSON no lugar do
  33. // erro cru, para o front conseguir mostrar a mensagem ao usuário.
  34. $exceptions->render(function (PostTooLargeException $e, Request $request) {
  35. if (!$request->expectsJson()) {
  36. return null;
  37. }
  38. return response()->json([
  39. 'message' => __('http.payload_too_large', [
  40. 'max' => round(config('uploads.max_size_kb') / 1024, 1),
  41. ]),
  42. ], 413);
  43. });
  44. })
  45. ->create();