Skip to content

Commit

Permalink
Add base for morphos microservice
Browse files Browse the repository at this point in the history
  • Loading branch information
wapmorgan committed Aug 6, 2023
1 parent ef87c75 commit abe1d72
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .rr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '3'
server:
command: "php web/api.php"

http:
address: 0.0.0.0:8080
pool:
num_workers: ${NUM_WORKERS}

rpc:
listen: tcp://127.0.0.1:6001
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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
COPY --from=roadrunner /usr/bin/rr /usr/local/bin/rr
RUN docker-php-ext-install -j$(nproc) sockets && apt update && apt install unzip
WORKDIR /app
COPY . /app
RUN composer install --prefer-dist --optimize-autoloader && composer require --prefer-dist --optimize-autoloader spiral/roadrunner-http nyholm/psr7
ENTRYPOINT rr --WorkDir /app --debug serve
EXPOSE 8080
ENV NUM_WORKERS 4
50 changes: 50 additions & 0 deletions web/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
require __DIR__ . '/../vendor/autoload.php';

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) {
try {
$request = $psr7->waitRequest();
} catch (\Throwable $e) {
// Although the PSR-17 specification clearly states that there can be
// no exceptions when creating a request, however, some implementations
// may violate this rule. Therefore, it is recommended to process the
// incoming request for errors.
//
// Send "Bad Request" response.
$psr7->respond(new Response(400));
continue;
}

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!'));
} 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.
//
// Reply by the 500 Internal Server Error response
$psr7->respond(new Response(500, [], 'Something Went Wrong!'));

// Additionally, we can inform the RoadRunner that the processing
// of the request failed.
$psr7->getWorker()->error((string)$e);
}
}

0 comments on commit abe1d72

Please sign in to comment.