-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[HttpServer] Add RoadRunnerHttpDispatcher
- Loading branch information
1 parent
5a33ed3
commit a500a44
Showing
3 changed files
with
81 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters