Merge pull request #603 from acelaya-forks/feature/versioned-health

Feature/versioned health
This commit is contained in:
Alejandro Celaya 2020-01-07 18:15:17 +01:00 committed by GitHub
commit c0881f9292
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 65 additions and 24 deletions

View file

@ -34,7 +34,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this
#### Fixed #### Fixed
* *Nothing* * [#600](https://github.com/shlinkio/shlink/issues/600) Fixed health action so that it works with and without version in the path.
## 1.21.1 - 2020-01-02 ## 1.21.1 - 2020-01-02

View file

@ -12,6 +12,7 @@ return [
'routes_whitelist' => [ 'routes_whitelist' => [
Action\HealthAction::class, Action\HealthAction::class,
Action\ShortUrl\SingleStepCreateShortUrlAction::class, Action\ShortUrl\SingleStepCreateShortUrlAction::class,
ConfigProvider::UNVERSIONED_HEALTH_ENDPOINT_NAME,
], ],
'plugins' => [ 'plugins' => [

View file

@ -6,6 +6,8 @@ namespace Shlinkio\Shlink\Rest;
use Closure; use Closure;
use function Functional\first;
use function Functional\map;
use function Shlinkio\Shlink\Common\loadConfigFromGlob; use function Shlinkio\Shlink\Common\loadConfigFromGlob;
use function sprintf; use function sprintf;
@ -13,6 +15,7 @@ class ConfigProvider
{ {
private const ROUTES_PREFIX = '/rest/v{version:1|2}'; private const ROUTES_PREFIX = '/rest/v{version:1|2}';
private const UNVERSIONED_ROUTES_PREFIX = '/rest'; private const UNVERSIONED_ROUTES_PREFIX = '/rest';
public const UNVERSIONED_HEALTH_ENDPOINT_NAME = 'unversioned_health';
private Closure $loadConfig; private Closure $loadConfig;
@ -29,18 +32,33 @@ class ConfigProvider
private function applyRoutesPrefix(array $config): array private function applyRoutesPrefix(array $config): array
{ {
$routes =& $config['routes'] ?? []; $routes = $config['routes'] ?? [];
$healthRoute = $this->buildUnversionedHealthRouteFromExistingRoutes($routes);
// Prepend the routes prefix to every path $prefixRoute = static function (array $route) {
foreach ($routes as $key => $route) {
['path' => $path] = $route; ['path' => $path] = $route;
$routes[$key]['path'] = sprintf( $route['path'] = sprintf('%s%s', self::ROUTES_PREFIX, $path);
'%s%s',
$path === '/health' ? self::UNVERSIONED_ROUTES_PREFIX : self::ROUTES_PREFIX, return $route;
$path, };
); $prefixedRoutes = map($routes, $prefixRoute);
}
$config['routes'] = $healthRoute !== null ? [...$prefixedRoutes, $healthRoute] : $prefixedRoutes;
return $config; return $config;
} }
private function buildUnversionedHealthRouteFromExistingRoutes(array $routes): ?array
{
$healthRoute = first($routes, fn (array $route) => $route['path'] === '/health');
if ($healthRoute === null) {
return null;
}
$path = $healthRoute['path'];
$healthRoute['path'] = sprintf('%s%s', self::UNVERSIONED_ROUTES_PREFIX, $path);
$healthRoute['name'] = self::UNVERSIONED_HEALTH_ENDPOINT_NAME;
return $healthRoute;
}
} }

View file

@ -25,25 +25,47 @@ class ConfigProviderTest extends TestCase
$this->assertArrayHasKey('dependencies', $config); $this->assertArrayHasKey('dependencies', $config);
} }
/** @test */ /**
public function routesAreProperlyPrefixed(): void * @test
* @dataProvider provideRoutesConfig
*/
public function routesAreProperlyPrefixed(array $routes, array $expected): void
{ {
$configProvider = new ConfigProvider(fn () => [ $configProvider = new ConfigProvider(fn () => ['routes' => $routes]);
'routes' => [
$config = $configProvider();
$this->assertEquals($expected, $config['routes']);
}
public function provideRoutesConfig(): iterable
{
yield 'health action present' => [
[
['path' => '/foo'], ['path' => '/foo'],
['path' => '/bar'], ['path' => '/bar'],
['path' => '/baz/foo'], ['path' => '/baz/foo'],
['path' => '/health'], ['path' => '/health'],
], ],
]); [
['path' => '/rest/v{version:1|2}/foo'],
$config = $configProvider(); ['path' => '/rest/v{version:1|2}/bar'],
['path' => '/rest/v{version:1|2}/baz/foo'],
$this->assertEquals([ ['path' => '/rest/v{version:1|2}/health'],
['path' => '/rest/v{version:1|2}/foo'], ['path' => '/rest/health', 'name' => ConfigProvider::UNVERSIONED_HEALTH_ENDPOINT_NAME],
['path' => '/rest/v{version:1|2}/bar'], ],
['path' => '/rest/v{version:1|2}/baz/foo'], ];
['path' => '/rest/health'], yield 'health action not present' => [
], $config['routes']); [
['path' => '/foo'],
['path' => '/bar'],
['path' => '/baz/foo'],
],
[
['path' => '/rest/v{version:1|2}/foo'],
['path' => '/rest/v{version:1|2}/bar'],
['path' => '/rest/v{version:1|2}/baz/foo'],
],
];
} }
} }