vertx-web icon indicating copy to clipboard operation
vertx-web copied to clipboard

Branching route handler helper

Open jponge opened this issue 5 months ago • 5 comments

This is useful when building elaborated routes in a programmatic fashion (e.g., predicates).

jponge avatar Jul 22 '25 13:07 jponge

If you're building routes manually for your API, then yes, I'd do what you did in the second snippet and roll my own handler with a conditional branching structure 😃

Where this gets more complicated is when you are building routes programmatically, and especially when you want branching and not just chains. A good example is when you mount a handler in the chain that doesn't provide any way back / vertx-http-proxy is a good example (and you can't just leverage failure recovery at the router level).

The use-case I have in mind is building predicates, so you can enable a route handler only if certain conditions are met (e.g., some request header is present, etc). Having such a building block is helpful, especially when you add routes programmatically.

jponge avatar Jul 24 '25 11:07 jponge

A good example is when you mount a handler in the chain that doesn't provide any way back

Wouldn't this do the trick?

Handler<RoutingContext> handler = createMyAwesomeHandler();
router.get("/branching").handler(rc -> {
    if (myConditionIsMet()) {
        handler.handle(rc);
    }
    else {
        rc.next();
    }
});

tsegismont avatar Jul 24 '25 13:07 tsegismont

It does, but having a building block like this is also a viable route

jponge avatar Jul 24 '25 14:07 jponge

It does, but having a building block like this is also a viable route

Not sure what you mean with "like this". Did you mean like the proposed BranchingHandler? In this case, the following code:

Handler<RoutingContext> handler = createMyAwesomeHandler();
router.get("/branching").handler(BranchingHandler.create(
    rc -> myConditionIsMet(),
    handler::handle
));

is not much more concise or readable than:

Handler<RoutingContext> handler = createMyAwesomeHandler();
router.get("/branching").handler(rc -> {
    if (myConditionIsMet()) {
        handler.handle(rc);
    } else {
        rc.next();
    }
});

Sorry if I'm missing something obvious.

tsegismont avatar Jul 24 '25 14:07 tsegismont

I'm not looking for conciseness but composability.

jponge avatar Jul 24 '25 15:07 jponge