mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Created HealthAction
This commit is contained in:
parent
29d49dfbf4
commit
3f65ef998c
@ -1,4 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
use Zend\Expressive\Container\WhoopsErrorResponseGeneratorFactory;
|
use Zend\Expressive\Container\WhoopsErrorResponseGeneratorFactory;
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
@ -10,6 +10,7 @@ return [
|
|||||||
'auth' => [
|
'auth' => [
|
||||||
'routes_whitelist' => [
|
'routes_whitelist' => [
|
||||||
Action\AuthenticateAction::class,
|
Action\AuthenticateAction::class,
|
||||||
|
Action\HealthAction::class,
|
||||||
Action\ShortUrl\SingleStepCreateShortUrlAction::class,
|
Action\ShortUrl\SingleStepCreateShortUrlAction::class,
|
||||||
],
|
],
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@ return [
|
|||||||
ApiKeyService::class => ConfigAbstractFactory::class,
|
ApiKeyService::class => ConfigAbstractFactory::class,
|
||||||
|
|
||||||
Action\AuthenticateAction::class => ConfigAbstractFactory::class,
|
Action\AuthenticateAction::class => ConfigAbstractFactory::class,
|
||||||
|
Action\HealthAction::class => Action\HealthActionFactory::class,
|
||||||
Action\ShortUrl\CreateShortUrlAction::class => ConfigAbstractFactory::class,
|
Action\ShortUrl\CreateShortUrlAction::class => ConfigAbstractFactory::class,
|
||||||
Action\ShortUrl\SingleStepCreateShortUrlAction::class => ConfigAbstractFactory::class,
|
Action\ShortUrl\SingleStepCreateShortUrlAction::class => ConfigAbstractFactory::class,
|
||||||
Action\ShortUrl\EditShortUrlAction::class => ConfigAbstractFactory::class,
|
Action\ShortUrl\EditShortUrlAction::class => ConfigAbstractFactory::class,
|
||||||
|
@ -3,12 +3,11 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Shlinkio\Shlink\Rest;
|
namespace Shlinkio\Shlink\Rest;
|
||||||
|
|
||||||
use Shlinkio\Shlink\Rest\Action;
|
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
|
||||||
'routes' => [
|
'routes' => [
|
||||||
Action\AuthenticateAction::getRouteDef(),
|
Action\AuthenticateAction::getRouteDef(),
|
||||||
|
Action\HealthAction::getRouteDef(),
|
||||||
|
|
||||||
// Short codes
|
// Short codes
|
||||||
Action\ShortUrl\CreateShortUrlAction::getRouteDef([
|
Action\ShortUrl\CreateShortUrlAction::getRouteDef([
|
||||||
|
@ -14,6 +14,7 @@ abstract class AbstractRestAction implements RequestHandlerInterface, RequestMet
|
|||||||
{
|
{
|
||||||
protected const ROUTE_PATH = '';
|
protected const ROUTE_PATH = '';
|
||||||
protected const ROUTE_ALLOWED_METHODS = [];
|
protected const ROUTE_ALLOWED_METHODS = [];
|
||||||
|
protected const ROUTE_CAN_BE_VERSIONED = true;
|
||||||
|
|
||||||
/** @var LoggerInterface */
|
/** @var LoggerInterface */
|
||||||
protected $logger;
|
protected $logger;
|
||||||
@ -30,6 +31,7 @@ abstract class AbstractRestAction implements RequestHandlerInterface, RequestMet
|
|||||||
'middleware' => array_merge($prevMiddleware, [static::class], $postMiddleware),
|
'middleware' => array_merge($prevMiddleware, [static::class], $postMiddleware),
|
||||||
'path' => static::ROUTE_PATH,
|
'path' => static::ROUTE_PATH,
|
||||||
'allowed_methods' => static::ROUTE_ALLOWED_METHODS,
|
'allowed_methods' => static::ROUTE_ALLOWED_METHODS,
|
||||||
|
'can_be_versioned' => static::ROUTE_CAN_BE_VERSIONED,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@ use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
|
|||||||
use Shlinkio\Shlink\Rest\Util\RestUtils;
|
use Shlinkio\Shlink\Rest\Util\RestUtils;
|
||||||
use Zend\Diactoros\Response\JsonResponse;
|
use Zend\Diactoros\Response\JsonResponse;
|
||||||
|
|
||||||
|
/** @deprecated */
|
||||||
class AuthenticateAction extends AbstractRestAction
|
class AuthenticateAction extends AbstractRestAction
|
||||||
{
|
{
|
||||||
protected const ROUTE_PATH = '/authenticate';
|
protected const ROUTE_PATH = '/authenticate';
|
||||||
|
52
module/Rest/src/Action/HealthAction.php
Normal file
52
module/Rest/src/Action/HealthAction.php
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Shlinkio\Shlink\Rest\Action;
|
||||||
|
|
||||||
|
use Doctrine\DBAL\Connection;
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
use Shlinkio\Shlink\Core\Options\AppOptions;
|
||||||
|
use Zend\Diactoros\Response\JsonResponse;
|
||||||
|
|
||||||
|
class HealthAction extends AbstractRestAction
|
||||||
|
{
|
||||||
|
private const HEALTH_CONTENT_TYPE = 'application/health+json';
|
||||||
|
|
||||||
|
protected const ROUTE_PATH = '/health';
|
||||||
|
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET];
|
||||||
|
protected const ROUTE_CAN_BE_VERSIONED = false;
|
||||||
|
|
||||||
|
/** @var AppOptions */
|
||||||
|
private $options;
|
||||||
|
/** @var Connection */
|
||||||
|
private $conn;
|
||||||
|
|
||||||
|
public function __construct(Connection $conn, AppOptions $options, LoggerInterface $logger = null)
|
||||||
|
{
|
||||||
|
parent::__construct($logger);
|
||||||
|
$this->conn = $conn;
|
||||||
|
$this->options = $options;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles a request and produces a response.
|
||||||
|
*
|
||||||
|
* May call other collaborating code to generate the response.
|
||||||
|
*/
|
||||||
|
public function handle(ServerRequestInterface $request): ResponseInterface
|
||||||
|
{
|
||||||
|
$connected = $this->conn->ping();
|
||||||
|
$statusCode = $connected ? self::STATUS_OK : self::STATUS_SERVICE_UNAVAILABLE;
|
||||||
|
|
||||||
|
return new JsonResponse([
|
||||||
|
'status' => $connected ? 'pass' : 'fail',
|
||||||
|
'version' => $this->options->getVersion(),
|
||||||
|
'links' => [
|
||||||
|
'about' => 'https://shlink.io',
|
||||||
|
'project' => 'https://github.com/shlinkio/shlink',
|
||||||
|
],
|
||||||
|
], $statusCode, ['Content-type' => self::HEALTH_CONTENT_TYPE]);
|
||||||
|
}
|
||||||
|
}
|
19
module/Rest/src/Action/HealthActionFactory.php
Normal file
19
module/Rest/src/Action/HealthActionFactory.php
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Shlinkio\Shlink\Rest\Action;
|
||||||
|
|
||||||
|
use Doctrine\ORM\EntityManager;
|
||||||
|
use Psr\Container\ContainerInterface;
|
||||||
|
use Shlinkio\Shlink\Core\Options\AppOptions;
|
||||||
|
|
||||||
|
class HealthActionFactory
|
||||||
|
{
|
||||||
|
public function __invoke(ContainerInterface $container)
|
||||||
|
{
|
||||||
|
$em = $container->get(EntityManager::class);
|
||||||
|
$options = $container->get(AppOptions::class);
|
||||||
|
$logger = $container->get('Logger_Shlink');
|
||||||
|
return new HealthAction($em->getConnection(), $options, $logger);
|
||||||
|
}
|
||||||
|
}
|
@ -5,10 +5,12 @@ namespace Shlinkio\Shlink\Rest;
|
|||||||
|
|
||||||
use Zend\Config\Factory;
|
use Zend\Config\Factory;
|
||||||
use Zend\Stdlib\Glob;
|
use Zend\Stdlib\Glob;
|
||||||
|
use function sprintf;
|
||||||
|
|
||||||
class ConfigProvider
|
class ConfigProvider
|
||||||
{
|
{
|
||||||
const ROUTES_PREFIX = '/rest/v{version:1}';
|
private const ROUTES_PREFIX = '/rest';
|
||||||
|
private const ROUTES_VERSION_PARAM = '/v{version:1}';
|
||||||
|
|
||||||
public function __invoke()
|
public function __invoke()
|
||||||
{
|
{
|
||||||
@ -23,7 +25,14 @@ class ConfigProvider
|
|||||||
|
|
||||||
// Prepend the routes prefix to every path
|
// Prepend the routes prefix to every path
|
||||||
foreach ($routes as $key => $route) {
|
foreach ($routes as $key => $route) {
|
||||||
$routes[$key]['path'] = self::ROUTES_PREFIX . $route['path'];
|
['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']);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $config;
|
return $config;
|
||||||
|
Loading…
Reference in New Issue
Block a user