Skip to content

Commit

Permalink
onStart parameter in HttpServer plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
luzrain committed Sep 8, 2024
1 parent 904d299 commit 91c0417
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
50 changes: 37 additions & 13 deletions src/Plugin/HttpServer/HttpServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@

use Amp\Future;
use Amp\Http\Server\Driver\HttpDriver;
use Amp\Http\Server\HttpErrorException;
use Amp\Http\Server\Request;
use Amp\Http\Server\RequestHandler\ClosureRequestHandler;
use Amp\Http\Server\Response;
use Amp\Http\Server\RequestHandler;
use Luzrain\PHPStreamServer\Internal\MasterProcess;
use Luzrain\PHPStreamServer\Plugin\PluginInterface;
use Luzrain\PHPStreamServer\WorkerProcess;
use function Amp\async;

final readonly class HttpServer implements PluginInterface
{
/**
* @param Listen|string|array<Listen> $listen
* @param \Closure(WorkerProcess): RequestHandler $onStart
*/
public function __construct(
private Listen|array $listen,
private Listen|string|array $listen,
private \Closure $onStart,
private string $name = 'HTTP Server',
private int $count = 1,
private bool $reloadable = true,
Expand All @@ -44,16 +46,18 @@ public function start(MasterProcess $masterProcess): void
user: $this->user,
group: $this->group,
onStart: function (WorkerProcess $worker) {
$requestHandler = new ClosureRequestHandler(function (Request $request) : Response {
return match ($request->getUri()->getPath()) {
'/' => new Response(body: 'Hello world1'),
'/ping' => new Response(body: 'pong2'),
default => throw new HttpErrorException(404),
};
});
$requestHandler = ($this->onStart)($worker);

if (!$requestHandler instanceof RequestHandler) {
throw new \RuntimeException(sprintf(
'onStart() closure: Return value must be of type %s, %s returned',
RequestHandler::class,
\get_debug_type($requestHandler)),
);
}

$worker->startWorkerModule(new HttpServerModule(
listen: $this->listen,
listen: self::createListenList($this->listen),
requestHandler: $requestHandler,
middleware: $this->middleware,
connectionLimit: $this->connectionLimit,
Expand All @@ -72,4 +76,24 @@ public function stop(): Future
{
return async(static fn() => null);
}

/**
* @return list<Listen>
*/
public static function createListenList(self|string|array $listen): array
{
$listen = \is_array($listen) ? $listen : [$listen];
$ret = [];
foreach ($listen as $listenItem) {
if ($listenItem instanceof Listen) {
$ret[] = $listenItem;
} elseif (\is_string($listenItem)) {
$ret[] = new Listen($listenItem);
} else {
throw new \InvalidArgumentException('Invalid listen');
}
}

return $ret;
}
}
4 changes: 4 additions & 0 deletions src/Plugin/HttpServer/Listen.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public function __construct(
$this->host = $p['host'] ?? $p;
$this->port = $p['port'] ?? ($tls ? 443 : 80);

if (\str_contains($listen, '://')) {
throw new \InvalidArgumentException('Listen should not contain schema');
}

if ($this->port < 0 || $this->port > 65535) {
throw new \InvalidArgumentException('Port number must be an integer between 0 and 65535; got ' . $this->port);
}
Expand Down

0 comments on commit 91c0417

Please sign in to comment.