Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmorbo committed Nov 27, 2023
1 parent a0c742e commit 38c44e7
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor
data/data.db
docker/data
11 changes: 11 additions & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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"
10 changes: 7 additions & 3 deletions src/Controller/InstanceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down Expand Up @@ -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) {
Expand Down
44 changes: 20 additions & 24 deletions src/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
2 changes: 0 additions & 2 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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', [
Expand Down

0 comments on commit 38c44e7

Please sign in to comment.