-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from cslant/feat/Create-ratelimit-for-API-#28
Feat/create ratelimit for api #28
- Loading branch information
Showing
4 changed files
with
38 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,8 @@ | ||
<?php | ||
|
||
$routePrefix = env('BLOG_API_ROUTE_PREFIX', 'api'); | ||
|
||
return [ | ||
'defaults' => [ | ||
/* Set route prefix for the blog API */ | ||
'route_prefix' => $routePrefix, | ||
'route_prefix' => env('BLOG_API_ROUTE_PREFIX', 'api'), | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace CSlant\Blog\ApiPackage\Providers; | ||
|
||
use CSlant\Blog\Core\Models\User; | ||
use Illuminate\Cache\RateLimiting\Limit; | ||
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\RateLimiter; | ||
|
||
class RouteServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Move base routes to a service provider to make sure all filters & actions can hook to base routes | ||
*/ | ||
public function boot(): void | ||
{ | ||
// add rate limit for api | ||
$this->configureRateLimiting(); | ||
} | ||
|
||
protected function configureRateLimiting(): void | ||
{ | ||
RateLimiter::for((string) config('blog-api.defaults.route_prefix'), function (Request $request) { | ||
/** @var null|User $user */ | ||
$user = $request->user(); | ||
$identifier = $user ? $user->id : $request->ip(); | ||
|
||
return Limit::perMinute(40)->by($identifier); | ||
}); | ||
} | ||
} |