Skip to content

Commit

Permalink
feat: add configurable rate limit for api and oauth (#5489)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusDick authored Sep 8, 2021
1 parent a305f96 commit bc50181
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,7 @@ ENABLE_WEATHER=false
# Darksky provides an api with 1000 free API calls per day
# You need to enable the weather above if you provide an API key here.
DARKSKY_API_KEY=

# Configure rate limits for RouteService per minute
RATE_LIMIT_PER_MINUTE_API=60
RATE_LIMIT_PER_MINUTE_OAUTH=5
4 changes: 2 additions & 2 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function map(): void
protected function configureRateLimiting()
{
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60)
return Limit::perMinute(config('monica.rate_limit_api'))
->by(optional($request->user())->id ?: RequestHelper::ip())
->response(function (Request $request, array $headers) {
$message = [
Expand All @@ -117,7 +117,7 @@ protected function configureRateLimiting()
});
});
RateLimiter::for('oauth', function (Request $request) {
return Limit::perMinute(5)->by($request->input('email') ?: RequestHelper::ip());
return Limit::perMinute(config('monica.rate_limit_oauth'))->by($request->input('email') ?: RequestHelper::ip());
});
}
}
10 changes: 10 additions & 0 deletions config/monica.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,16 @@
*/
'darksky_api_key' => env('DARKSKY_API_KEY', null),

/*
|--------------------------------------------------------------------------
| Configure default rate limit for route services per minute
|--------------------------------------------------------------------------
|
| Configure rate limit for route services per minute
*/
'rate_limit_api' => env('RATE_LIMIT_PER_MINUTE_API', 60),
'rate_limit_oauth' => env('RATE_LIMIT_PER_MINUTE_OAUTH', 5),

/*
|--------------------------------------------------------------------------
| Default avatar size
Expand Down

0 comments on commit bc50181

Please sign in to comment.