Starfish
Starfish

Reputation: 1091

How do I match all routes in Mojolicious?

I'm trying to use a Mojolicious one-liner to display a message to users about a service outage. I'd like the message to display regardless of the route. Here's what I have which is slightly modified from what's in the documentation.

perl -Mojo -E 'a("/" => {text => "The service is down for maintenance."})->start' daemon

That works for / but not anything else. I added an asterisk to turn it into a wildcard route.

perl -Mojo -E 'a("/*" => {text => "The service is down for maintenance."})->start' daemon

That matches all routes except for /. Is there a way to match all routes in a single definition?

Upvotes: 2

Views: 520

Answers (3)

Eugen Konkov
Eugen Konkov

Reputation: 25282

Yes, you can. Try these examples:

perl -Mojo -E 'app->routes->get( "/" => { text => "start" }); app->routes->get( "/*any" => { text => "any" }); app->start' get /

perl -Mojo -E 'app->routes->get( "/" => { text => "start" }); app->routes->get( "/*any" => { text => "any" }); app->start' get /some_route

Here you define catch all route *any after the specific one /
Documentation

Upvotes: 1

Christopher Causer
Christopher Causer

Reputation: 1504

If you create a named placeholder, with a default value of anything, I believe it does what you want:

perl -Mojo -E 'a("/*x" => { text => "The service is down for maintenance.", x => ''})->start' daemon

May not be the prettiest code you'll ever see, but it's only a few more characters :-)

Upvotes: 0

vikin9
vikin9

Reputation: 505

How about:

perl -Mojo -E 'a("/*any" => {text => "The service is down for maintenance."})->start' daemon

I think it works for all urls but '/'.

Upvotes: 0

Related Questions