Skip to content

Commit

Permalink
feat(frontend): frontend may be used on debug port
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Jun 15, 2024
1 parent 5152a6d commit 3b7e4df
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 22 deletions.
52 changes: 43 additions & 9 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,16 @@ public function __construct(
$this->buffer = new Buffer(bufferSize: 10485760, timer: 0.1);
$this->container->set($this->buffer);

$inspector = $container->make(Inspector::class, [
// Frontend
$feConfig = $this->container->get(FrontendConfig::class);
$feSeparated = $withFrontend && !\in_array(
$feConfig->port,
\array_map(static fn(SocketServer $item): ?int => $item->type === 'tcp' ? $item->port : null, $map, ),
true,
);
$withFrontend and $this->configureFrontend($feSeparated);

$inspectorWithFrontend = $inspector = $container->make(Inspector::class, [
// new Traffic\Dispatcher\WebSocket(),
new Traffic\Dispatcher\VarDumper(),
new Traffic\Dispatcher\Http(
Expand All @@ -71,11 +80,32 @@ public function __construct(
]);
$this->processors[] = $inspector;

$withFrontend and $this->configureFrontend();
if (!$feSeparated) {
$inspectorWithFrontend = $container->make(Inspector::class, [
new Traffic\Dispatcher\VarDumper(),
new Traffic\Dispatcher\Http(
[
$this->container->get(Sender\Frontend\Http\Pipeline::class),
$this->container->get(Middleware\Resources::class),
$this->container->get(Middleware\DebugPage::class),
$this->container->get(Middleware\RayRequestDump::class),
$this->container->get(Middleware\SentryTrap::class),
$this->container->get(Middleware\XHProfTrap::class),
],
[$this->container->get(Sender\Frontend\Http\RequestHandler::class)],
),
new Traffic\Dispatcher\Smtp(),
new Traffic\Dispatcher\Monolog(),
]);
$this->processors[] = $inspectorWithFrontend;
}

$this->configureFileObserver();

foreach ($map as $config) {
$this->prepareServerFiber($config, $inspector, $this->logger);
!$feSeparated && $config->type === 'tcp' && $config->port === $feConfig->port
? $this->prepareServerFiber($config, $inspectorWithFrontend, $this->logger)
: $this->prepareServerFiber($config, $inspector, $this->logger);
}
}

Expand Down Expand Up @@ -175,18 +205,22 @@ public function prepareServerFiber(SocketServer $config, Inspector $inspector, L
});
}

public function configureFrontend(): void
public function configureFrontend(bool $separated): void
{
$this->processors[] = $this->senders[] = $wsSender = Sender\FrontendSender::create($this->logger);
$this->container->set($wsSender->getEventStorage(), Sender\Frontend\EventStorage::class);
$this->container->set($wsSender);
$this->container->set($wsSender->getEventStorage());
$this->container->set($wsSender->getConnectionPool());

if (!$separated) {
return;
}

// Separated port
$inspector = $this->container->make(Inspector::class, [
new Traffic\Dispatcher\Http(
[
new Sender\Frontend\Http\Pipeline($this->logger, $wsSender),
],
[new Sender\Frontend\Http\RequestHandler($wsSender->getConnectionPool())],
[$this->container->get(Sender\Frontend\Http\Pipeline::class)],
[$this->container->get(Sender\Frontend\Http\RequestHandler::class)],
silentMode: true,
),
]);
Expand Down
18 changes: 7 additions & 11 deletions src/Command/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ final class Run extends Command implements SignalableCommandInterface
{
private ?Application $app = null;

private Logger $logger;

Check failure on line 35 in src/Command/Run.php

View workflow job for this annotation

GitHub Actions / psalm (ubuntu-latest, 8.2, locked)

PropertyNotSetInConstructor

src/Command/Run.php:35:20: PropertyNotSetInConstructor: Property Buggregator\Trap\Command\Run::$logger is not defined in constructor of Buggregator\Trap\Command\Run or in any private or final methods called in the constructor (see https://psalm.dev/074)

private bool $cancelled = false;

public function configure(): void
Expand Down Expand Up @@ -129,6 +131,7 @@ protected function execute(
InputInterface $input,
OutputInterface $output,
): int {
$this->logger = new Logger($output);
try {
// Print intro
$output->writeln(\sprintf('<fg=yellow;options=bold>%s</> <info>v%s</>', Info::NAME, Info::version()));
Expand All @@ -147,7 +150,7 @@ protected function execute(
)->finish();
$container->set($registry);
$container->set($input, InputInterface::class);
$container->set(new Logger($output));
$container->set($this->logger);
$this->app = $container->get(Application::class, [
'map' => $this->getServers($container),
'senders' => $registry->getSenders($senders),
Expand All @@ -157,16 +160,9 @@ protected function execute(

$this->app->run();
} catch (\Throwable $e) {
if ($output->isVerbose()) {
// Write colorful exception (title, message, stacktrace)
$output->writeln(\sprintf("<fg=red;options=bold>%s</>", $e::class));
}

$output->writeln(\sprintf("<fg=red>%s</>", $e->getMessage()));

if ($output->isDebug()) {
$output->writeln(\sprintf("<fg=gray>%s</>", $e->getTraceAsString()));
}
do {
$this->logger->exception($e);
} while ($e = $e->getPrevious());
}

return Command::SUCCESS;
Expand Down
5 changes: 5 additions & 0 deletions src/Handler/Http/Handler/Fallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Buggregator\Trap\Handler\Http\RequestHandler;
use Buggregator\Trap\Handler\Pipeline;
use Buggregator\Trap\Proto\Frame;
use Buggregator\Trap\Sender\Frontend\Http\Pipeline as FrontendPipeline;
use Buggregator\Trap\Traffic\StreamClient;
use Nyholm\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -81,6 +82,10 @@ public function handle(StreamClient $streamClient, ServerRequestInterface $reque
$streamClient->disconnect();
}

if (isset($response) && $response->hasHeader(FrontendPipeline::FRONTEND_HEADER)) {

Check failure on line 85 in src/Handler/Http/Handler/Fallback.php

View workflow job for this annotation

GitHub Actions / psalm (ubuntu-latest, 8.2, locked)

MixedMethodCall

src/Handler/Http/Handler/Fallback.php:85:44: MixedMethodCall: Cannot determine the type of $response when calling method hasHeader (see https://psalm.dev/015)
return;
}

if (!$gotFrame) {
yield new Frame\Http(
$request,
Expand Down
5 changes: 4 additions & 1 deletion src/Sender/Frontend/Http/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Buggregator\Trap\Handler\Http\Middleware;
use Buggregator\Trap\Handler\Pipeline as MiddlewaresPipeline;
use Buggregator\Trap\Info;
use Buggregator\Trap\Logger;
use Nyholm\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -19,6 +20,8 @@
*/
final class Pipeline implements Middleware
{
public const FRONTEND_HEADER = 'X-Trap-Frontend';

/** @var MiddlewaresPipeline<Middleware, ResponseInterface> */
private MiddlewaresPipeline $pipeline;

Expand Down Expand Up @@ -48,6 +51,6 @@ public function handle(ServerRequestInterface $request, callable $next): Respons

return $response->getStatusCode() === 404
? $next($request)
: $response;
: $response->withHeader(self::FRONTEND_HEADER, Info::version());
}
}
2 changes: 1 addition & 1 deletion src/Service/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function make(string $class, array $arguments = []): object
try {
$result = $this->injector->make($class, \array_merge((array) $binding, $arguments));
} catch (\Throwable $e) {
throw new class("Unable to create object of class $class.", previous: $e, ) extends \RuntimeException implements NotFoundExceptionInterface {};
throw new class("Unable to create object of class $class.", previous: $e) extends \RuntimeException implements NotFoundExceptionInterface {};
}
}

Expand Down

0 comments on commit 3b7e4df

Please sign in to comment.