4
0
mirror of https://github.com/AzuraCast/AzuraCast.git synced 2024-06-21 00:17:09 +00:00
AzuraCast/src/Middleware/RateLimit.php
2021-07-19 00:53:45 -05:00

31 lines
787 B
PHP

<?php
declare(strict_types=1);
namespace App\Middleware;
use App\Http\ServerRequest;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\RequestHandlerInterface;
/**
* Apply a rate limit for requests on this page and throw an exception if the limit is exceeded.
*/
class RateLimit
{
public function __construct(
protected string $rl_group = 'default',
protected int $rl_interval = 5,
protected int $rl_limit = 2
) {
}
public function __invoke(ServerRequest $request, RequestHandlerInterface $handler): ResponseInterface
{
$rateLimit = $request->getRateLimit();
$rateLimit->checkRequestRateLimit($request, $this->rl_group, $this->rl_interval, $this->rl_limit);
return $handler->handle($request);
}
}