-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
HttpServer.php
140 lines (118 loc) · 3.33 KB
/
HttpServer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php declare(strict_types = 1);
/**
* HttpServer.php
*
* @license More in LICENSE.md
* @copyright https://www.fastybird.com
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:WebServerPlugin!
* @subpackage Commands
* @since 1.0.0
*
* @date 15.03.20
*/
namespace FastyBird\Plugin\WebServer\Commands;
use FastyBird\Library\Application\Helpers as ApplicationHelpers;
use FastyBird\Library\Metadata\Types as MetadataTypes;
use FastyBird\Plugin\WebServer\Events;
use FastyBird\Plugin\WebServer\Exceptions;
use FastyBird\Plugin\WebServer\Server;
use Nette;
use Psr\EventDispatcher;
use Psr\Log;
use React\EventLoop;
use React\Socket;
use Symfony\Component\Console;
use Symfony\Component\Console\Input;
use Symfony\Component\Console\Output;
use Throwable;
use function file_exists;
use function is_file;
/**
* HTTP server command
*
* @package FastyBird:WebServerPlugin!
* @subpackage Commands
*
* @author Adam Kadlec <adam.kadlec@fastybird.com>
*/
class HttpServer extends Console\Command\Command
{
use Nette\SmartObject;
public const NAME = 'fb:web-server:start';
public function __construct(
private readonly string $serverAddress,
private readonly int $serverPort,
private readonly Server\Factory $serverFactory,
private readonly EventLoop\LoopInterface $eventLoop,
private readonly EventDispatcher\EventDispatcherInterface|null $dispatcher = null,
private readonly Log\LoggerInterface $logger = new Log\NullLogger(),
private readonly string|null $serverCertificate = null,
string|null $name = null,
)
{
parent::__construct($name);
}
/**
* @throws Console\Exception\InvalidArgumentException
*/
protected function configure(): void
{
parent::configure();
$this
->setName(self::NAME)
->setDescription('Start http server');
}
protected function execute(
Input\InputInterface $input,
Output\OutputInterface $output,
): int
{
$this->logger->info(
'Starting HTTP Server',
[
'source' => MetadataTypes\Sources\Plugin::WEB_SERVER->value,
'type' => 'server-command',
],
);
try {
$this->dispatcher?->dispatch(new Events\Startup());
$socketServer = new Socket\SocketServer(
$this->serverAddress . ':' . $this->serverPort,
[],
$this->eventLoop,
);
if ($this->serverCertificate !== null) {
if (
is_file($this->serverCertificate)
&& file_exists($this->serverCertificate)
) {
$socketServer = new Socket\SecureServer($socketServer, $this->eventLoop, [
'local_cert' => $this->serverCertificate,
]);
} else {
throw new Exceptions\InvalidArgument('Provided SSL certificate file could not be loaded');
}
}
$socketServer->on('error', function (Throwable $ex): void {
$this->dispatcher?->dispatch(new Events\Error($ex));
});
$this->serverFactory->create($socketServer);
$this->eventLoop->run();
} catch (Throwable $ex) {
// Log error action reason
$this->logger->error(
'An unhandled error occurred. Stopping HTTP server',
[
'source' => MetadataTypes\Sources\Plugin::WEB_SERVER->value,
'type' => 'server-command',
'exception' => ApplicationHelpers\Logger::buildException($ex),
'cmd' => $this->getName(),
],
);
$this->eventLoop->stop();
return self::FAILURE;
}
return self::SUCCESS;
}
}