trypost/bootstrap/app.php
Paulo Castellano 039ed02b42 feat: rate limiting, cleanup duplicates, ApiKey app tests, bootstrap hardening
- Add API rate limiting (60/min per workspace) in AppServiceProvider
- Add throttle:api middleware to all API routes
- Add rate limit exception rendering for JSON responses
- Add middleware priority list (AuthenticateApiToken before ThrottleRequests)
- Add ApiKeyControllerTest (6 tests for App dashboard CRUD)
- Remove duplicate test files (Controllers/ dir duplicates)
- Remove old controllers and routes/settings.php
- Add routes/mcp.php placeholder
- Add API Keys nav item to settings layout
- Fix LazilyRefreshDatabase conflict in API tests
- 698 tests passing
2026-03-29 20:14:23 -03:00

63 lines
2.3 KiB
PHP

<?php
use App\Http\Middleware\Api\AuthenticateApiToken;
use App\Http\Middleware\EnsureSubscribed;
use App\Http\Middleware\HandleAppearance;
use App\Http\Middleware\HandleInertiaRequests;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Http\Middleware\AddLinkHeadersForPreloadedAssets;
use Illuminate\Http\Request;
use Illuminate\Routing\Middleware\ThrottleRequests;
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
api: __DIR__.'/../routes/api.php',
apiPrefix: '',
commands: __DIR__.'/../routes/console.php',
channels: __DIR__.'/../routes/channels.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware): void {
$middleware->trustProxies(at: '*');
$middleware->encryptCookies(except: ['appearance', 'sidebar_state']);
$middleware->web(append: [
HandleAppearance::class,
HandleInertiaRequests::class,
AddLinkHeadersForPreloadedAssets::class,
]);
$middleware->alias([
'subscribed' => EnsureSubscribed::class,
'api.auth' => AuthenticateApiToken::class,
]);
$middleware->preventRequestForgery(except: [
'stripe/*',
]);
$middleware->prependToPriorityList(
ThrottleRequests::class,
AuthenticateApiToken::class,
);
})
->withExceptions(function (Exceptions $exceptions): void {
$exceptions->renderable(function (TooManyRequestsHttpException $e, Request $request) {
if ($request->expectsJson()) {
$retryAfter = $e->getHeaders()['Retry-After'] ?? null;
$message = $retryAfter
? "Rate limit exceeded. Please retry after {$retryAfter} seconds."
: 'Rate limit exceeded. Please try again later.';
return response()->json([
'name' => 'rate_limit_exceeded',
'message' => $message,
], 429)->withHeaders($e->getHeaders());
}
});
})->create();