Skip to content

Commit 795cbff

Browse files
committed
feat(rate limiter): applied rate limiter for APIs
added laravel default rate limiter on all apis
1 parent 0de982a commit 795cbff

File tree

5 files changed

+26
-1
lines changed

5 files changed

+26
-1
lines changed

.env.docker.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ LOG_STACK=daily
2121
LOG_DEPRECATIONS_CHANNEL=null
2222
LOG_LEVEL=debug
2323

24+
# API Rate Limiting Configuration
25+
DEFAULT_API_RATE_LIMIT=60
26+
2427
# Docker MySQL Configuration
2528
DB_CONNECTION=mysql
2629
DB_HOST=mysql

app/Providers/AppServiceProvider.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
namespace App\Providers;
66

77
use Carbon\CarbonImmutable;
8+
use Illuminate\Cache\RateLimiting\Limit;
89
use Illuminate\Database\Eloquent\Model;
10+
use Illuminate\Http\Request;
911
use Illuminate\Support\Facades\Date;
1012
use Illuminate\Support\Facades\DB;
13+
use Illuminate\Support\Facades\RateLimiter;
1114
use Illuminate\Support\ServiceProvider;
1215

1316
class AppServiceProvider extends ServiceProvider
@@ -28,5 +31,16 @@ public function boot(): void
2831
Date::use(CarbonImmutable::class);
2932
Model::shouldBeStrict(! $this->app->isProduction());
3033
DB::prohibitDestructiveCommands($this->app->isProduction());
34+
35+
// Disable rate limiting during testing
36+
if ($this->app->environment('testing')) {
37+
RateLimiter::for('api', fn () => Limit::none());
38+
} else {
39+
// Rate Limiting for API routes
40+
RateLimiter::for('api', function (Request $request) {
41+
return Limit::perMinute((int) config('rate-limiting.api.default_rate_limit'))
42+
->by($request->user()?->id ?: $request->ip());
43+
});
44+
}
3145
}
3246
}

bootstrap/app.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
->withMiddleware(function (Middleware $middleware): void {
1717
$middleware->alias([
1818
'ability' => \App\Http\Middleware\CheckTokenAbility::class,
19+
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
1920
]);
2021
})
2122
->withExceptions(function (Exceptions $exceptions): void {

config/rate-limiting.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
return [
4+
'api' => [
5+
'default_rate_limit' => env('DEFAULT_API_RATE_LIMIT', 60),
6+
],
7+
];

routes/api_v1.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use Illuminate\Http\Request;
44
use Illuminate\Support\Facades\Route;
55

6-
Route::prefix('v1')->group(function () {
6+
Route::prefix('v1')->middleware(['throttle:api'])->group(function () {
77
Route::get('/', function (Request $request) {
88
return 'Laravel Blog API V1 Root is working';
99
})->name('api.v1.status');

0 commit comments

Comments
 (0)