2022-08-04 04:14:26 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Shlinkio\Shlink;
|
|
|
|
|
2022-08-04 04:27:33 -05:00
|
|
|
use Fig\Http\Message\RequestMethodInterface;
|
2022-08-04 04:14:26 -05:00
|
|
|
use RKA\Middleware\IpAddress;
|
|
|
|
use Shlinkio\Shlink\Core\Action as CoreAction;
|
2022-09-05 13:56:49 -05:00
|
|
|
use Shlinkio\Shlink\Core\Config\EnvVars;
|
2022-09-30 10:19:07 -05:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Middleware\TrimTrailingSlashMiddleware;
|
2022-08-04 04:14:26 -05:00
|
|
|
use Shlinkio\Shlink\Rest\Action;
|
|
|
|
use Shlinkio\Shlink\Rest\ConfigProvider;
|
|
|
|
use Shlinkio\Shlink\Rest\Middleware;
|
|
|
|
use Shlinkio\Shlink\Rest\Middleware\Mercure\NotConfiguredMercureErrorHandler;
|
|
|
|
|
2022-09-05 13:56:49 -05:00
|
|
|
use function sprintf;
|
|
|
|
|
2022-08-04 04:14:26 -05:00
|
|
|
return (static function (): array {
|
|
|
|
$dropDomainMiddleware = Middleware\ShortUrl\DropDefaultDomainFromRequestMiddleware::class;
|
|
|
|
$overrideDomainMiddleware = Middleware\ShortUrl\OverrideDomainMiddleware::class;
|
2022-09-30 10:19:07 -05:00
|
|
|
|
|
|
|
// TODO This should be based on config, not the env var
|
2022-09-05 13:56:49 -05:00
|
|
|
$shortUrlRouteSuffix = EnvVars::SHORT_URL_TRAILING_SLASH->loadFromEnv(false) ? '[/]' : '';
|
2022-08-04 04:14:26 -05:00
|
|
|
|
|
|
|
return [
|
|
|
|
|
2022-08-06 02:30:13 -05:00
|
|
|
// The order of the routes defined here matters. Changing it might cause path conflicts
|
2022-08-04 04:14:26 -05:00
|
|
|
'routes' => [
|
2022-08-04 04:27:33 -05:00
|
|
|
// Rest
|
2022-08-04 04:14:26 -05:00
|
|
|
...ConfigProvider::applyRoutesPrefix([
|
|
|
|
Action\HealthAction::getRouteDef(),
|
|
|
|
|
2024-02-27 14:00:53 -06:00
|
|
|
// Visits and rules routes must go first, as they have a more specific path, otherwise, when
|
|
|
|
// multi-segment slugs are enabled, routes with a less-specific path might match first
|
|
|
|
|
2023-11-08 02:06:12 -06:00
|
|
|
// Visits.
|
2022-08-04 04:14:26 -05:00
|
|
|
Action\Visit\ShortUrlVisitsAction::getRouteDef([$dropDomainMiddleware]),
|
2023-11-08 02:06:12 -06:00
|
|
|
Action\ShortUrl\DeleteShortUrlVisitsAction::getRouteDef([$dropDomainMiddleware]),
|
2022-08-04 04:14:26 -05:00
|
|
|
Action\Visit\TagVisitsAction::getRouteDef(),
|
|
|
|
Action\Visit\DomainVisitsAction::getRouteDef(),
|
|
|
|
Action\Visit\GlobalVisitsAction::getRouteDef(),
|
|
|
|
Action\Visit\OrphanVisitsAction::getRouteDef(),
|
2023-05-18 02:04:28 -05:00
|
|
|
Action\Visit\DeleteOrphanVisitsAction::getRouteDef(),
|
2022-08-04 04:14:26 -05:00
|
|
|
Action\Visit\NonOrphanVisitsAction::getRouteDef(),
|
|
|
|
|
2024-02-27 14:00:53 -06:00
|
|
|
//Redirect rules
|
|
|
|
Action\RedirectRule\ListRedirectRulesAction::getRouteDef([$dropDomainMiddleware]),
|
2024-02-28 13:24:16 -06:00
|
|
|
Action\RedirectRule\SetRedirectRulesAction::getRouteDef([$dropDomainMiddleware]),
|
2024-02-27 14:00:53 -06:00
|
|
|
|
2022-08-04 04:14:26 -05:00
|
|
|
// Short URLs
|
|
|
|
Action\ShortUrl\CreateShortUrlAction::getRouteDef([
|
|
|
|
$dropDomainMiddleware,
|
|
|
|
$overrideDomainMiddleware,
|
|
|
|
Middleware\ShortUrl\DefaultShortCodesLengthMiddleware::class,
|
|
|
|
]),
|
|
|
|
Action\ShortUrl\SingleStepCreateShortUrlAction::getRouteDef([
|
2024-02-27 14:00:53 -06:00
|
|
|
Middleware\ShortUrl\CreateShortUrlContentNegotiationMiddleware::class,
|
2022-08-04 04:14:26 -05:00
|
|
|
$overrideDomainMiddleware,
|
|
|
|
]),
|
|
|
|
Action\ShortUrl\EditShortUrlAction::getRouteDef([$dropDomainMiddleware]),
|
|
|
|
Action\ShortUrl\DeleteShortUrlAction::getRouteDef([$dropDomainMiddleware]),
|
|
|
|
Action\ShortUrl\ResolveShortUrlAction::getRouteDef([$dropDomainMiddleware]),
|
|
|
|
Action\ShortUrl\ListShortUrlsAction::getRouteDef(),
|
|
|
|
|
|
|
|
// Tags
|
|
|
|
Action\Tag\ListTagsAction::getRouteDef(),
|
|
|
|
Action\Tag\TagsStatsAction::getRouteDef(),
|
|
|
|
Action\Tag\DeleteTagsAction::getRouteDef(),
|
|
|
|
Action\Tag\UpdateTagAction::getRouteDef(),
|
|
|
|
|
|
|
|
// Domains
|
|
|
|
Action\Domain\ListDomainsAction::getRouteDef(),
|
|
|
|
Action\Domain\DomainRedirectsAction::getRouteDef(),
|
|
|
|
|
|
|
|
Action\MercureInfoAction::getRouteDef([NotConfiguredMercureErrorHandler::class]),
|
2022-08-06 02:30:13 -05:00
|
|
|
]),
|
2022-08-04 04:14:26 -05:00
|
|
|
|
|
|
|
// Non-rest
|
|
|
|
[
|
|
|
|
'name' => CoreAction\RobotsAction::class,
|
|
|
|
'path' => '/robots.txt',
|
|
|
|
'middleware' => [
|
|
|
|
CoreAction\RobotsAction::class,
|
|
|
|
],
|
2022-08-04 04:27:33 -05:00
|
|
|
'allowed_methods' => [RequestMethodInterface::METHOD_GET],
|
2022-08-04 04:14:26 -05:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'name' => CoreAction\PixelAction::class,
|
2022-08-06 02:30:13 -05:00
|
|
|
'path' => '/{shortCode}/track',
|
2022-08-04 04:14:26 -05:00
|
|
|
'middleware' => [
|
|
|
|
IpAddress::class,
|
|
|
|
CoreAction\PixelAction::class,
|
|
|
|
],
|
2022-08-04 04:27:33 -05:00
|
|
|
'allowed_methods' => [RequestMethodInterface::METHOD_GET],
|
2022-08-04 04:14:26 -05:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'name' => CoreAction\QrCodeAction::class,
|
2022-08-06 02:30:13 -05:00
|
|
|
'path' => '/{shortCode}/qr-code',
|
2022-08-04 04:14:26 -05:00
|
|
|
'middleware' => [
|
|
|
|
CoreAction\QrCodeAction::class,
|
|
|
|
],
|
2022-08-04 04:27:33 -05:00
|
|
|
'allowed_methods' => [RequestMethodInterface::METHOD_GET],
|
2022-08-04 04:14:26 -05:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'name' => CoreAction\RedirectAction::class,
|
2022-09-05 13:56:49 -05:00
|
|
|
'path' => sprintf('/{shortCode}%s', $shortUrlRouteSuffix),
|
2022-08-04 04:14:26 -05:00
|
|
|
'middleware' => [
|
|
|
|
IpAddress::class,
|
2022-09-30 10:19:07 -05:00
|
|
|
TrimTrailingSlashMiddleware::class,
|
2022-08-04 04:14:26 -05:00
|
|
|
CoreAction\RedirectAction::class,
|
|
|
|
],
|
2022-08-04 04:27:33 -05:00
|
|
|
'allowed_methods' => [RequestMethodInterface::METHOD_GET],
|
2022-08-04 04:14:26 -05:00
|
|
|
],
|
|
|
|
],
|
|
|
|
|
|
|
|
];
|
|
|
|
})();
|