1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ekinhbayar\GitAmp; |
4
|
|
|
|
5
|
|
|
use Amp\Http\Client\HttpClientBuilder; |
6
|
|
|
use Amp\Http\Server\HttpServer; |
7
|
|
|
use Amp\Http\Server\Router; |
8
|
|
|
use Amp\Http\Server\StaticContent\DocumentRoot; |
9
|
|
|
use Amp\Promise; |
10
|
|
|
use Amp\Socket\BindContext; |
11
|
|
|
use Amp\Socket\Server as SocketServer; |
12
|
|
|
use Amp\Socket\ServerTlsContext; |
13
|
|
|
use Amp\Websocket\Server\Websocket; |
14
|
|
|
use Psr\Log\LoggerInterface; |
15
|
|
|
use ekinhbayar\GitAmp\Event\Factory as EventFactory; |
16
|
|
|
use ekinhbayar\GitAmp\Provider\GitHub; |
17
|
|
|
use ekinhbayar\GitAmp\Response\Factory as EventCollectionFactory; |
18
|
|
|
use ekinhbayar\GitAmp\Websocket\Handler; |
19
|
|
|
|
20
|
|
|
final class Server |
21
|
|
|
{ |
22
|
|
|
private LoggerInterface $logger; |
23
|
|
|
|
24
|
|
|
private Configuration $configuration; |
25
|
|
|
|
26
|
1 |
|
public function __construct(LoggerInterface $logger, Configuration $configuration) |
27
|
|
|
{ |
28
|
1 |
|
$this->logger = $logger; |
29
|
1 |
|
$this->configuration = $configuration; |
30
|
1 |
|
} |
31
|
|
|
|
32
|
1 |
|
public function start(): Promise |
33
|
|
|
{ |
34
|
1 |
|
$server = new HttpServer($this->getSockets(), $this->getRouter(), $this->logger); |
35
|
|
|
|
36
|
1 |
|
return $server->start(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return array<SocketServer> |
41
|
|
|
*/ |
42
|
1 |
|
private function getSockets(): array |
43
|
|
|
{ |
44
|
1 |
|
$sockets = array_map( |
45
|
1 |
|
fn (ServerAddress $address) => SocketServer::listen($address->getUri()), |
46
|
1 |
|
$this->configuration->getServerAddresses(), |
47
|
|
|
); |
48
|
|
|
|
49
|
1 |
|
$sockets = array_merge($sockets, array_map( |
50
|
1 |
|
fn (SslServerAddress $address) => SocketServer::listen( |
51
|
|
|
$address->getUri(), |
52
|
|
|
(new BindContext()) |
53
|
|
|
->withTlsContext((new ServerTlsContext())->withDefaultCertificate($address->getCertificate())), |
54
|
1 |
|
), |
55
|
1 |
|
$this->configuration->getSslServerAddresses(), |
56
|
|
|
)); |
57
|
|
|
|
58
|
1 |
|
return $sockets; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
private function getRouter(): Router |
62
|
|
|
{ |
63
|
1 |
|
$router = new Router(); |
64
|
|
|
|
65
|
1 |
|
$router->addRoute('GET', '/ws', $this->getWebSocket()); |
66
|
1 |
|
$router->setFallback(new DocumentRoot(__DIR__ . '/../public')); |
67
|
|
|
|
68
|
1 |
|
return $router; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
private function getWebSocket(): Websocket |
72
|
|
|
{ |
73
|
1 |
|
$eventCollectionFactory = new EventCollectionFactory( |
74
|
1 |
|
new EventFactory($this->configuration->getSpecialRepositories()), |
75
|
1 |
|
$this->logger, |
76
|
|
|
); |
77
|
|
|
|
78
|
1 |
|
$gitHubListener = new GitHub( |
79
|
1 |
|
HttpClientBuilder::buildDefault(), |
80
|
1 |
|
$this->configuration->getGithubToken(), |
81
|
|
|
$eventCollectionFactory, |
82
|
1 |
|
$this->logger, |
83
|
|
|
); |
84
|
|
|
|
85
|
1 |
|
$clientHandler = new Handler( |
86
|
1 |
|
$gitHubListener, |
87
|
1 |
|
$this->configuration, |
88
|
1 |
|
$eventCollectionFactory->build(), |
89
|
1 |
|
$this->logger, |
90
|
|
|
); |
91
|
|
|
|
92
|
1 |
|
return new Websocket($clientHandler); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|