From 38c44e7b363175f112428f6a15de21255a656c99 Mon Sep 17 00:00:00 2001 From: Aleksandr Nikitin Date: Mon, 27 Nov 2023 20:12:08 +0300 Subject: [PATCH] fixes --- .gitignore | 1 + docker/docker-compose.yml | 11 +++++++ src/Controller/InstanceController.php | 10 ++++-- src/Router/Router.php | 44 ++++++++++++--------------- src/Server.php | 2 -- 5 files changed, 39 insertions(+), 29 deletions(-) create mode 100644 docker/docker-compose.yml diff --git a/.gitignore b/.gitignore index f9fc6e1..6932011 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ vendor data/data.db +docker/data \ No newline at end of file diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml new file mode 100644 index 0000000..93e2f1a --- /dev/null +++ b/docker/docker-compose.yml @@ -0,0 +1,11 @@ +services: + react-trassir: + container_name: react-trassir + restart: unless-stopped + image: ghcr.io/alexmorbo/react-trassir:master + environment: + - TIMEZONE=Europe/Moscow + ports: + - "8080:8080" + volumes: + - "./data:/app/data" \ No newline at end of file diff --git a/src/Controller/InstanceController.php b/src/Controller/InstanceController.php index 6f20385..abf1a66 100644 --- a/src/Controller/InstanceController.php +++ b/src/Controller/InstanceController.php @@ -133,7 +133,7 @@ public function addInstance(ServerRequestInterface $request): PromiseInterface $request->hasHeader('Content-Type') && $request->getHeaderLine('Content-Type') === 'application/json' ) { - $input = json_decode($request->getBody()->getContents(), true); + $input = json_decode((string)$request->getBody(), true); if (json_last_error() !== JSON_ERROR_NONE) { throw new BadRequestHttpException('Invalid JSON'); } @@ -309,8 +309,12 @@ function ($video) use ($request) { ); } - public function getChannelArchive(string $instanceId, string $channelId, string $start, string $end): PromiseInterface - { + public function getChannelArchive( + string $instanceId, + string $channelId, + string $start, + string $end + ): PromiseInterface { return $this->trassirHelper->getInstance($instanceId) ->then( function (Instance $instance) use ($channelId) { diff --git a/src/Router/Router.php b/src/Router/Router.php index ce52341..034ad58 100644 --- a/src/Router/Router.php +++ b/src/Router/Router.php @@ -6,6 +6,9 @@ use React\Http\Message\Response; use React\Promise\PromiseInterface; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; + +use function React\Promise\reject; use function React\Promise\resolve; class Router @@ -170,34 +173,27 @@ public function resolve(string $url, string $method = 'get'): ?array public function handle(ServerRequestInterface $request): PromiseInterface { - $match = $this->resolve($request->getUri()->getPath(), $request->getMethod()); - if (!$match) { - return resolve( - Response::json(['status' => 'error', 'error' => 'Route not found']) - ); - } + try { + $match = $this->resolve($request->getUri()->getPath(), $request->getMethod()); + if (!$match) { + throw new NotFoundHttpException('Route not found'); + } - $routeItem = $match['item']; - $action = $routeItem->action; + $routeItem = $match['item']; + $action = $routeItem->action; - if (!is_callable($action)) { - return resolve( - Response::json( - ['status' => 'error', 'error' => 'Internal Error', 'message' => '#1'] - ) - ); - } + if (!is_callable($action)) { + throw new \Exception('Internal Error #1'); + } - $response = $action($request, ...array_values($match['params'])); + $response = $action($request, ...array_values($match['params'])); + if (!$response instanceof PromiseInterface) { + throw new \Exception('Internal Error #2'); + } - if (!$response instanceof PromiseInterface) { - return resolve( - Response::json( - ['status' => 'error', 'error' => 'Internal Error', 'message' => '#2'] - ) - ); + return $response; + } catch (\Throwable $e) { + return reject($e); } - - return $response; } } \ No newline at end of file diff --git a/src/Server.php b/src/Server.php index 7aad097..308aef6 100644 --- a/src/Server.php +++ b/src/Server.php @@ -20,7 +20,6 @@ use React\EventLoop\LoopInterface; use React\Http\HttpServer; use React\Http\Message\Response; -use React\Http\Middleware\StreamingRequestMiddleware; use React\Promise\PromiseInterface; use React\Socket\SocketServer; use Symfony\Component\Console\Command\Command; @@ -127,7 +126,6 @@ private function initServer(string $ip, int $port): int $router = $this->getRouter(); $http = new HttpServer( - new StreamingRequestMiddleware(), function (ServerRequestInterface $request) use ($router) { $id = uniqid(); $this->logger->debug('New request', [