Skip to content

Commit

Permalink
refactor: use route service map function (#5365)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Jul 14, 2021
1 parent f0c59be commit b4b4a13
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Str;
use Illuminate\Http\Request;
use App\Helpers\RequestHelper;
use App\Models\Contact\Contact;
use Illuminate\Http\JsonResponse;
use App\Services\Instance\IdHasher;
Expand Down Expand Up @@ -48,25 +49,6 @@ public function boot()

$this->configureRateLimiting();

$this->routes(function () {
Route::prefix('api')
->middleware('api')
->namespace($this->namespace.'\Api')
->group(base_path('routes/api.php'));

Route::prefix('oauth')
->namespace($this->namespace.'\Api')
->group(base_path('routes/oauth.php'));

Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));

Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/special.php'));
});

Route::bind('contact', function ($value) {
// In case the user is logged out
if (! Auth::check()) {
Expand All @@ -90,6 +72,29 @@ public function boot()
Route::model('otherContact', Contact::class);
}

/**
* Define the routes for the application.
*/
public function map(): void
{
Route::prefix('api')
->middleware('api')
->namespace($this->namespace.'\Api')
->group(base_path('routes/api.php'));

Route::prefix('oauth')
->namespace($this->namespace.'\Api')
->group(base_path('routes/oauth.php'));

Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));

Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/special.php'));
}

/**
* Configure the rate limiters for the application.
*
Expand All @@ -99,7 +104,7 @@ protected function configureRateLimiting()
{
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60)
->by(optional($request->user())->id ?: $request->ip())
->by(optional($request->user())->id ?: RequestHelper::ip())
->response(function (Request $request, array $headers) {
$message = [
'error' => [
Expand All @@ -112,7 +117,7 @@ protected function configureRateLimiting()
});
});
RateLimiter::for('oauth', function (Request $request) {
return Limit::perMinute(5)->by($request->input('email') ?: $request->ip());
return Limit::perMinute(5)->by($request->input('email') ?: RequestHelper::ip());
});
}
}

0 comments on commit b4b4a13

Please sign in to comment.