mirror of
https://github.com/rekryt/iplist.git
synced 2025-10-13 08:59:34 +03:00
Initial commit
This commit is contained in:
119
src/Infrastructure/API/Server.php
Normal file
119
src/Infrastructure/API/Server.php
Normal file
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
namespace OpenCCK\Infrastructure\API;
|
||||
|
||||
use Amp\ByteStream\BufferException;
|
||||
use Amp\Http\Server\DefaultErrorHandler;
|
||||
use Amp\Http\Server\Driver\ConnectionLimitingClientFactory;
|
||||
use Amp\Http\Server\Driver\ConnectionLimitingServerSocketFactory;
|
||||
use Amp\Http\Server\Driver\DefaultHttpDriverFactory;
|
||||
use Amp\Http\Server\Driver\SocketClientFactory;
|
||||
use Amp\Http\Server\ErrorHandler;
|
||||
use Amp\Http\Server\HttpServer;
|
||||
use Amp\Http\Server\HttpServerStatus;
|
||||
use Amp\Http\Server\SocketHttpServer;
|
||||
use Amp\Socket\BindContext;
|
||||
use Amp\Sync\LocalSemaphore;
|
||||
use Amp\Socket;
|
||||
|
||||
use Monolog\Logger;
|
||||
use OpenCCK\App\Service\IPListService;
|
||||
use OpenCCK\Infrastructure\API\Handler\HTTPHandler;
|
||||
use Throwable;
|
||||
|
||||
use function OpenCCK\getEnv;
|
||||
|
||||
final class Server implements AppModuleInterface {
|
||||
private static Server $_instance;
|
||||
|
||||
private int $connectionLimit = 1024;
|
||||
private int $connectionPerIpLimit = 10;
|
||||
|
||||
/**
|
||||
* @param ?HttpServer $httpServer
|
||||
* @param ?ErrorHandler $errorHandler
|
||||
* @param ?BindContext $bindContext
|
||||
* @param ?Logger $logger
|
||||
* @throws Throwable
|
||||
*/
|
||||
private function __construct(
|
||||
private ?HttpServer $httpServer,
|
||||
private ?ErrorHandler $errorHandler,
|
||||
private ?Socket\BindContext $bindContext,
|
||||
private ?Logger $logger
|
||||
) {
|
||||
$this->logger = $logger ?? App::getLogger();
|
||||
$serverSocketFactory = new ConnectionLimitingServerSocketFactory(new LocalSemaphore($this->connectionLimit));
|
||||
$clientFactory = new ConnectionLimitingClientFactory(
|
||||
new SocketClientFactory($this->logger),
|
||||
$this->logger,
|
||||
$this->connectionPerIpLimit
|
||||
);
|
||||
$this->httpServer =
|
||||
$httpServer ??
|
||||
new SocketHttpServer(
|
||||
logger: $this->logger,
|
||||
serverSocketFactory: $serverSocketFactory,
|
||||
clientFactory: $clientFactory,
|
||||
httpDriverFactory: new DefaultHttpDriverFactory(logger: $this->logger, streamTimeout: 60)
|
||||
);
|
||||
$this->bindContext = $bindContext ?? (new Socket\BindContext())->withoutTlsContext();
|
||||
$this->errorHandler = $errorHandler ?? new DefaultErrorHandler();
|
||||
|
||||
// инициализация сервиса
|
||||
IPListService::getInstance($this->logger);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ?HttpServer $httpServer
|
||||
* @param ?ErrorHandler $errorHandler
|
||||
* @param ?BindContext $bindContext
|
||||
* @param ?Logger $logger
|
||||
* @throws BufferException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public static function getInstance(
|
||||
HttpServer $httpServer = null,
|
||||
ErrorHandler $errorHandler = null,
|
||||
Socket\BindContext $bindContext = null,
|
||||
Logger $logger = null
|
||||
): Server {
|
||||
return self::$_instance ??= new self($httpServer, $errorHandler, $bindContext, $logger);
|
||||
}
|
||||
|
||||
/**
|
||||
* Запуск веб-сервера
|
||||
* @return void
|
||||
*/
|
||||
public function start(): void {
|
||||
try {
|
||||
$this->httpServer->expose(
|
||||
new Socket\InternetAddress(getEnv('HTTP_HOST') ?? '0.0.0.0', getEnv('HTTP_PORT') ?? 8080),
|
||||
$this->bindContext
|
||||
);
|
||||
//$this->socketHttpServer->expose(
|
||||
// new Socket\InternetAddress('[::]', $_ENV['HTTP_PORT'] ?? 8080),
|
||||
// $this->bindContext
|
||||
//);
|
||||
$this->httpServer->start(HTTPHandler::getInstance($this->logger)->getHandler(), $this->errorHandler);
|
||||
} catch (Socket\SocketException $e) {
|
||||
$this->logger->warning($e->getMessage());
|
||||
} catch (Throwable $e) {
|
||||
$this->logger->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function stop(): void {
|
||||
$this->httpServer->stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HttpServerStatus
|
||||
*/
|
||||
public function getStatus(): HttpServerStatus {
|
||||
return $this->httpServer->getStatus();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user