1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Antidot\React; |
6
|
|
|
|
7
|
|
|
use Antidot\Application\Http\Application; |
8
|
|
|
use Antidot\Application\Http\Response\ErrorResponseGenerator; |
9
|
|
|
use Antidot\React\ReactApplication; |
10
|
|
|
use Laminas\Diactoros\Response\HtmlResponse; |
11
|
|
|
use Psr\Container\ContainerInterface; |
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
13
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
14
|
|
|
use Ramsey\Uuid\Uuid; |
15
|
|
|
use React\EventLoop\LoopInterface; |
16
|
|
|
use React\Http\Server; |
17
|
|
|
use React\Http\Middleware\LimitConcurrentRequestsMiddleware; |
18
|
|
|
use React\Http\Middleware\RequestBodyBufferMiddleware; |
19
|
|
|
use React\Http\Middleware\RequestBodyParserMiddleware; |
20
|
|
|
use React\Http\Middleware\StreamingRequestMiddleware; |
21
|
|
|
use Throwable; |
22
|
|
|
use function React\Promise\resolve; |
23
|
|
|
|
24
|
|
|
class ServerFactory |
25
|
|
|
{ |
26
|
1 |
|
public function __invoke(ContainerInterface $container): Server |
27
|
|
|
{ |
28
|
1 |
|
$application = $container->get(Application::class); |
29
|
|
|
/** @var ErrorResponseGenerator $errorResponseGenerator */ |
30
|
1 |
|
$errorResponseGenerator = $container->get(ErrorResponseGenerator::class); |
|
|
|
|
31
|
1 |
|
assert($application instanceof ReactApplication); |
32
|
|
|
/** @var LoopInterface $loop */ |
33
|
1 |
|
$loop = $container->get(LoopInterface::class); |
34
|
|
|
/** @var array<string, array> $globalConfig */ |
35
|
1 |
|
$globalConfig = $container->get('config'); |
36
|
|
|
/** @var array<string, int|null> $config */ |
37
|
1 |
|
$config = $globalConfig['server']; |
38
|
|
|
|
39
|
1 |
|
$server = new Server( |
40
|
1 |
|
$loop, |
41
|
1 |
|
new StreamingRequestMiddleware(), |
42
|
1 |
|
new LimitConcurrentRequestsMiddleware(($config['max_concurrency']) ?? 100), |
43
|
1 |
|
new RequestBodyBufferMiddleware($config['buffer_size'] ?? 4 * 1024 * 1024), // 4 MiB |
44
|
1 |
|
new RequestBodyParserMiddleware(), |
45
|
1 |
|
static fn (ServerRequestInterface $request): ResponseInterface => $application->handle($request) |
46
|
1 |
|
); |
47
|
|
|
|
48
|
1 |
|
return $server; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|