Skip to content

Commit 91c0417

Browse files
committed
onStart parameter in HttpServer plugin
1 parent 904d299 commit 91c0417

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

src/Plugin/HttpServer/HttpServer.php

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@
66

77
use Amp\Future;
88
use Amp\Http\Server\Driver\HttpDriver;
9-
use Amp\Http\Server\HttpErrorException;
10-
use Amp\Http\Server\Request;
11-
use Amp\Http\Server\RequestHandler\ClosureRequestHandler;
12-
use Amp\Http\Server\Response;
9+
use Amp\Http\Server\RequestHandler;
1310
use Luzrain\PHPStreamServer\Internal\MasterProcess;
1411
use Luzrain\PHPStreamServer\Plugin\PluginInterface;
1512
use Luzrain\PHPStreamServer\WorkerProcess;
1613
use function Amp\async;
1714

1815
final readonly class HttpServer implements PluginInterface
1916
{
17+
/**
18+
* @param Listen|string|array<Listen> $listen
19+
* @param \Closure(WorkerProcess): RequestHandler $onStart
20+
*/
2021
public function __construct(
21-
private Listen|array $listen,
22+
private Listen|string|array $listen,
23+
private \Closure $onStart,
2224
private string $name = 'HTTP Server',
2325
private int $count = 1,
2426
private bool $reloadable = true,
@@ -44,16 +46,18 @@ public function start(MasterProcess $masterProcess): void
4446
user: $this->user,
4547
group: $this->group,
4648
onStart: function (WorkerProcess $worker) {
47-
$requestHandler = new ClosureRequestHandler(function (Request $request) : Response {
48-
return match ($request->getUri()->getPath()) {
49-
'/' => new Response(body: 'Hello world1'),
50-
'/ping' => new Response(body: 'pong2'),
51-
default => throw new HttpErrorException(404),
52-
};
53-
});
49+
$requestHandler = ($this->onStart)($worker);
50+
51+
if (!$requestHandler instanceof RequestHandler) {
52+
throw new \RuntimeException(sprintf(
53+
'onStart() closure: Return value must be of type %s, %s returned',
54+
RequestHandler::class,
55+
\get_debug_type($requestHandler)),
56+
);
57+
}
5458

5559
$worker->startWorkerModule(new HttpServerModule(
56-
listen: $this->listen,
60+
listen: self::createListenList($this->listen),
5761
requestHandler: $requestHandler,
5862
middleware: $this->middleware,
5963
connectionLimit: $this->connectionLimit,
@@ -72,4 +76,24 @@ public function stop(): Future
7276
{
7377
return async(static fn() => null);
7478
}
79+
80+
/**
81+
* @return list<Listen>
82+
*/
83+
public static function createListenList(self|string|array $listen): array
84+
{
85+
$listen = \is_array($listen) ? $listen : [$listen];
86+
$ret = [];
87+
foreach ($listen as $listenItem) {
88+
if ($listenItem instanceof Listen) {
89+
$ret[] = $listenItem;
90+
} elseif (\is_string($listenItem)) {
91+
$ret[] = new Listen($listenItem);
92+
} else {
93+
throw new \InvalidArgumentException('Invalid listen');
94+
}
95+
}
96+
97+
return $ret;
98+
}
7599
}

src/Plugin/HttpServer/Listen.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public function __construct(
1919
$this->host = $p['host'] ?? $p;
2020
$this->port = $p['port'] ?? ($tls ? 443 : 80);
2121

22+
if (\str_contains($listen, '://')) {
23+
throw new \InvalidArgumentException('Listen should not contain schema');
24+
}
25+
2226
if ($this->port < 0 || $this->port > 65535) {
2327
throw new \InvalidArgumentException('Port number must be an integer between 0 and 65535; got ' . $this->port);
2428
}

0 commit comments

Comments
 (0)