Replace request-id middleware dependency with userland code

This commit is contained in:
Alejandro Celaya 2024-02-04 14:16:42 +01:00
parent 6017db260a
commit 0f894dcdfe
5 changed files with 44 additions and 58 deletions

View File

@ -36,16 +36,15 @@
"league/uri": "^6.8", "league/uri": "^6.8",
"matomo/matomo-php-tracker": "^3.2", "matomo/matomo-php-tracker": "^3.2",
"mezzio/mezzio": "^3.17", "mezzio/mezzio": "^3.17",
"mezzio/mezzio-fastroute": "^3.10", "mezzio/mezzio-fastroute": "^3.11",
"mezzio/mezzio-problem-details": "^1.13", "mezzio/mezzio-problem-details": "^1.13",
"mezzio/mezzio-swoole": "^4.7", "mezzio/mezzio-swoole": "^4.8",
"mlocati/ip-lib": "^1.18", "mlocati/ip-lib": "^1.18",
"mobiledetect/mobiledetectlib": "^4.8", "mobiledetect/mobiledetectlib": "^4.8",
"pagerfanta/core": "^3.8", "pagerfanta/core": "^3.8",
"php-middleware/request-id": "^4.1",
"pugx/shortid-php": "^1.1", "pugx/shortid-php": "^1.1",
"ramsey/uuid": "^4.7", "ramsey/uuid": "^4.7",
"shlinkio/shlink-common": "^5.7.1", "shlinkio/shlink-common": "dev-main#a309824 as 6.0",
"shlinkio/shlink-config": "^2.5", "shlinkio/shlink-config": "^2.5",
"shlinkio/shlink-event-dispatcher": "^3.1", "shlinkio/shlink-event-dispatcher": "^3.1",
"shlinkio/shlink-importer": "^5.2.1", "shlinkio/shlink-importer": "^5.2.1",
@ -56,11 +55,11 @@
"spiral/roadrunner-cli": "^2.5", "spiral/roadrunner-cli": "^2.5",
"spiral/roadrunner-http": "^3.1", "spiral/roadrunner-http": "^3.1",
"spiral/roadrunner-jobs": "^4.0", "spiral/roadrunner-jobs": "^4.0",
"symfony/console": "^6.3", "symfony/console": "^6.4",
"symfony/filesystem": "^6.3", "symfony/filesystem": "^6.4",
"symfony/lock": "^6.3", "symfony/lock": "^6.4",
"symfony/process": "^6.3", "symfony/process": "^6.4",
"symfony/string": "^6.3" "symfony/string": "^6.4"
}, },
"require-dev": { "require-dev": {
"devizzent/cebe-php-openapi": "^1.0.1", "devizzent/cebe-php-openapi": "^1.0.1",
@ -76,7 +75,7 @@
"roave/security-advisories": "dev-master", "roave/security-advisories": "dev-master",
"shlinkio/php-coding-standard": "~2.3.0", "shlinkio/php-coding-standard": "~2.3.0",
"shlinkio/shlink-test-utils": "^3.8.1", "shlinkio/shlink-test-utils": "^3.8.1",
"symfony/var-dumper": "^6.3", "symfony/var-dumper": "^6.4",
"veewee/composer-run-parallel": "^1.3" "veewee/composer-run-parallel": "^1.3"
}, },
"conflict": { "conflict": {

View File

@ -7,20 +7,21 @@ namespace Shlinkio\Shlink;
use Laminas\ServiceManager\Factory\InvokableFactory; use Laminas\ServiceManager\Factory\InvokableFactory;
use Monolog\Level; use Monolog\Level;
use Monolog\Logger; use Monolog\Logger;
use PhpMiddleware\RequestId;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger; use Psr\Log\NullLogger;
use Shlinkio\Shlink\Common\Logger\LoggerFactory; use Shlinkio\Shlink\Common\Logger\LoggerFactory;
use Shlinkio\Shlink\Common\Logger\LoggerType; use Shlinkio\Shlink\Common\Logger\LoggerType;
use Shlinkio\Shlink\Common\Middleware\AccessLogMiddleware; use Shlinkio\Shlink\Common\Middleware\AccessLogMiddleware;
use Shlinkio\Shlink\Common\Middleware\RequestIdMiddleware;
use function Shlinkio\Shlink\Config\runningInRoadRunner; use function Shlinkio\Shlink\Config\runningInRoadRunner;
return (static function (): array { return (static function (): array {
$common = [ $common = [
'level' => Level::Info->value, 'level' => Level::Info->value,
'processors' => [RequestId\MonologProcessor::class], 'processors' => [RequestIdMiddleware::class],
'line_format' => '[%datetime%] [%extra.request_id%] %channel%.%level_name% - %message%', 'line_format' =>
'[%datetime%] [%extra.' . RequestIdMiddleware::ATTRIBUTE . '%] %channel%.%level_name% - %message%',
]; ];
return [ return [

View File

@ -7,10 +7,10 @@ namespace Shlinkio\Shlink;
use Laminas\Stratigility\Middleware\ErrorHandler; use Laminas\Stratigility\Middleware\ErrorHandler;
use Mezzio\ProblemDetails; use Mezzio\ProblemDetails;
use Mezzio\Router; use Mezzio\Router;
use PhpMiddleware\RequestId\RequestIdMiddleware;
use RKA\Middleware\IpAddress; use RKA\Middleware\IpAddress;
use Shlinkio\Shlink\Common\Middleware\AccessLogMiddleware; use Shlinkio\Shlink\Common\Middleware\AccessLogMiddleware;
use Shlinkio\Shlink\Common\Middleware\ContentLengthMiddleware; use Shlinkio\Shlink\Common\Middleware\ContentLengthMiddleware;
use Shlinkio\Shlink\Common\Middleware\RequestIdMiddleware;
return [ return [

View File

@ -1,44 +0,0 @@
<?php
declare(strict_types=1);
use Laminas\ServiceManager\AbstractFactory\ConfigAbstractFactory;
use Laminas\ServiceManager\Factory\InvokableFactory;
use PhpMiddleware\RequestId;
use Shlinkio\Shlink\Common\Logger\Processor\BackwardsCompatibleMonologProcessorDelegator;
return [
'request_id' => [
'allow_override' => true,
'header_name' => 'X-Request-Id',
],
'dependencies' => [
'factories' => [
RequestId\Generator\RamseyUuid4StaticGenerator::class => InvokableFactory::class,
RequestId\RequestIdProviderFactory::class => ConfigAbstractFactory::class,
RequestId\RequestIdMiddleware::class => ConfigAbstractFactory::class,
RequestId\MonologProcessor::class => ConfigAbstractFactory::class,
],
'delegators' => [
RequestId\MonologProcessor::class => [
BackwardsCompatibleMonologProcessorDelegator::class,
],
],
],
ConfigAbstractFactory::class => [
RequestId\RequestIdProviderFactory::class => [
RequestId\Generator\RamseyUuid4StaticGenerator::class,
'config.request_id.allow_override',
'config.request_id.header_name',
],
RequestId\RequestIdMiddleware::class => [
RequestId\RequestIdProviderFactory::class,
'config.request_id.header_name',
],
RequestId\MonologProcessor::class => [RequestId\RequestIdMiddleware::class],
],
];

View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace ShlinkioApiTest\Shlink\Rest\Middleware;
use GuzzleHttp\RequestOptions;
use PHPUnit\Framework\Attributes\Test;
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
class RequestIdTest extends ApiTestCase
{
#[Test]
public function generatesRequestId(): void
{
$response = $this->callApi('GET', '/health');
self::assertTrue($response->hasHeader('X-Request-Id'));
}
#[Test]
public function keepsProvidedRequestId(): void
{
$response = $this->callApi('GET', '/health', [
RequestOptions::HEADERS => [
'X-Request-Id' => 'foobar',
],
]);
self::assertEquals('foobar', $response->hasHeader('X-Request-Id'));
}
}