Skip to content

Commit

Permalink
Fulfill dockerfile and api point
Browse files Browse the repository at this point in the history
  • Loading branch information
wapmorgan committed Aug 7, 2023
1 parent 7c3f75d commit 7aface2
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ghcr.io/roadrunner-server/roadrunner:2023.1.0 AS roadrunner
FROM ghcr.io/roadrunner-server/roadrunner:2023.2.1 AS roadrunner
FROM composer:2.4.4 AS composer
FROM php:8.1-cli
COPY --from=composer /usr/bin/composer /usr/bin/composer
Expand Down
80 changes: 69 additions & 11 deletions web/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,54 @@

use Nyholm\Psr7\Response;
use Nyholm\Psr7\Factory\Psr17Factory;

use Spiral\RoadRunner\Worker;
use Spiral\RoadRunner\Http\PSR7Worker;

// Create new RoadRunner worker from global environment
$worker = Worker::create();

// Create common PSR-17 HTTP factory
$factory = new Psr17Factory();

$psr7 = new PSR7Worker($worker, $factory, $factory, $factory);

while (true) {
$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) {
$r->addGroup('/ru', function (\FastRoute\RouteCollector $r) {
$r->addRoute('GET', '/cases', ['Russian', 'cases']);
$r->addRoute('GET', '/name', ['Russian', 'name']);
$r->addRoute('GET', '/detectGender', ['Russian', 'detectGender']);
$r->addRoute('GET', '/pluralize', ['Russian', 'pluralize']);
});
});

class Russian {
public function cases()
{
return \morphos\CasesHelper::getAllCases();
}

public function name(array $args)
{
return \morphos\Russian\inflectName($args['name'], $args['case'] ?? null, $args['gender'] ?? null);
}

public function detectGender(array $args)
{
return \morphos\Russian\detectGender($args['name']);
}

public function pluralize(array $args)
{
return \morphos\Russian\pluralize($args['count'], $args['word'], $args['animateness'] ?? false, $args['case'] ?? null);
}
}

$handlers = [];

do {
try {
$request = $psr7->waitRequest();
if ($request === null) {
break;
}
} catch (\Throwable $e) {
// Although the PSR-17 specification clearly states that there can be
// no exceptions when creating a request, however, some implementations
Expand All @@ -30,12 +63,37 @@
}

try {
// Here is where the call to your application code will be located.
// For example:
// $response = $app->send($request);
//
// Reply by the 200 OK response
$psr7->respond(new Response(200, [], 'Hello RoadRunner!'));
$routeInfo = $dispatcher->dispatch($request->getMethod(), $request->getUri()->getPath());
switch ($routeInfo[0]) {
case FastRoute\Dispatcher::NOT_FOUND:
// ... 404 Not Found
$psr7->respond(new Response(404));
break;
case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
$allowedMethods = $routeInfo[1];
// ... 405 Method Not Allowed
$psr7->respond(new Response(405));
break;
case FastRoute\Dispatcher::FOUND:
if (!isset($handlers[$routeInfo[1][0]])) {
$handlers[$routeInfo[1][0]] = new $routeInfo[1][0];
}
parse_str($request->getUri()->getQuery(), $args);
// Reply by the 200 OK response
$psr7->respond(
new Response(200, [
'Content-Type' => 'application/json',
], json_encode(
[
'result' => call_user_func([
$handlers[$routeInfo[1][0]],
$routeInfo[1][1]
], $args)
]
))
);
break;
}
} catch (\Throwable $e) {
// In case of any exceptions in the application code, you should handle
// them and inform the client about the presence of a server error.
Expand All @@ -47,4 +105,4 @@
// of the request failed.
$psr7->getWorker()->error((string)$e);
}
}
} while (isset($request));

0 comments on commit 7aface2

Please sign in to comment.