Skip to content

Commit

Permalink
fix: include DatabaseKeepAlive proposition
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielberthier committed Jun 17, 2023
1 parent 878e085 commit fb3c0c7
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 24 deletions.
3 changes: 2 additions & 1 deletion .docker/roadrunner/conf/rr.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
RR_VERSION=latest
PHP_IMAGE_VERSION=8.2-cli
PHP_IMAGE_VERSION=8.2-cli
RR=true
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ up: docker-compose.yml
docker compose up -d

up-rr: roadrunner-docker-compose.yml
PHP_IMAGE_VERSION=${PHP_IMAGE_VERSION} docker compose -f roadrunner-docker-compose.yml up -d
PHP_IMAGE_VERSION=${PHP_IMAGE_VERSION} RR=true docker compose -f roadrunner-docker-compose.yml up -d
echo ${PHP_IMAGE_VERSION}

build: docker-compose.yml
Expand Down
13 changes: 13 additions & 0 deletions app/Builder/AppBuilderManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
use Exception;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Respect\Validation\Factory;
use Slim\App;
use Slim\Factory\AppFactory;
use Slim\Interfaces\ErrorHandlerInterface;


class AppBuilderManager
{
private ContainerInterface $container;
Expand All @@ -27,6 +29,9 @@ class AppBuilderManager

private bool $enableShutdownHandler = true;

/** @var MiddlewareInterface[] */
private array $preMiddlewares = [];

public function __construct(ContainerInterface $containerInterface)
{
$this->container = $containerInterface;
Expand All @@ -36,12 +41,20 @@ public function __construct(ContainerInterface $containerInterface)
$this->errorFactory = new ErrorFactory($this->container);
}

public function appendMiddlewares(MiddlewareInterface $middlewareInterface){
$this->preMiddlewares[] = $middlewareInterface;
}

public function build(ServerRequestInterface $request): App
{
$app = $this->createApp();

$app->addRoutingMiddleware(); // Add the Slim built-in routing middleware

foreach ($this->preMiddlewares as $preMiddleware) {
$app->addMiddleware($preMiddleware);
}

\Core\Builder\MiddlewareCollector::collect(new SlimMiddlewareIncluder($app));

$router = new RouterCollector(new SlimRouteCollector($app));
Expand Down
4 changes: 2 additions & 2 deletions app/Builder/Factories/ContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Core\Builder\Factories;

use Core\Builder\ProvidersCollector;
use DI\Container;
use DI\ContainerBuilder;
use Psr\Container\ContainerInterface;

class ContainerFactory
{
Expand All @@ -16,7 +16,7 @@ public function __construct()
$this->containerBuilder = new ContainerBuilder();
}

public function get(): ContainerInterface
public function get(): Container
{
$containerBuilder = $this->setContainerValues();
$containerBuilder->useAnnotations($this->useAnnotations);
Expand Down
5 changes: 4 additions & 1 deletion app/Decorators/ReopeningEntityManagerDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Core\Data\Doctrine\EntityManagerBuilder;
use Doctrine\ORM\Decorator\EntityManagerDecorator;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Container\ContainerInterface;

class ReopeningEntityManagerDecorator extends EntityManagerDecorator
Expand All @@ -13,13 +14,15 @@ public function __construct(private ContainerInterface $container)
parent::__construct(EntityManagerBuilder::produce($container->get('settings')['doctrine']));
}

public function open(): void
public function open(): EntityManagerInterface
{
if (!$this->wrapped->isOpen()) {
$settings = $this->container->get('settings');
$doctrine = $settings['doctrine'];

$this->wrapped = EntityManagerBuilder::produce($doctrine);
}

return $this->wrapped;
}
}
4 changes: 2 additions & 2 deletions app/Definitions/connection.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
declare(strict_types=1);

declare(strict_types=1);

use function Core\functions\mode;

Expand Down Expand Up @@ -28,4 +28,4 @@
default => throw new Exception($exceptionMessage, 500)
};
}
];
];
2 changes: 0 additions & 2 deletions app/Definitions/middlewares.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

use App\Presentation\Middleware\JWTAuthMiddleware;
use App\Presentation\Middleware\SessionMiddleware;
// use Core\Http\Middlewares\DatabaseKeepAliveMiddleware;
use Slim\Middleware\BodyParsingMiddleware;

return [
SessionMiddleware::class,
JWTAuthMiddleware::class,
BodyParsingMiddleware::class,
// DatabaseKeepAliveMiddleware::class
//ErrorMiddleware::class
];
16 changes: 12 additions & 4 deletions app/Http/Middlewares/DatabaseKeepAliveMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,31 @@


use Core\Decorators\ReopeningEntityManagerDecorator;
use Psr\Container\ContainerInterface;
use DI\Container;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

use function Core\functions\dd;

class DatabaseKeepAliveMiddleware implements MiddlewareInterface
{
private ReopeningEntityManagerDecorator $reopeningEntityManagerDecorator;

public function __construct(
private ReopeningEntityManagerDecorator $reopeningEntityManagerDecorator,
private ContainerInterface $containerInterface
private Container $container
) {
$this->reopeningEntityManagerDecorator = new ReopeningEntityManagerDecorator($container);
$container->set(EntityManagerInterface::class, $this->reopeningEntityManagerDecorator->open());
}

function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$this->reopeningEntityManagerDecorator->open();
echo "cALLED";
$em = $this->reopeningEntityManagerDecorator->open();
$this->container->set(EntityManagerInterface::class, $em);

try {
return $handler->handle($request);
Expand Down
3 changes: 1 addition & 2 deletions app/Providers/Worker/WorkerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ class WorkerProvider extends AbstractProvider

public function provide(ContainerBuilder $container, array $definitions)
{
if (false) {
if (boolval(getenv("RR"))) {
$container->addDefinitions($definitions);

}
}
}
4 changes: 4 additions & 0 deletions app/logs/app.log
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@
[2023-06-06T15:18:40.994054+00:00] slim-app.DEBUG: Using token from request header [] {"uid":"aa46db1"}
[2023-06-06T18:15:20.409432+00:00] slim-app.DEBUG: Using token from request header [] {"uid":"79e1405"}
[2023-06-06T18:15:20.410457+00:00] slim-app.WARNING: Expired token ["eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2ODYwNjQwNDksImV4cCI6MTY4NjA2NDk0OSwianRpIjoia3luRUdiQ2hMQjdVak9hcHBwK1dRQT09Iiwic3ViIjoiYWRtaW4iLCJkYXRhIjp7ImVtYWlsIjoiYWRtaW5nQGFyY3RoaWUuY29tIiwidXVpZCI6IjBjMDRkN2Y1LTVlNDItNGZkZC05YmEyLWQxYjQ0Y2QyMmFjOSIsInJvbGUiOiJhZG1pbiIsInVzZXJuYW1lIjoiYWRtaW4ifSwiaXNzIjoiQVJUQ0hJRSJ9.lK7ZoPPvS5U6iIM2jIasGrV1cdMg8GaUL4A7JyfvvdU"] {"uid":"79e1405"}
[2023-06-12T18:17:22.106751+00:00] slim-app.ERROR: O nome de usuário ou o email escolhido já foi utilizado [] {"uid":"4594dd7"}
[2023-06-12T18:17:25.225778+00:00] slim-app.ERROR: O nome de usuário ou o email escolhido já foi utilizado [] {"uid":"ab746c4"}
[2023-06-12T18:17:28.205764+00:00] slim-app.ERROR: O nome de usuário ou o email escolhido já foi utilizado [] {"uid":"98e6a6f"}
[2023-06-12T18:17:42.794272+00:00] slim-app.ERROR: O nome de usuário ou o email escolhido já foi utilizado [] {"uid":"96be1f1"}
1 change: 0 additions & 1 deletion logs/README.md

This file was deleted.

4 changes: 3 additions & 1 deletion src/Presentation/Handlers/HttpErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ protected function respond(): Response
$statusCode = $exception->getCode();
$error->setDescription($exception->getMessage());

$this->logError($exception->getMessage());

$errorType = match (true) {
$exception instanceof HttpNotFoundException => ActionError::RESOURCE_NOT_FOUND,
$exception instanceof HttpMethodNotAllowedException => ActionError::NOT_ALLOWED,
Expand Down Expand Up @@ -67,4 +69,4 @@ protected function respond(): Response

return $response->withHeader('Content-Type', 'application/json');
}
}
}
17 changes: 10 additions & 7 deletions worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,31 @@
use Core\Builder\AppBuilderManager;
use Core\Builder\Factories\ContainerFactory;
use Core\Http\Factories\RequestFactory;

use Core\Http\Middlewares\DatabaseKeepAliveMiddleware;

require __DIR__ . '/configs/bootstrap.php';

$containerFactory = new ContainerFactory();

$containerFactory
// Set to true in production
->enableCompilation(false)
// Make use of annotations in classes
->withAnnotations()
;
->enableCompilation(false);

$container = $containerFactory->get();

$appBuilder = new AppBuilderManager($container);

$appBuilder = new AppBuilderManager($containerFactory->get());
$requestFactory = new RequestFactory();

$request = (new RequestFactory())->createRequest();
$request = $requestFactory->createRequest();

$app = $appBuilder->build($request);


/** @var Spiral\RoadRunner\Http\PSR7WorkerInterface $psr7Worker */
$psr7Worker = $app->getContainer()->get(Spiral\RoadRunner\Http\PSR7WorkerInterface::class);


while ($req = $psr7Worker->waitRequest()) {
try {
$res = $app->handle($req);
Expand Down

0 comments on commit fb3c0c7

Please sign in to comment.