Skip to content

Commit 38e1616

Browse files
authored
rename path (#136)
1 parent f331fd4 commit 38e1616

File tree

4 files changed

+34
-18
lines changed

4 files changed

+34
-18
lines changed

avaje-jex/src/main/java/io/avaje/jex/DefaultRouting.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.util.ArrayDeque;
44
import java.util.ArrayList;
55
import java.util.Collection;
6-
import java.util.Collections;
76
import java.util.Deque;
87
import java.util.HashMap;
98
import java.util.List;
@@ -38,10 +37,10 @@ private String path(String path) {
3837
return String.join("", pathDeque) + ((path.startsWith("/") || path.isEmpty()) ? path : "/" + path);
3938
}
4039

41-
private void addEndpoints(String path, Group group) {
40+
private void addEndpoints(String path, HttpService group) {
4241
path = path.startsWith("/") ? path : "/" + path;
4342
pathDeque.addLast(path);
44-
group.addGroup(this);
43+
group.add(this);
4544
pathDeque.removeLast();
4645
}
4746

@@ -66,7 +65,7 @@ public <T extends Exception> Routing error(Class<T> type, ExceptionHandler<T> ha
6665
}
6766

6867
@Override
69-
public Routing path(String path, Group group) {
68+
public Routing group(String path, HttpService group) {
7069
addEndpoints(path, group);
7170
return this;
7271
}

avaje-jex/src/main/java/io/avaje/jex/Jex.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.function.Consumer;
55

66
import io.avaje.inject.BeanScope;
7+
import io.avaje.jex.Routing.HttpService;
78
import io.avaje.jex.security.Role;
89
import io.avaje.jex.spi.JexPlugin;
910
import io.avaje.jex.spi.JsonService;
@@ -170,6 +171,26 @@ default <T extends Exception> Jex error(Class<T> exceptionClass, ExceptionHandle
170171
return this;
171172
}
172173

174+
/**
175+
* Add a group of route handlers with a common path prefix.
176+
*
177+
* <pre>{@code
178+
* routing.path("api", g -> {
179+
* g.get("/", ctx -> ctx.text("apiRoot"));
180+
* g.get("{id}", ctx -> ctx.text("api-" + ctx.pathParam("id")));
181+
* });
182+
*
183+
* }</pre>
184+
*
185+
* @param path the common path prefix
186+
* @param group the function to register the rout handlers
187+
*
188+
*/
189+
default Jex group(String path, HttpService group) {
190+
routing().group(path, group);
191+
return this;
192+
}
193+
173194
/**
174195
* Sets the JSON service to use for serialization and deserialization.
175196
*

avaje-jex/src/main/java/io/avaje/jex/Routing.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,18 @@ public sealed interface Routing permits DefaultRouting {
3131
* Add a group of route handlers with a common path prefix.
3232
*
3333
* <pre>{@code
34-
* routing.path("api", () -> {
35-
* routing.get("/", ctx -> ctx.text("apiRoot"));
36-
* routing.get("{id}", ctx -> ctx.text("api-" + ctx.pathParam("id")));
34+
* routing.path("api", g -> {
35+
* g.get("/", ctx -> ctx.text("apiRoot"));
36+
* g.get("{id}", ctx -> ctx.text("api-" + ctx.pathParam("id")));
3737
* });
3838
*
3939
* }</pre>
40+
*
41+
* @param path the common path prefix
42+
* @param group the function to register the rout handlers
43+
*
4044
*/
41-
Routing path(String path, Group group);
45+
Routing group(String path, HttpService group);
4246

4347
/**
4448
* Adds a HEAD handler to the route configuration.
@@ -142,14 +146,6 @@ default Routing after(Consumer<Context> handler) {
142146
/** Return all the registered Exception Handlers. */
143147
Map<Class<?>, ExceptionHandler<?>> errorHandlers();
144148

145-
/** A group of routing entries prefixed by a common path. */
146-
@FunctionalInterface
147-
interface Group {
148-
149-
/** Add the group of entries with a common prefix. */
150-
void addGroup(Routing routing);
151-
}
152-
153149
/** Adds to the Routing. */
154150
@FunctionalInterface
155151
interface HttpService {

avaje-jex/src/test/java/io/avaje/jex/core/NestedRoutesTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ static TestPair init() {
1616
Jex app = Jex.create()
1717
.routing(routing -> routing
1818
.get("/", ctx -> ctx.text("hello"))
19-
.path("api", g -> {
19+
.group("api", g -> {
2020
g.get("/", ctx -> ctx.text("apiRoot"));
2121
g.get("{id}", ctx -> ctx.text("api-" + ctx.pathParam("id")));
2222
})
23-
.path("extra", g -> {
23+
.group("extra", g -> {
2424
g.get("/", ctx -> ctx.text("extraRoot"));
2525
g.get("{id}", ctx -> ctx.text("extra-id-" + ctx.pathParam("id")));
2626
g.get("more/{id}", ctx -> ctx.text("extraMore-" + ctx.pathParam("id")));

0 commit comments

Comments
 (0)