Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible to handle routes for base URI without path from extensions #42840

Closed
mabartos opened this issue Aug 28, 2024 · 3 comments · Fixed by #42896
Closed

Possible to handle routes for base URI without path from extensions #42840

mabartos opened this issue Aug 28, 2024 · 3 comments · Fixed by #42896
Labels
area/vertx kind/enhancement New feature or request
Milestone

Comments

@mabartos
Copy link
Contributor

mabartos commented Aug 28, 2024

Description

In our custom extension (Keycloak), I've encountered the impossibility of defining RouteBuildItem for the base URI without path when the root-path (quarkus.http.root-path) is different than "/".

The thing is that I want to automatically redirect from the base URI to the root-path endpoint.

The case:
quarkus.http.root-path=/auth => our app is accessible on localhost:8080/auth
When I access localhost:8080, I want to be redirected to the localhost:8080/auth.

I tried to build my own RouteBuildItem with the HttpRootPathBuildItem, but I'm not able to do it for the base URI, when the root-path is specified.

See this code (pseudo):

@Record(ExecutionTime.STATIC_INIT)
@BuildStep
RouteBuildItem configureRedirectForRootPath(KeycloakRecorder recorder, HttpRootPathBuildItem httpRootPathBuildItem){
    return httpRootPathBuildItem.routeBuilder()
                .route("/")
                .handler(routingContext -> routingContext.redirect("/auth"))
                .build();
}

But it is not possible to do it with a specified route("/") as an absolute path - it's evaluated as relative path to the root-path. When I change the route to something else (route("/something") and then access it, everything works as expected.

Or is there any other approach how to achieve it?

Implementation ideas

Consider .route("/") as an absolute path unrelated to quarkus.http.root-path. Or is it problematic in some cases?

@mabartos mabartos added the kind/enhancement New feature or request label Aug 28, 2024
@cescoffier
Copy link
Member

This is the code deciding where to mount the route:

if (route.startsWith(buildItem.getRootPath())) {
                // relative to http root (leading slash for vert.x route)
                this.path = "/" + UriNormalizationUtil.relativize(buildItem.getRootPath(), route);
                this.routerType = RouteType.APPLICATION_ROUTE;
            } else if (route.startsWith("/")) {
                // absolute path
                this.path = route;
                this.routerType = RouteType.ABSOLUTE_ROUTE;
            }

I don't see why your route would be mounted on the application router and not as an absolute route.

@mabartos
Copy link
Contributor Author

mabartos commented Aug 30, 2024

@cescoffier The problem is in URI normalization in resolvePath():

route = super.absolutePath = buildItem.resolvePath(route);

Leading to the normalizeWithBase() method.

When considering rootPath=/auth:

UriNormalizationUtil.normalizeWithBase("/auth", "/", false).getPath();  // returns '/auth/'

UriNormalizationUtil.normalizeWithBase("/auth", "/", true).getPath(); // returns '/' => required behavior

@mabartos
Copy link
Contributor Author

@cescoffier Might this be considered as a fix?

#42896

@cescoffier cescoffier changed the title [Vert.X] Possible to handle routes for base URI without path from extensions Possible to handle routes for base URI without path from extensions Aug 30, 2024
@gsmet gsmet closed this as completed in c4beb8c Aug 30, 2024
@quarkus-bot quarkus-bot bot added this to the 3.16 - main milestone Aug 30, 2024
@gsmet gsmet modified the milestones: 3.16 - main, 3.14.2 Aug 30, 2024
gsmet pushed a commit to gsmet/quarkus that referenced this issue Sep 2, 2024
…ensions

Closes quarkusio#42840

Signed-off-by: Martin Bartoš <mabartos@redhat.com>
(cherry picked from commit c4beb8c)
gsmet pushed a commit to gsmet/quarkus that referenced this issue Sep 3, 2024
…ensions

Closes quarkusio#42840

Signed-off-by: Martin Bartoš <mabartos@redhat.com>
(cherry picked from commit c4beb8c)
danielsoro pushed a commit to danielsoro/quarkus that referenced this issue Sep 20, 2024
…ensions

Closes quarkusio#42840

Signed-off-by: Martin Bartoš <mabartos@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/vertx kind/enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants