63 lines
1.8 KiB
PHP
63 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Providers;
|
|
|
|
use Illuminate\Cache\RateLimiting\Limit;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\RateLimiter;
|
|
use Illuminate\Support\Facades\URL;
|
|
use Illuminate\Support\Facades\Vite;
|
|
use Illuminate\Support\ServiceProvider;
|
|
|
|
class AppServiceProvider extends ServiceProvider {
|
|
/**
|
|
* Register any application services.
|
|
*/
|
|
public function register(): void {
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Bootstrap any application services.
|
|
*/
|
|
public function boot(): void {
|
|
// Prevents lazy loading
|
|
// Prevents silently discarding attributes.
|
|
// Prevents accessing missing attributes.
|
|
Model::shouldBeStrict();
|
|
|
|
// Automatically eager-load needed relations.
|
|
Model::automaticallyEagerLoadRelationships();
|
|
|
|
// Force HTTPS for all generated URLs.
|
|
URL::forceHttps(
|
|
$this->app->environment(['production']),
|
|
);
|
|
|
|
// Prohibits: db:wipe, migrate:fresh, migrate:refresh, and migrate:reset
|
|
DB::prohibitDestructiveCommands($this->app->environment(['production']));
|
|
|
|
// Prefetch all assets at once.
|
|
Vite::prefetch();
|
|
|
|
// Disable resourc wrapping
|
|
JsonResource::withoutWrapping();
|
|
|
|
// Globally rate limit api requests
|
|
RateLimiter::for('api', function (Request $request) {
|
|
return Limit::perMinute(500)
|
|
->by($request->user()->id ?? $request->ip())
|
|
->response(function () {
|
|
return response()->json([
|
|
'message' => 'Too many attempts. Try again later.',
|
|
], 429);
|
|
});
|
|
});
|
|
}
|
|
}
|