From b99b1c791ab2e0480116a71a3a8c7fbfd6789089 Mon Sep 17 00:00:00 2001 From: Josiah Noel <32279667+SentryMan@users.noreply.github.com> Date: Fri, 6 Dec 2024 12:17:39 -0500 Subject: [PATCH] rename path --- .../java/io/avaje/jex/DefaultRouting.java | 7 +++---- avaje-jex/src/main/java/io/avaje/jex/Jex.java | 21 +++++++++++++++++++ .../src/main/java/io/avaje/jex/Routing.java | 20 +++++++----------- .../io/avaje/jex/core/NestedRoutesTest.java | 4 ++-- 4 files changed, 34 insertions(+), 18 deletions(-) diff --git a/avaje-jex/src/main/java/io/avaje/jex/DefaultRouting.java b/avaje-jex/src/main/java/io/avaje/jex/DefaultRouting.java index be168e8d..f2cb1561 100644 --- a/avaje-jex/src/main/java/io/avaje/jex/DefaultRouting.java +++ b/avaje-jex/src/main/java/io/avaje/jex/DefaultRouting.java @@ -3,7 +3,6 @@ import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.Deque; import java.util.HashMap; import java.util.List; @@ -38,10 +37,10 @@ private String path(String path) { return String.join("", pathDeque) + ((path.startsWith("/") || path.isEmpty()) ? path : "/" + path); } - private void addEndpoints(String path, Group group) { + private void addEndpoints(String path, HttpService group) { path = path.startsWith("/") ? path : "/" + path; pathDeque.addLast(path); - group.addGroup(this); + group.add(this); pathDeque.removeLast(); } @@ -66,7 +65,7 @@ public Routing error(Class type, ExceptionHandler ha } @Override - public Routing path(String path, Group group) { + public Routing group(String path, HttpService group) { addEndpoints(path, group); return this; } diff --git a/avaje-jex/src/main/java/io/avaje/jex/Jex.java b/avaje-jex/src/main/java/io/avaje/jex/Jex.java index d768e0d7..a7d2a870 100644 --- a/avaje-jex/src/main/java/io/avaje/jex/Jex.java +++ b/avaje-jex/src/main/java/io/avaje/jex/Jex.java @@ -4,6 +4,7 @@ import java.util.function.Consumer; import io.avaje.inject.BeanScope; +import io.avaje.jex.Routing.HttpService; import io.avaje.jex.security.Role; import io.avaje.jex.spi.JexPlugin; import io.avaje.jex.spi.JsonService; @@ -170,6 +171,26 @@ default Jex error(Class exceptionClass, ExceptionHandle return this; } + /** + * Add a group of route handlers with a common path prefix. + * + *
{@code
+   * routing.path("api", g -> {
+   *     g.get("/", ctx -> ctx.text("apiRoot"));
+   *     g.get("{id}", ctx -> ctx.text("api-" + ctx.pathParam("id")));
+   * });
+   *
+   * }
+ * + * @param path the common path prefix + * @param group the function to register the rout handlers + * + */ + default Jex group(String path, HttpService group) { + routing().group(path, group); + return this; + } + /** * Sets the JSON service to use for serialization and deserialization. * diff --git a/avaje-jex/src/main/java/io/avaje/jex/Routing.java b/avaje-jex/src/main/java/io/avaje/jex/Routing.java index 1e54dff3..ae9aaa20 100644 --- a/avaje-jex/src/main/java/io/avaje/jex/Routing.java +++ b/avaje-jex/src/main/java/io/avaje/jex/Routing.java @@ -31,14 +31,18 @@ public sealed interface Routing permits DefaultRouting { * Add a group of route handlers with a common path prefix. * *
{@code
-   * routing.path("api", () -> {
-   *     routing.get("/", ctx -> ctx.text("apiRoot"));
-   *     routing.get("{id}", ctx -> ctx.text("api-" + ctx.pathParam("id")));
+   * routing.path("api", g -> {
+   *     g.get("/", ctx -> ctx.text("apiRoot"));
+   *     g.get("{id}", ctx -> ctx.text("api-" + ctx.pathParam("id")));
    * });
    *
    * }
+ * + * @param path the common path prefix + * @param group the function to register the rout handlers + * */ - Routing path(String path, Group group); + Routing group(String path, HttpService group); /** * Adds a HEAD handler to the route configuration. @@ -142,14 +146,6 @@ default Routing after(Consumer handler) { /** Return all the registered Exception Handlers. */ Map, ExceptionHandler> errorHandlers(); - /** A group of routing entries prefixed by a common path. */ - @FunctionalInterface - interface Group { - - /** Add the group of entries with a common prefix. */ - void addGroup(Routing routing); - } - /** Adds to the Routing. */ @FunctionalInterface interface HttpService { diff --git a/avaje-jex/src/test/java/io/avaje/jex/core/NestedRoutesTest.java b/avaje-jex/src/test/java/io/avaje/jex/core/NestedRoutesTest.java index 67e92230..ef183a8c 100644 --- a/avaje-jex/src/test/java/io/avaje/jex/core/NestedRoutesTest.java +++ b/avaje-jex/src/test/java/io/avaje/jex/core/NestedRoutesTest.java @@ -16,11 +16,11 @@ static TestPair init() { Jex app = Jex.create() .routing(routing -> routing .get("/", ctx -> ctx.text("hello")) - .path("api", g -> { + .group("api", g -> { g.get("/", ctx -> ctx.text("apiRoot")); g.get("{id}", ctx -> ctx.text("api-" + ctx.pathParam("id"))); }) - .path("extra", g -> { + .group("extra", g -> { g.get("/", ctx -> ctx.text("extraRoot")); g.get("{id}", ctx -> ctx.text("extra-id-" + ctx.pathParam("id"))); g.get("more/{id}", ctx -> ctx.text("extraMore-" + ctx.pathParam("id")));