-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update endpoints; use the same Service for Websockets and HTTP reques…
…ts from the Frontend
- Loading branch information
Showing
13 changed files
with
182 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Buggregator\Trap\Sender\Frontend\Http; | ||
|
||
use Buggregator\Trap\Handler\Http\Middleware; | ||
use Buggregator\Trap\Handler\Router\Method; | ||
use Buggregator\Trap\Handler\Router\Router as CommonRouter; | ||
use Buggregator\Trap\Logger; | ||
use Buggregator\Trap\Sender\Frontend\EventsStorage; | ||
use Buggregator\Trap\Sender\Frontend\Service; | ||
use Buggregator\Trap\Support\Json; | ||
use Nyholm\Psr7\Response; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
/** | ||
* @internal | ||
* @psalm-internal Buggregator\Trap | ||
*/ | ||
final class Router implements Middleware | ||
{ | ||
private readonly CommonRouter $router; | ||
|
||
public function __construct( | ||
private readonly Logger $logger, | ||
EventsStorage $eventsStorage, | ||
) { | ||
$service = new Service($logger, $eventsStorage); | ||
$this->router = CommonRouter::new($service); | ||
} | ||
|
||
public function handle(ServerRequestInterface $request, callable $next): ResponseInterface | ||
{ | ||
try { | ||
$path = \trim($request->getUri()->getPath(), '/'); | ||
$method = $request->getMethod(); | ||
|
||
$handler = $this->router->match(Method::fromString($method), $path); | ||
|
||
\trap($path, $method, $handler)->if(Method::fromString($method) === Method::Delete); | ||
if ($handler === null) { | ||
return new Response(404); | ||
} | ||
|
||
$message = $handler($request); | ||
|
||
return new Response( | ||
200, | ||
[ | ||
'Content-Type' => 'application/json', | ||
'Cache-Control' => 'no-cache', | ||
], | ||
Json::encode($message), | ||
); | ||
} catch (\Throwable $e) { | ||
$this->logger->exception($e); | ||
|
||
return new Response( | ||
500, | ||
[ | ||
'Content-Type' => 'application/json', | ||
'Cache-Control' => 'no-cache', | ||
], | ||
Json::encode(['error' => $e->getMessage()]), | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Buggregator\Trap\Sender\Frontend\Message; | ||
|
||
final class EventCollection implements \JsonSerializable | ||
{ | ||
/** | ||
* @param array<array-key, Event> $events | ||
* @param array<array-key, mixed> $meta | ||
*/ | ||
public function __construct( | ||
public readonly array $events, | ||
public readonly array $meta = [], | ||
) { | ||
} | ||
|
||
public function jsonSerialize(): array | ||
{ | ||
return [ | ||
'data' => $this->events, | ||
'meta' => $this->meta + ['grid' => []], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Buggregator\Trap\Sender\Frontend\Message; | ||
|
||
use Buggregator\Trap\Info; | ||
use JsonSerializable; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class Version implements JsonSerializable | ||
{ | ||
public function __construct( | ||
public readonly string $number = Info::VERSION, | ||
) { | ||
} | ||
|
||
public function jsonSerialize(): array | ||
{ | ||
return [ | ||
'version' => $this->number, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters