Skip to content

Instantly share code, notes, and snippets.

@l0gicgate
Last active June 16, 2020 02:30
Show Gist options
  • Save l0gicgate/9fc61f24a132f772f6b6e0b8cace9b2e to your computer and use it in GitHub Desktop.
Save l0gicgate/9fc61f24a132f772f6b6e0b8cace9b2e to your computer and use it in GitHub Desktop.
Roadrunner Worker
<?php
declare(strict_types=1);
use DI\Container;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Slim\Factory\AppFactory;
use Slim\Factory\ServerRequestCreatorFactory;
use Slim\Psr7\Factory\StreamFactory;
use Slim\Psr7\Factory\UploadedFileFactory;
use Spiral\Goridge\StreamRelay;
use Spiral\RoadRunner\PSR7Client;
use Spiral\RoadRunner\Worker;
ini_set('display_errors', 'stderr');
require __DIR__ . '/../vendor/autoload.php';
// Set Container
$container = new Container();
AppFactory::setContainer($container);
// Create App
$app = AppFactory::create();
// Factories
$streamFactory = new StreamFactory();
$uploadedFileFactory = new UploadedFileFactory();
$serverRequestCreator = ServerRequestCreatorFactory::create();
// Routes
$app->get('/', function (ServerRequestInterface $request, ResponseInterface $response) {
$response->getBody()->write('Hello world from RoadRunner and Slim!');
return $response;
});
// RoadRunner
$relay = new StreamRelay(STDIN, STDOUT);
$worker = new Worker($relay);
$psr7 = new PSR7Client($worker, $serverRequestCreator, $streamFactory, $uploadedFileFactory);
while ($request = $psr7->acceptRequest()) {
try {
$response = $app->handle($request);
$psr7->respond($response);
} catch (\Throwable $e) {
$psr7->getWorker()->error((string)$e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment