Skip to content

Commit

Permalink
BE-007: Implement Rate Limiting
Browse files Browse the repository at this point in the history
Due too much open request by unidentified users that access FE/BE module,
although request actually are not allowed, but somehow it junk notification and database services.
Result to overflow database insert and telegram call API.

Possibility Issue:
- Overflow database
- Timeout or too many request telegram notification API

Resolution:
- Implement laravel rate limiting features

References:
- https://laravel.com/docs/10.x/rate-limiting

Signed-off-by: Dicky Herlambang (花) <herlambangdicky5@gmail.com>
  • Loading branch information
Nicklas373 committed Apr 21, 2024
1 parent 8ad880e commit 3e9ef5f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
14 changes: 11 additions & 3 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ class RouteServiceProvider extends ServiceProvider
*/
public function boot(): void
{
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
});
$this->configureRateLimiting();

$this->routes(function () {
Route::middleware('api')
Expand All @@ -37,4 +35,14 @@ public function boot(): void
->group(base_path('routes/web.php'));
});
}

/**
* Configure the rate limiters for the application.
*/
protected function configureRateLimiting(): void
{
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(5)->by($request->ip());
});
}
}
6 changes: 3 additions & 3 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
Route::post('revoke', [AuthController::class, 'revoke']);
});

Route::middleware('auth:api')->prefix('v1/core')->group(function() {
Route::middleware(['auth:api'],['throttle:api'])->prefix('v1/core')->group(function() {
// API v1 Backend PDF Core Processing Route
Route::post('compress', [compressController::class, 'compress']);
Route::post('convert', [convertController::class, 'convert']);
Expand All @@ -46,14 +46,14 @@
Route::post('watermark', [watermarkController::class, 'watermark']);
});

Route::middleware('auth:api')->prefix('v1/file')->group(function() {
Route::middleware(['auth:api'],['throttle:api'])->prefix('v1/file')->group(function() {
// API v1 Backend File Management Route
Route::post('upload', [uploadController::class, 'upload']);
Route::post('remove', [uploadController::class, 'remove']);
Route::post('thumbnail', [thumbnailController::class, 'getThumbnail']);
});

Route::middleware('auth:api')->prefix('v1/logs')->group(function() {
Route::middleware(['auth:api'],['throttle:api'])->prefix('v1/logs')->group(function() {
// API v1 Backend Logging Route
Route::get('limit', [limitLogController::class, 'getLimit']);
Route::post('proc/single', [processLogController::class, 'getLogs']);
Expand Down

0 comments on commit 3e9ef5f

Please sign in to comment.