How to 运行 Mojolicious appication under '/api' path( How to ignore some prefix in path )?

How to run Mojolicious appication under '/api' path( How to ignore some prefix in path )?

我有应用程序,它工作正常。但现在我们决定将其移动到 /api 路径下。所以我使用 detour

my $r = $self->routes;

# Application is always under /api/v1, /api/v2 etc. path
$r =  $r->any( '/api/:api', [ api => qr/v\d+/ ] )->detour( 'MyApp' );

$r->get( '/users/me' )->to( 'user#show_me' );

但此后没有任何效果。请求 site.domain/api/v1 导致应用程序陷入死循环。

指定路线下还有Mojolicious::Plugin::Mount but it only useful to mount another one application

This 指导也没有解决问题。

这一行应该修复:

# Application is always under /api/v1, /api/v2 etc. path
$r =  $r->any( '/api/:api', [ api => qr/v\d+/ ] )->partial( 1 )

更新
因为它是 documented:

Route has no specific end, remaining characters will be captured in path.

如果你 运行 myapp.pl routes -v 你会看到:

/api/:api        .D..  *        apiapi       ^\/api/((?^:v\d+))
   +/users/me    ....  GET      usersme      ^\/users\/me/?(?:\.([^/]+))?$

当请求到来时,将根据此正则表达式进行检查:

^\/api/((?^:v\d+))\/users\/me/?(?:\.([^/]+))?$

其中只有 /users/me 会被捕获到 path