From 179fab7f5ea227a261495051272ec06d406f9d17 Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Tue, 10 Dec 2024 22:35:30 +0700 Subject: [PATCH 1/6] #28 feat: Create rate limit for API --- src/Providers/RouteServiceProvider.php | 28 ++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/Providers/RouteServiceProvider.php diff --git a/src/Providers/RouteServiceProvider.php b/src/Providers/RouteServiceProvider.php new file mode 100644 index 0000000..f203734 --- /dev/null +++ b/src/Providers/RouteServiceProvider.php @@ -0,0 +1,28 @@ +configureRateLimiting(); + } + + protected function configureRateLimiting(): void + { + RateLimiter::for((string) config('blog-api.defaults.route_prefix'), function (Request $request) { + return Limit::perMinute(40)->by(optional($request->user())->id ?: $request->ip()); + }); + } +} From 4fed815edc1093f5d3ded9fb528784b146d39f2d Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Tue, 10 Dec 2024 22:36:17 +0700 Subject: [PATCH 2/6] #28 refactor: the config for blog api --- config/blog-api.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/config/blog-api.php b/config/blog-api.php index 3d8509d..b987ae6 100644 --- a/config/blog-api.php +++ b/config/blog-api.php @@ -1,10 +1,8 @@ [ /* Set route prefix for the blog API */ - 'route_prefix' => $routePrefix, + 'route_prefix' => env('BLOG_API_ROUTE_PREFIX', 'api'), ], ]; From 09694a6a1c6356a9d508b8cfc5fe16db1a14dbbf Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Tue, 10 Dec 2024 22:38:45 +0700 Subject: [PATCH 3/6] #28 fix: register blog route service to apply rate limit --- composer.json | 3 ++- src/Providers/RouteServiceProvider.php | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 2cb912b..850fa5e 100644 --- a/composer.json +++ b/composer.json @@ -70,7 +70,8 @@ "extra": { "laravel": { "providers": [ - "CSlant\\Blog\\Api\\Providers\\BlogApiServiceProvider" + "CSlant\\Blog\\Api\\Providers\\BlogApiServiceProvider", + "CSlant\\Blog\\Api\\Providers\\RouteServiceProvider" ] } }, diff --git a/src/Providers/RouteServiceProvider.php b/src/Providers/RouteServiceProvider.php index f203734..8c6cfd6 100644 --- a/src/Providers/RouteServiceProvider.php +++ b/src/Providers/RouteServiceProvider.php @@ -2,7 +2,6 @@ namespace CSlant\Blog\ApiPackage\Providers; -use Botble\Theme\Facades\Theme; use Illuminate\Cache\RateLimiting\Limit; use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; use Illuminate\Http\Request; From b4ebd0651a1accadf8a4485f174783f899381782 Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Tue, 10 Dec 2024 22:43:56 +0700 Subject: [PATCH 4/6] #28 fix: analyse --- src/Providers/RouteServiceProvider.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Providers/RouteServiceProvider.php b/src/Providers/RouteServiceProvider.php index 8c6cfd6..b8de82b 100644 --- a/src/Providers/RouteServiceProvider.php +++ b/src/Providers/RouteServiceProvider.php @@ -2,6 +2,7 @@ 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; @@ -21,7 +22,11 @@ public function boot(): void protected function configureRateLimiting(): void { RateLimiter::for((string) config('blog-api.defaults.route_prefix'), function (Request $request) { - return Limit::perMinute(40)->by(optional($request->user())->id ?: $request->ip()); + /** @var User|null $user */ + $user = $request->user(); + $identifier = $user ? $user->id : $request->ip(); + + return Limit::perMinute(40)->by($identifier); }); } } From 2e832d8eedde72dfc122e88262cb52d2a7aa8f85 Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Tue, 10 Dec 2024 22:45:20 +0700 Subject: [PATCH 5/6] #28 chore: ignore some small warnings --- phpstan-baseline.neon | 3 +++ 1 file changed, 3 insertions(+) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index da9ba7d..cbc5146 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -11,3 +11,6 @@ parameters: - message: '#Call to an undefined static method CSlant\\Blog\\Api\\Http\\Resources\\ListPostResource::collection\(\).#' path: src/Http/Controllers/PostController.php + + - message: '#Cannot cast mixed to string#' + path: '*' From e1c656b8920e70104905265ab0e73a33729d7191 Mon Sep 17 00:00:00 2001 From: tanhongit Date: Tue, 10 Dec 2024 15:45:41 +0000 Subject: [PATCH 6/6] Fix styling --- src/Providers/RouteServiceProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Providers/RouteServiceProvider.php b/src/Providers/RouteServiceProvider.php index b8de82b..c598964 100644 --- a/src/Providers/RouteServiceProvider.php +++ b/src/Providers/RouteServiceProvider.php @@ -22,7 +22,7 @@ public function boot(): void protected function configureRateLimiting(): void { RateLimiter::for((string) config('blog-api.defaults.route_prefix'), function (Request $request) { - /** @var User|null $user */ + /** @var null|User $user */ $user = $request->user(); $identifier = $user ? $user->id : $request->ip();