1
|
|
|
<?php declare(strict_types = 1); |
|
|
|
|
2
|
|
|
|
3
|
|
|
use Aerys\Host; |
4
|
|
|
use Amp\Redis\Client; |
5
|
|
|
use Auryn\Injector; |
6
|
|
|
use ekinhbayar\GitAmp\Github\Credentials; |
7
|
|
|
use ekinhbayar\GitAmp\Storage\Counter; |
8
|
|
|
use ekinhbayar\GitAmp\Storage\NativeCounter; |
9
|
|
|
use ekinhbayar\GitAmp\Websocket\Handler; |
10
|
|
|
use Monolog\Logger; |
11
|
|
|
use Monolog\Handler\StreamHandler; |
12
|
|
|
use Psr\Log\LoggerInterface; |
13
|
|
|
use function Aerys\root; |
14
|
|
|
use function Aerys\router; |
15
|
|
|
use function Aerys\websocket; |
16
|
|
|
|
17
|
|
|
$configuration = require_once __DIR__ . '/config.php'; |
18
|
|
|
|
19
|
|
|
$injector = new Injector; |
20
|
|
|
|
21
|
|
|
$injector->alias(Counter::class, NativeCounter::class); |
22
|
|
|
|
23
|
|
|
$injector->alias(Credentials::class, get_class($configuration['github'])); |
24
|
|
|
|
25
|
|
|
$injector->share($configuration['github']); |
26
|
|
|
|
27
|
|
|
$injector->alias(LoggerInterface::class, Logger::class); |
28
|
|
|
$injector->share(Logger::class); |
29
|
|
|
|
30
|
|
|
$injector->delegate(Logger::class, function() use ($configuration) { |
31
|
|
|
$logger = new Logger('gitamp'); |
32
|
|
|
|
33
|
|
|
$logger->pushHandler(new StreamHandler('php://stdout', $configuration['logLevel'])); |
34
|
|
|
|
35
|
|
|
return $logger; |
36
|
|
|
}); |
37
|
|
|
|
38
|
|
|
// @todo unuglify this |
|
|
|
|
39
|
|
|
if (isset($configuration['ssl'])) { |
40
|
|
|
$origin = 'https://' . $configuration['hostname']; |
41
|
|
|
|
42
|
|
View Code Duplication |
if ($configuration['ssl']['port'] !== 443) { |
|
|
|
|
43
|
|
|
$origin .= ':' . $configuration['ssl']['port']; |
44
|
|
|
} |
45
|
|
|
} else { |
46
|
|
|
$origin = 'http://' . $configuration['hostname']; |
47
|
|
|
|
48
|
|
View Code Duplication |
if ($configuration['expose']['port'] !== 80) { |
|
|
|
|
49
|
|
|
$origin .= ':' . $configuration['expose']['port']; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$injector->define(Handler::class, [ |
54
|
|
|
':origin' => $origin, |
55
|
|
|
]); |
56
|
|
|
|
57
|
|
|
$injector->define(Client::class, [ |
58
|
|
|
':uri' => $configuration['redis'] |
59
|
|
|
]); |
60
|
|
|
|
61
|
|
|
$websocket = $injector->make(Handler::class); |
62
|
|
|
|
63
|
|
|
$router = router()->get('/ws', websocket($websocket)); |
64
|
|
|
|
65
|
|
|
$requestLogger = new class implements \Aerys\Bootable { |
|
|
|
|
66
|
|
|
private $logger; |
67
|
|
|
|
68
|
|
|
public function boot(\Aerys\Server $server, \Psr\Log\LoggerInterface $logger) |
69
|
|
|
{ |
70
|
|
|
$this->logger = $logger; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function __invoke(\Aerys\Request $req, \Aerys\Response $res) |
74
|
|
|
{ |
75
|
|
|
$this->logger->debug('Incoming request', [ |
76
|
|
|
'method' => $req->getMethod(), |
77
|
|
|
'uri' => $req->getUri(), |
78
|
|
|
'headers' => $req->getAllHeaders(), |
79
|
|
|
'parameters' => $req->getAllParams(), |
80
|
|
|
'body' => $req->getBody(), |
81
|
|
|
]); |
82
|
|
|
} |
83
|
|
|
}; |
84
|
|
|
|
85
|
|
|
if (isset($configuration['ssl'])) { |
86
|
|
|
(new Host()) |
87
|
|
|
->name($configuration['hostname']) |
88
|
|
|
->expose($configuration['expose']['ip'], $configuration['expose']['port']) |
89
|
|
|
->use($requestLogger) |
90
|
|
|
->redirect('https://' . $configuration['hostname']) |
91
|
|
|
; |
92
|
|
|
|
93
|
|
|
(new Host()) |
94
|
|
|
->name($configuration['hostname']) |
95
|
|
|
->expose($configuration['ssl']['ip'], $configuration['ssl']['port']) |
96
|
|
|
->encrypt($configuration['ssl']['certificate'], $configuration['ssl']['key']) |
97
|
|
|
->use($requestLogger) |
98
|
|
|
->use($router) |
99
|
|
|
->use(root(__DIR__ . '/public')) |
|
|
|
|
100
|
|
|
; |
101
|
|
|
} else { |
102
|
|
|
(new Host()) |
103
|
|
|
->name($configuration['hostname']) |
104
|
|
|
->expose($configuration['expose']['ip'], $configuration['expose']['port']) |
105
|
|
|
->use($requestLogger) |
106
|
|
|
->use($router) |
107
|
|
|
->use(root(__DIR__ . '/public')) |
|
|
|
|
108
|
|
|
; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$logger = $injector->make(LoggerInterface::class); |
112
|
|
|
|
113
|
|
|
\Amp\onError(function(Throwable $e) use ($logger) { |
114
|
|
|
$logger->emergency('GitAmp blew up', ['exception' => $e]); |
115
|
|
|
}); |
116
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.