Skip to content

Commit

Permalink
Identify non application routes with a name
Browse files Browse the repository at this point in the history
The name is used in the metrics if defined and thus we can pass a fully
qualifier path for the route.
  • Loading branch information
gsmet committed Jan 10, 2025
1 parent 8d81d78 commit ce1d85f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ public static class Builder extends RouteBuildItem.Builder {
private final NonApplicationRootPathBuildItem buildItem;
private RouteBuildItem.RouteType routeType = RouteBuildItem.RouteType.FRAMEWORK_ROUTE;
private RouteBuildItem.RouteType routerType = RouteBuildItem.RouteType.FRAMEWORK_ROUTE;
private String name;
private String path;

Builder(NonApplicationRootPathBuildItem buildItem) {
Expand Down Expand Up @@ -330,7 +331,13 @@ public Builder orderedRoute(String route, Integer order, Consumer<Route> routeFu
this.path = route;
this.routerType = RouteBuildItem.RouteType.ABSOLUTE_ROUTE;
}
super.orderedRoute(this.path, order, routeFunction);

// we normalize the route name to remove trailing *, this is to be consistent with the path
// see RouteImpl#setPath()
String routeName = route.charAt(route.length() - 1) == '*' ? route.substring(0, route.length() - 1) : route;

// we pass a route name for proper identification in the metrics
super.orderedRoute(routeName, this.path, order, routeFunction);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,20 @@ public Builder orderedRoute(String route, Integer order, Consumer<Route> routeCu
return this;
}

/**
* @param name The name of the route. It is used to identify the route in the metrics.
* @param route A normalized path used to define a basic route
* (e.g. use HttpRootPathBuildItem to construct/resolve the path value). This path this is also
* used on the "Not Found" page in dev mode.
* @param order Priority ordering of the route
* @param routeCustomizer Route customizer.
*/
public Builder orderedRoute(String name, String route, Integer order, Consumer<Route> routeCustomizer) {
this.routeFunction = new BasicRoute(name, route, order, routeCustomizer);
this.notFoundPagePath = this.routePath = route;
return this;
}

public Builder handler(Handler<RoutingContext> handler) {
this.handler = handler;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,38 @@

public class BasicRoute implements Function<Router, Route> {

private String name;

private String path;

private Integer order;

private Consumer<Route> customizer;

public BasicRoute(String path) {
this(path, null);
this(null, path);
}

public BasicRoute(String path, Integer order) {
this(null, path, order);
}

public BasicRoute(String path, Integer order, Consumer<Route> customizer) {
this(null, path, order, customizer);
}

public BasicRoute(String name, String path) {
this(name, path, null);
}

public BasicRoute(String name, String path, Integer order) {
this.name = name;
this.path = path;
this.order = order;
}

public BasicRoute(String path, Integer order, Consumer<Route> customizer) {
public BasicRoute(String name, String path, Integer order, Consumer<Route> customizer) {
this.name = name;
this.path = path;
this.order = order;
this.customizer = customizer;
Expand All @@ -32,6 +48,14 @@ public BasicRoute(String path, Integer order, Consumer<Route> customizer) {
public BasicRoute() {
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPath() {
return path;
}
Expand Down Expand Up @@ -60,6 +84,9 @@ public BasicRoute setCustomizer(Consumer<Route> customizer) {
@Override
public Route apply(Router router) {
Route route = router.route(path);
if (name != null) {
route.setName(name);
}
if (order != null) {
route.order(order);
}
Expand Down

0 comments on commit ce1d85f

Please sign in to comment.