mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Added feature flag to enable/disable multi-segment support
This commit is contained in:
parent
7acf27dd38
commit
619999d4f8
@ -7,16 +7,20 @@ namespace Shlinkio\Shlink;
|
|||||||
use Fig\Http\Message\RequestMethodInterface;
|
use Fig\Http\Message\RequestMethodInterface;
|
||||||
use RKA\Middleware\IpAddress;
|
use RKA\Middleware\IpAddress;
|
||||||
use Shlinkio\Shlink\Core\Action as CoreAction;
|
use Shlinkio\Shlink\Core\Action as CoreAction;
|
||||||
|
use Shlinkio\Shlink\Core\Config\EnvVars;
|
||||||
use Shlinkio\Shlink\Rest\Action;
|
use Shlinkio\Shlink\Rest\Action;
|
||||||
use Shlinkio\Shlink\Rest\ConfigProvider;
|
use Shlinkio\Shlink\Rest\ConfigProvider;
|
||||||
use Shlinkio\Shlink\Rest\Middleware;
|
use Shlinkio\Shlink\Rest\Middleware;
|
||||||
use Shlinkio\Shlink\Rest\Middleware\Mercure\NotConfiguredMercureErrorHandler;
|
use Shlinkio\Shlink\Rest\Middleware\Mercure\NotConfiguredMercureErrorHandler;
|
||||||
|
|
||||||
|
use function sprintf;
|
||||||
|
|
||||||
// The order of the routes defined here matters. Changing it might cause path conflicts
|
// The order of the routes defined here matters. Changing it might cause path conflicts
|
||||||
return (static function (): array {
|
return (static function (): array {
|
||||||
$contentNegotiationMiddleware = Middleware\ShortUrl\CreateShortUrlContentNegotiationMiddleware::class;
|
$contentNegotiationMiddleware = Middleware\ShortUrl\CreateShortUrlContentNegotiationMiddleware::class;
|
||||||
$dropDomainMiddleware = Middleware\ShortUrl\DropDefaultDomainFromRequestMiddleware::class;
|
$dropDomainMiddleware = Middleware\ShortUrl\DropDefaultDomainFromRequestMiddleware::class;
|
||||||
$overrideDomainMiddleware = Middleware\ShortUrl\OverrideDomainMiddleware::class;
|
$overrideDomainMiddleware = Middleware\ShortUrl\OverrideDomainMiddleware::class;
|
||||||
|
$multiSegment = (bool) EnvVars::MULTI_SEGMENT_SLUGS_ENABLED->loadFromEnv(false);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
|
||||||
@ -60,7 +64,7 @@ return (static function (): array {
|
|||||||
Action\Domain\DomainRedirectsAction::getRouteDef(),
|
Action\Domain\DomainRedirectsAction::getRouteDef(),
|
||||||
|
|
||||||
Action\MercureInfoAction::getRouteDef([NotConfiguredMercureErrorHandler::class]),
|
Action\MercureInfoAction::getRouteDef([NotConfiguredMercureErrorHandler::class]),
|
||||||
]),
|
], $multiSegment),
|
||||||
|
|
||||||
// Non-rest
|
// Non-rest
|
||||||
[
|
[
|
||||||
@ -73,7 +77,7 @@ return (static function (): array {
|
|||||||
],
|
],
|
||||||
[
|
[
|
||||||
'name' => CoreAction\PixelAction::class,
|
'name' => CoreAction\PixelAction::class,
|
||||||
'path' => '/{shortCode:.+}/track',
|
'path' => sprintf('/{shortCode%s}/track', $multiSegment ? ':.+' : ''),
|
||||||
'middleware' => [
|
'middleware' => [
|
||||||
IpAddress::class,
|
IpAddress::class,
|
||||||
CoreAction\PixelAction::class,
|
CoreAction\PixelAction::class,
|
||||||
@ -82,7 +86,7 @@ return (static function (): array {
|
|||||||
],
|
],
|
||||||
[
|
[
|
||||||
'name' => CoreAction\QrCodeAction::class,
|
'name' => CoreAction\QrCodeAction::class,
|
||||||
'path' => '/{shortCode:.+}/qr-code',
|
'path' => sprintf('/{shortCode%s}/qr-code', $multiSegment ? ':.+' : ''),
|
||||||
'middleware' => [
|
'middleware' => [
|
||||||
CoreAction\QrCodeAction::class,
|
CoreAction\QrCodeAction::class,
|
||||||
],
|
],
|
||||||
@ -90,7 +94,7 @@ return (static function (): array {
|
|||||||
],
|
],
|
||||||
[
|
[
|
||||||
'name' => CoreAction\RedirectAction::class,
|
'name' => CoreAction\RedirectAction::class,
|
||||||
'path' => '/{shortCode:.+}',
|
'path' => sprintf('/{shortCode%s}', $multiSegment ? ':.+' : ''),
|
||||||
'middleware' => [
|
'middleware' => [
|
||||||
IpAddress::class,
|
IpAddress::class,
|
||||||
CoreAction\RedirectAction::class,
|
CoreAction\RedirectAction::class,
|
||||||
|
@ -59,6 +59,7 @@ enum EnvVars: string
|
|||||||
case AUTO_RESOLVE_TITLES = 'AUTO_RESOLVE_TITLES';
|
case AUTO_RESOLVE_TITLES = 'AUTO_RESOLVE_TITLES';
|
||||||
case REDIRECT_APPEND_EXTRA_PATH = 'REDIRECT_APPEND_EXTRA_PATH';
|
case REDIRECT_APPEND_EXTRA_PATH = 'REDIRECT_APPEND_EXTRA_PATH';
|
||||||
case TIMEZONE = 'TIMEZONE';
|
case TIMEZONE = 'TIMEZONE';
|
||||||
|
case MULTI_SEGMENT_SLUGS_ENABLED = 'MULTI_SEGMENT_SLUGS_ENABLED';
|
||||||
/** @deprecated */
|
/** @deprecated */
|
||||||
case VISITS_WEBHOOKS = 'VISITS_WEBHOOKS';
|
case VISITS_WEBHOOKS = 'VISITS_WEBHOOKS';
|
||||||
/** @deprecated */
|
/** @deprecated */
|
||||||
|
@ -14,7 +14,7 @@ use Shlinkio\Shlink\Rest\Middleware\AuthenticationMiddleware;
|
|||||||
|
|
||||||
class DeleteShortUrlAction extends AbstractRestAction
|
class DeleteShortUrlAction extends AbstractRestAction
|
||||||
{
|
{
|
||||||
protected const ROUTE_PATH = '/short-urls/{shortCode:.+}';
|
protected const ROUTE_PATH = '/short-urls/{shortCode}';
|
||||||
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_DELETE];
|
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_DELETE];
|
||||||
|
|
||||||
public function __construct(private DeleteShortUrlServiceInterface $deleteShortUrlService)
|
public function __construct(private DeleteShortUrlServiceInterface $deleteShortUrlService)
|
||||||
|
@ -16,7 +16,7 @@ use Shlinkio\Shlink\Rest\Middleware\AuthenticationMiddleware;
|
|||||||
|
|
||||||
class EditShortUrlAction extends AbstractRestAction
|
class EditShortUrlAction extends AbstractRestAction
|
||||||
{
|
{
|
||||||
protected const ROUTE_PATH = '/short-urls/{shortCode:.+}';
|
protected const ROUTE_PATH = '/short-urls/{shortCode}';
|
||||||
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_PATCH];
|
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_PATCH];
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
|
@ -15,7 +15,7 @@ use Shlinkio\Shlink\Rest\Middleware\AuthenticationMiddleware;
|
|||||||
|
|
||||||
class ResolveShortUrlAction extends AbstractRestAction
|
class ResolveShortUrlAction extends AbstractRestAction
|
||||||
{
|
{
|
||||||
protected const ROUTE_PATH = '/short-urls/{shortCode:.+}';
|
protected const ROUTE_PATH = '/short-urls/{shortCode}';
|
||||||
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET];
|
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET];
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
|
@ -18,7 +18,7 @@ class ShortUrlVisitsAction extends AbstractRestAction
|
|||||||
{
|
{
|
||||||
use PagerfantaUtilsTrait;
|
use PagerfantaUtilsTrait;
|
||||||
|
|
||||||
protected const ROUTE_PATH = '/short-urls/{shortCode:.+}/visits';
|
protected const ROUTE_PATH = '/short-urls/{shortCode}/visits';
|
||||||
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET];
|
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET];
|
||||||
|
|
||||||
public function __construct(private VisitsStatsHelperInterface $visitsHelper)
|
public function __construct(private VisitsStatsHelperInterface $visitsHelper)
|
||||||
|
@ -8,6 +8,7 @@ use function Functional\first;
|
|||||||
use function Functional\map;
|
use function Functional\map;
|
||||||
use function Shlinkio\Shlink\Config\loadConfigFromGlob;
|
use function Shlinkio\Shlink\Config\loadConfigFromGlob;
|
||||||
use function sprintf;
|
use function sprintf;
|
||||||
|
use function str_replace;
|
||||||
|
|
||||||
class ConfigProvider
|
class ConfigProvider
|
||||||
{
|
{
|
||||||
@ -20,11 +21,14 @@ class ConfigProvider
|
|||||||
return loadConfigFromGlob(__DIR__ . '/../config/{,*.}config.php');
|
return loadConfigFromGlob(__DIR__ . '/../config/{,*.}config.php');
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function applyRoutesPrefix(array $routes): array
|
public static function applyRoutesPrefix(array $routes, bool $multiSegmentEnabled): array
|
||||||
{
|
{
|
||||||
$healthRoute = self::buildUnversionedHealthRouteFromExistingRoutes($routes);
|
$healthRoute = self::buildUnversionedHealthRouteFromExistingRoutes($routes);
|
||||||
$prefixedRoutes = map($routes, static function (array $route) {
|
$prefixedRoutes = map($routes, static function (array $route) use ($multiSegmentEnabled) {
|
||||||
['path' => $path] = $route;
|
['path' => $path] = $route;
|
||||||
|
if ($multiSegmentEnabled) {
|
||||||
|
$path = str_replace('{shortCode}', '{shortCode:.+}', $path);
|
||||||
|
}
|
||||||
$route['path'] = sprintf('%s%s', self::ROUTES_PREFIX, $path);
|
$route['path'] = sprintf('%s%s', self::ROUTES_PREFIX, $path);
|
||||||
|
|
||||||
return $route;
|
return $route;
|
||||||
@ -40,7 +44,7 @@ class ConfigProvider
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
$path = $healthRoute['path'];
|
['path' => $path] = $healthRoute;
|
||||||
$healthRoute['path'] = sprintf('%s%s', self::UNVERSIONED_ROUTES_PREFIX, $path);
|
$healthRoute['path'] = sprintf('%s%s', self::UNVERSIONED_ROUTES_PREFIX, $path);
|
||||||
$healthRoute['name'] = self::UNVERSIONED_HEALTH_ENDPOINT_NAME;
|
$healthRoute['name'] = self::UNVERSIONED_HEALTH_ENDPOINT_NAME;
|
||||||
|
|
||||||
|
@ -71,7 +71,7 @@ class CorsTest extends ApiTestCase
|
|||||||
|
|
||||||
public function providePreflightEndpoints(): iterable
|
public function providePreflightEndpoints(): iterable
|
||||||
{
|
{
|
||||||
// yield 'invalid route' => ['/foo/bar', 'GET,POST,PUT,PATCH,DELETE']; // TODO This won't work with multi-segment
|
yield 'invalid route' => ['/foo/bar', 'GET,POST,PUT,PATCH,DELETE']; // TODO This won't work with multi-segment
|
||||||
yield 'short URLs route' => ['/short-urls', 'GET,POST'];
|
yield 'short URLs route' => ['/short-urls', 'GET,POST'];
|
||||||
yield 'tags route' => ['/tags', 'GET,DELETE,PUT'];
|
yield 'tags route' => ['/tags', 'GET,DELETE,PUT'];
|
||||||
yield 'health route' => ['/health', 'GET'];
|
yield 'health route' => ['/health', 'GET'];
|
||||||
|
@ -33,9 +33,9 @@ class ConfigProviderTest extends TestCase
|
|||||||
* @test
|
* @test
|
||||||
* @dataProvider provideRoutesConfig
|
* @dataProvider provideRoutesConfig
|
||||||
*/
|
*/
|
||||||
public function routesAreProperlyPrefixed(array $routes, array $expected): void
|
public function routesAreProperlyPrefixed(array $routes, bool $multiSegmentEnabled, array $expected): void
|
||||||
{
|
{
|
||||||
self::assertEquals($expected, ConfigProvider::applyRoutesPrefix($routes));
|
self::assertEquals($expected, ConfigProvider::applyRoutesPrefix($routes, $multiSegmentEnabled));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function provideRoutesConfig(): iterable
|
public function provideRoutesConfig(): iterable
|
||||||
@ -47,6 +47,7 @@ class ConfigProviderTest extends TestCase
|
|||||||
['path' => '/baz/foo'],
|
['path' => '/baz/foo'],
|
||||||
['path' => '/health'],
|
['path' => '/health'],
|
||||||
],
|
],
|
||||||
|
false,
|
||||||
[
|
[
|
||||||
['path' => '/rest/v{version:1|2}/foo'],
|
['path' => '/rest/v{version:1|2}/foo'],
|
||||||
['path' => '/rest/v{version:1|2}/bar'],
|
['path' => '/rest/v{version:1|2}/bar'],
|
||||||
@ -61,11 +62,25 @@ class ConfigProviderTest extends TestCase
|
|||||||
['path' => '/bar'],
|
['path' => '/bar'],
|
||||||
['path' => '/baz/foo'],
|
['path' => '/baz/foo'],
|
||||||
],
|
],
|
||||||
|
false,
|
||||||
[
|
[
|
||||||
['path' => '/rest/v{version:1|2}/foo'],
|
['path' => '/rest/v{version:1|2}/foo'],
|
||||||
['path' => '/rest/v{version:1|2}/bar'],
|
['path' => '/rest/v{version:1|2}/bar'],
|
||||||
['path' => '/rest/v{version:1|2}/baz/foo'],
|
['path' => '/rest/v{version:1|2}/baz/foo'],
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
yield 'multi-segment enabled' => [
|
||||||
|
[
|
||||||
|
['path' => '/foo'],
|
||||||
|
['path' => '/bar/{shortCode}'],
|
||||||
|
['path' => '/baz/{shortCode}/foo'],
|
||||||
|
],
|
||||||
|
true,
|
||||||
|
[
|
||||||
|
['path' => '/rest/v{version:1|2}/foo'],
|
||||||
|
['path' => '/rest/v{version:1|2}/bar/{shortCode:.+}'],
|
||||||
|
['path' => '/rest/v{version:1|2}/baz/{shortCode:.+}/foo'],
|
||||||
|
],
|
||||||
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user