vertx router to redirect on dynamic routes

767 views
Skip to first unread message

Gadi Eichhorn

unread,
May 29, 2019, 4:59:08 AM5/29/19
to vert.x
Hi all,

I have this web server with a router and all the routes are added dynamically with dependency injection (OSGi)

One of the routes is a static site on a path "app/dashboard/*"
Other routes are "login" and "api/*" with subroutes 

I would like to catch any routes that don't match and redirect to the dashboard.

I tried doing this but I get stack overflow and server crash...

So to be clear I tried to add this last() but not sure this works at all as other routes might be added after dynamically.

what am I doing wrong here?  thanks.
  
router.get().failureHandler(event -> {
log.warn("Failed to handle route: {}", event.failure().getMessage());
event.reroute("/app/dashboard/");
});

router.route(HttpMethod.GET, "/*").last().handler(event -> event.reroute("/app/dashboard/"));

Thomas SEGISMONT

unread,
Jun 3, 2019, 4:32:10 AM6/3/19
to ve...@googlegroups.com
If you get stack overflow perhaps something wrong happens while rerouting and the request is rerouted to the dashboard again.

I don't know the business context but I instead of redirecting internally I would send a 302 response:

HTTP/1.1 302 Found
Location: /app/dashboard

And also, sometimes it's just better to send a 404. If you have static file for examples, and the file is missing, then your reply will always be the dashboard html.

Gadi Eichhorn

unread,
Jun 3, 2019, 4:42:07 AM6/3/19
to vert.x
Thanks, Thomas,

What I need to accomplish is any URL that is not matched by the router will be redirected to dashboard.

Did you mean this? is it guaranteed to be matched last? as I mentioned I have other routes that are added dynamically so very likely to be added after this route is defined.


router.route(HttpMethod.GET, "/*").last().handler(event -> event.response().putHeader("location", "/app/dashboard/").setStatusCode(302).end());

Thomas SEGISMONT

unread,
Jun 3, 2019, 4:51:44 AM6/3/19
to ve...@googlegroups.com
Le lun. 3 juin 2019 à 10:42, 'Gadi Eichhorn' via vert.x <ve...@googlegroups.com> a écrit :
Thanks, Thomas,

What I need to accomplish is any URL that is not matched by the router will be redirected to dashboard.

Did you mean this? is it guaranteed to be matched last? as I mentioned I have other routes that are added dynamically so very likely to be added after this route is defined.


router.route(HttpMethod.GET, "/*").last().handler(event -> event.response().putHeader("location", "/app/dashboard/").setStatusCode(302).end());

Yes I meant this. Except that the the catch all path ("/*") is not really needed:

router.get().last().handler(event -> event.response().putHeader("location", "/app/dashboard/").setStatusCode(302).end());

The last() method call sets the priority to Integer.MAX_VALUE so unless you invoke it on other routes, this route will be processed last.

 


On Monday, June 3, 2019 at 9:32:10 AM UTC+1, Thomas SEGISMONT wrote:
If you get stack overflow perhaps something wrong happens while rerouting and the request is rerouted to the dashboard again.

I don't know the business context but I instead of redirecting internally I would send a 302 response:

HTTP/1.1 302 Found
Location: /app/dashboard

And also, sometimes it's just better to send a 404. If you have static file for examples, and the file is missing, then your reply will always be the dashboard html.

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/ad567972-9632-4172-be70-6dca6ce439a8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages