Skip to content

Commit

Permalink
[HttpServer] Add RoadRunnerHttpDispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
il-masaru-yamagishi committed Aug 17, 2024
1 parent 5a33ed3 commit a500a44
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
"cakephp/chronos": "^3.0.0",
"monolog/monolog": "^3.7.0",
"nesbot/carbon": "^3.0.0",
"phpunit/phpunit": "^11.2.9"
"phpunit/phpunit": "^11.2.9",
"spiral/roadrunner-http": "^3.5.0"
},
"scripts": {
"lint": [
Expand Down
70 changes: 70 additions & 0 deletions src/HttpServer/RoadRunnerHttpDispatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

/**
* @author Masaru Yamagishi <[email protected]>
* @license Apache-2.0
*/

namespace Rayleigh\HttpServer;

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface;
use Rayleigh\HttpFactory\ServerRequestFactory;
use Rayleigh\HttpFactory\StreamFactory;
use Rayleigh\HttpFactory\UploadedFileFactory;
use Rayleigh\HttpMessage\Response;
use Spiral\RoadRunner\Http\PSR7Worker;
use Spiral\RoadRunner\Worker;

/**
* RoadRunner HTTP Dispatcher
* @package Rayleigh\HttpServer
* @link https://docs.roadrunner.dev/docs/php-worker/worker
*/
class HttpDispatcher
{
public function __construct()
{
if (\class_exists(PSR7Worker::class) === false) {
throw new \RuntimeException('You need to require "spiral/roadrunner-http" package');
}
}

public function serve(LoggerInterface $logger, RequestHandlerInterface $handler): void
{
$worker = new PSR7Worker(
Worker::create(interceptSideEffects: true, logger: $logger),
new ServerRequestFactory(),
new StreamFactory(),
new UploadedFileFactory(),
);

while (true) {
try {
$request = $worker->waitRequest();
if ($request === null) {
break; // @codeCoverageIgnore
}
} catch (\Throwable $e) {
// @codeCoverageIgnoreStart
$logger->error('Failed to receive request', ['exception' => $e]);
$worker->respond(new Response(400));
continue;
// @codeCoverageIgnoreEnd
}

\assert($request instanceof ServerRequestInterface);

try {
$response = $handler->handle($request);
$worker->respond($response);
} catch (\Throwable $e) {
$logger->error('Unhandled throwable', ['exception' => $e]);
$worker->respond(new Response(500));
}
}
}
}
11 changes: 9 additions & 2 deletions src/HttpServer/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,17 @@
"psr/http-message": "^2.0.0",
"psr/http-server-handler": "^1.0.0",
"psr/http-server-middleware": "^1.0.0",
"psr/log": "^2.0.0|^3.0.0",
"rayleigh/http-factory": "^0.0.1",
"rayleigh/http-message": "^0.0.1"
},
"require-dev": {},
"suggest": {},
"require-dev": {
"spiral/roadrunner-http": "^3.5.0"
},
"suggest": {
"ext-protobuf": "With RoadRunner to huge performance improvement",
"spiral/roadrunner-http": "RoadRunner HTTP Server integration"
},
"autoload": {
"psr-4": {
"Rayleigh\\HttpServer\\": ""
Expand Down

0 comments on commit a500a44

Please sign in to comment.