I don't have an explanation for why this seems to be necessary for your
situation, but after reading Mojolicious::Guides::Routing#Under
<https://mojolicious.org/perldoc/Mojolicious/Guides/Routing#Under>, I found
that this should work for you:

  # REST interface
  my $rest_v1 = $r->under('/rest/v1'* => sub{1}*);
  $rest_v1->get('/')->to('example#welcome');

On Thu, Jun 13, 2019 at 1:09 AM Justin Hawkins <jus...@hawkins.id.au> wrote:

> Hello all,
>
> I think there is a good chance this is a case of “holding it wrong”, so I
> will first explain what I’m trying to achieve.
>
> Standard web app, multiple ways of establishing rights to privileged
> routes.
>
> The first is standard username/password, establishing a session.
>
> The second is via an API key (supplied with a header).
>
> I use two separate controller methods, one to check the session, one to
> check the header. Each one has the opportunity to set a stash variable
> indicating that the user has a valid session (via either cookie session or
> API key).
>
> So I use two “under” routes to call each method. So far so good, things
> work ok.
>
> The problem I have is that I later have another under route, to establish
> a new URL path for my REST interface.
>
> Any requests that go through routes derived from that third “under" have a
> strange behaviour - they call the second ‘under’ controller twice!
>
> This is probably easiest to see with an example:
>
> package TestUnder;
> use Mojo::Base 'Mojolicious';
>
> # This method will run once at server start
> sub startup {
>   my $self = shift;
>
>   # Load configuration from hash returned by config file
>   my $config = $self->plugin('Config');
>
>   # Configure the application
>   $self->secrets($config->{secrets});
>
>   # Router
>   my $r = $self->routes;
>
>   # All requests should go through these two
>   $r = $r->under()->to('example#under_one');
>   $r = $r->under()->to('example#under_two');
>   $r->get('/')->to('example#welcome');
>
>   # REST interface
>   my $rest_v1 = $r->under('/rest/v1');
>   $rest_v1->get('/')->to('example#welcome');
>
> }
>
> package TestUnder::Controller::Example;
>
> use Mojo::Base 'Mojolicious::Controller';
>
> sub under_one {
>   my $self = shift;
>   $self->app->log->info("under_one called");
>   1;
> }
>
> sub under_two {
>   my $self = shift;
>   $self->app->log->info("under_two called");
>   1;
> }
>
> sub welcome {
>   my $self = shift;
>   $self->render(text => 'hi');
> }
>
> 1;
>
> Example below - you can easily see that for the “REST” route, ‘under_two’
> is called twice:
>
>
>
>
> $ script/test_under get / >/dev/null
> [2019-06-13 15:28:30.55547] [44306] [debug] GET "/" (9ca39c7c)
> [2019-06-13 15:28:30.55627] [44306] [debug] Routing to controller
> "TestUnder::Controller::Example" and action "under_one"
> [2019-06-13 15:28:30.55637] [44306] [info] under_one called
> [2019-06-13 15:28:30.55651] [44306] [debug] Routing to controller
> "TestUnder::Controller::Example" and action "under_two"
> [2019-06-13 15:28:30.55661] [44306] [info] under_two called
> [2019-06-13 15:28:30.55675] [44306] [debug] Routing to controller
> "TestUnder::Controller::Example" and action "welcome"
> [2019-06-13 15:28:30.55704] [44306] [debug] 200 OK (0.001571s, 636.537/s)
>
> $ script/test_under get /rest/v1 >/dev/null
> [2019-06-13 15:28:34.95883] [44315] [debug] GET "/rest/v1" (6aad1c75)
> [2019-06-13 15:28:34.95975] [44315] [debug] Routing to controller
> "TestUnder::Controller::Example" and action "under_one"
> [2019-06-13 15:28:34.95984] [44315] [info] under_one called
> [2019-06-13 15:28:34.95997] [44315] [debug] Routing to controller
> "TestUnder::Controller::Example" and action "under_two"
> [2019-06-13 15:28:34.96005] [44315] [info] under_two called
> [2019-06-13 15:28:34.96020] [44315] [debug] Routing to controller
> "TestUnder::Controller::Example" and action "under_two"
> [2019-06-13 15:28:34.96031] [44315] [info] under_two called
> [2019-06-13 15:28:34.96044] [44315] [debug] Routing to controller
> "TestUnder::Controller::Example" and action "welcome"
> [2019-06-13 15:28:34.96083] [44315] [debug] 200 OK (0.001985s, 503.778/s)
>
> $ mojo version
> CORE
>   Perl        (v5.26.1, darwin)
>   Mojolicious (8.17, Supervillain)
>
> OPTIONAL
>   Cpanel::JSON::XS 4.04+  (n/a)
>   EV 4.0+                 (n/a)
>   IO::Socket::Socks 0.64+ (n/a)
>   IO::Socket::SSL 2.009+  (2.051)
>   Net::DNS::Native 0.15+  (n/a)
>   Role::Tiny 2.000001+    (2.000005)
>
> This version is up to date, have fun!
>
> Regards,
>
> Justin
>
> --
> You received this message because you are subscribed to the Google Groups
> "Mojolicious" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mojolicious+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/mojolicious/8EB007C9-0631-448B-B654-56C6D1EE0A5A%40hawkins.id.au
> <https://groups.google.com/d/msgid/mojolicious/8EB007C9-0631-448B-B654-56C6D1EE0A5A%40hawkins.id.au?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to mojolicious+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/mojolicious/CACyQ%2BFQ-nMkLVeXx1dZqcWh%3DTsK4PhuJWCsTAdMA%3DzMRjm3TiA%40mail.gmail.com.

Reply via email to