From 0f86123ccbd463f8a6ece72c299d509dceb64b41 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 29 Dec 2018 11:24:55 +0100 Subject: [PATCH] Finished health action implementation --- module/Rest/src/Action/AbstractRestAction.php | 2 -- module/Rest/src/Action/HealthAction.php | 5 +++-- module/Rest/src/ConfigProvider.php | 13 +++---------- .../Rest/src/Middleware/PathVersionMiddleware.php | 3 +++ 4 files changed, 9 insertions(+), 14 deletions(-) diff --git a/module/Rest/src/Action/AbstractRestAction.php b/module/Rest/src/Action/AbstractRestAction.php index a9c449c4..21e15d66 100644 --- a/module/Rest/src/Action/AbstractRestAction.php +++ b/module/Rest/src/Action/AbstractRestAction.php @@ -14,7 +14,6 @@ abstract class AbstractRestAction implements RequestHandlerInterface, RequestMet { protected const ROUTE_PATH = ''; protected const ROUTE_ALLOWED_METHODS = []; - protected const ROUTE_CAN_BE_VERSIONED = true; /** @var LoggerInterface */ protected $logger; @@ -31,7 +30,6 @@ abstract class AbstractRestAction implements RequestHandlerInterface, RequestMet 'middleware' => array_merge($prevMiddleware, [static::class], $postMiddleware), 'path' => static::ROUTE_PATH, 'allowed_methods' => static::ROUTE_ALLOWED_METHODS, - 'can_be_versioned' => static::ROUTE_CAN_BE_VERSIONED, ]; } } diff --git a/module/Rest/src/Action/HealthAction.php b/module/Rest/src/Action/HealthAction.php index 35af83fa..d062d5b2 100644 --- a/module/Rest/src/Action/HealthAction.php +++ b/module/Rest/src/Action/HealthAction.php @@ -13,10 +13,11 @@ use Zend\Diactoros\Response\JsonResponse; class HealthAction extends AbstractRestAction { private const HEALTH_CONTENT_TYPE = 'application/health+json'; + private const PASS_STATUS = 'pass'; + private const FAIL_STATUS = 'fail'; protected const ROUTE_PATH = '/health'; protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET]; - protected const ROUTE_CAN_BE_VERSIONED = false; /** @var AppOptions */ private $options; @@ -41,7 +42,7 @@ class HealthAction extends AbstractRestAction $statusCode = $connected ? self::STATUS_OK : self::STATUS_SERVICE_UNAVAILABLE; return new JsonResponse([ - 'status' => $connected ? 'pass' : 'fail', + 'status' => $connected ? self::PASS_STATUS : self::FAIL_STATUS, 'version' => $this->options->getVersion(), 'links' => [ 'about' => 'https://shlink.io', diff --git a/module/Rest/src/ConfigProvider.php b/module/Rest/src/ConfigProvider.php index a951161b..56cfd740 100644 --- a/module/Rest/src/ConfigProvider.php +++ b/module/Rest/src/ConfigProvider.php @@ -9,8 +9,7 @@ use function sprintf; class ConfigProvider { - private const ROUTES_PREFIX = '/rest'; - private const ROUTES_VERSION_PARAM = '/v{version:1}'; + private const ROUTES_PREFIX = '/rest/v{version:1}'; public function __invoke() { @@ -25,14 +24,8 @@ class ConfigProvider // Prepend the routes prefix to every path foreach ($routes as $key => $route) { - ['can_be_versioned' => $routeCanBeVersioned, 'path' => $path] = $route; - $routes[$key]['path'] = sprintf( - '%s%s%s', - self::ROUTES_PREFIX, - $routeCanBeVersioned ? self::ROUTES_VERSION_PARAM : '', - $path - ); - unset($routes[$key]['can_be_versioned']); + ['path' => $path] = $route; + $routes[$key]['path'] = sprintf('%s%s', self::ROUTES_PREFIX, $path); } return $config; diff --git a/module/Rest/src/Middleware/PathVersionMiddleware.php b/module/Rest/src/Middleware/PathVersionMiddleware.php index cc28be0b..d39c8a2f 100644 --- a/module/Rest/src/Middleware/PathVersionMiddleware.php +++ b/module/Rest/src/Middleware/PathVersionMiddleware.php @@ -11,6 +11,9 @@ use function strpos; class PathVersionMiddleware implements MiddlewareInterface { + // TODO The /health endpoint needs this middleware in order to work without the version. + // Take it into account if this middleware is ever removed. + /** * Process an incoming server request and return a response, optionally delegating * to the next middleware component to create the response.