Skip to content

Commit

Permalink
feat: add a console command to see memcached stats (#5186)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored May 14, 2021
1 parent 4409d85 commit b359c90
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 265 deletions.
2 changes: 2 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ protected function commands()
if ($this->app->environment() != 'production') {
$this->load(__DIR__.'/Commands/Tests');
}

require base_path('routes/console.php');
}

/**
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ class Kernel extends HttpKernel
],

'api' => [
'throttle:60,1',
'throttle:api',
'sentry.context',
'locale',
],

'oauth' => [
'throttle:5,1',
'throttle:oauth',
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
Expand Down Expand Up @@ -84,7 +84,7 @@ class Kernel extends HttpKernel
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'sentry.context' => \App\Http\Middleware\SentryContext::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \App\Http\Middleware\ThrottleRequestsMiddleware::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \App\Http\Middleware\EnsureEmailIsVerified::class,
'webauthn' => \LaravelWebauthn\Http\Middleware\WebauthnMiddleware::class,
];
Expand Down
180 changes: 0 additions & 180 deletions app/Http/Middleware/ThrottleRequestsMiddleware.php

This file was deleted.

3 changes: 1 addition & 2 deletions app/Jobs/GetGPSCoordinate.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public function __construct(Place $place)
public function middleware()
{
return [
new RateLimited('GPSCoordinatePerMinute'),
new RateLimited('GPSCoordinatePerDay'),
new RateLimited('GPSCoordinate'),
];
}

Expand Down
10 changes: 5 additions & 5 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ public function boot()

Paginator::defaultView('vendor.pagination.default');

RateLimiter::for('GPSCoordinatePerMinute', function () {
return Limit::perMinute(60);
});
RateLimiter::for('GPSCoordinatePerDay', function () {
return Limit::perDay(5000);
RateLimiter::for('GPSCoordinate', function () {
return [
Limit::perMinute(60),
Limit::perDay(5000),
];
});
}

Expand Down
117 changes: 42 additions & 75 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
namespace App\Providers;

use Illuminate\Support\Str;
use Illuminate\Routing\Router;
use Illuminate\Http\Request;
use App\Models\Contact\Contact;
use Illuminate\Http\JsonResponse;
use App\Services\Instance\IdHasher;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\URL;
use App\Exceptions\WrongIdException;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Config;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

Expand Down Expand Up @@ -43,6 +46,27 @@ public function boot()
URL::forceScheme('https');
}

$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 @@ -67,85 +91,28 @@ public function boot()
}

/**
* Define the routes for the application.
*
* @param \Illuminate\Routing\Router $router
* @return void
*/
public function map(Router $router)
{
$this->mapApiRoutes($router);
$this->mapWebRoutes($router);
$this->mapOAuthRoutes($router);
$this->mapSpecialRoutes($router);
}

/**
* Define the "web" routes for the application.
* Configure the rate limiters for the application.
*
* These routes all receive session state, CSRF protection, etc.
*
* @param \Illuminate\Routing\Router $router
* @return void
*/
protected function mapWebRoutes(Router $router)
protected function configureRateLimiting()
{
$router->group([
'middleware' => 'web',
'namespace' => $this->namespace,
], function () {
require base_path('routes/web.php');
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60)
->by(optional($request->user())->id ?: $request->ip())
->response(function (Request $request, array $headers) {
$message = [
'error' => [
'message' => config('api.error_codes.34'),
'error_code' => 34,
],
];

return new JsonResponse($message, 429, $headers);
});
});
}

/**
* Define the custom oauth routes for the API.
*
* @param \Illuminate\Routing\Router $router
* @return void
*/
protected function mapOAuthRoutes(Router $router)
{
$router->group([
'prefix' => 'oauth',
'namespace' => $this->namespace.'\Api',
], function () {
require base_path('routes/oauth.php');
});
}

/**
* Define the "api" routes for the application.
*
* These routes are typically stateless.
*
* @return void
*/
protected function mapApiRoutes(Router $router)
{
$router->group([
'prefix' => 'api',
'middleware' => 'api',
'namespace' => $this->namespace.'\Api',
], function () {
require base_path('routes/api.php');
});
}

/**
* Define the "special" routes for the application.
*
* These routes are typically stateless.
*
* @return void
*/
protected function mapSpecialRoutes(Router $router)
{
$router->group([
'middleware' => 'web',
'namespace' => $this->namespace,
], function () {
require base_path('routes/special.php');
RateLimiter::for('oauth', function (Request $request) {
return Limit::perMinute(5)->by($request->input('email') ?: $request->ip());
});
}
}
Loading

0 comments on commit b359c90

Please sign in to comment.