Skip to content

Commit

Permalink
In addition to the regular Router, also exposes the Mutiny variant as…
Browse files Browse the repository at this point in the history
… bean and event.

Fix quarkusio#20185
  • Loading branch information
cescoffier committed Sep 16, 2021
1 parent 550e59a commit 0b8a134
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 6 deletions.
9 changes: 9 additions & 0 deletions docs/src/main/asciidoc/reactive-routes.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,15 @@ Check the https://vertx.io/docs/vertx-web/java/#_basic_vert_x_web_concepts[Vert.
If you use `quarkus-resteasy` or `quarkus-reactive-routes`, the extension will be added automatically.
====
You can also receive the Mutiny variant of the Router (`io.vertx.mutiny.ext.web.Router`):
[source,java]
----
public void init(@Observes io.vertx.mutiny.ext.web.Router router) {
router.get("/my-route").handler(rc -> rc.response().endAndForget("Hello from my route"));
}
----
== Intercepting HTTP requests
You can also register filters that would intercept incoming HTTP requests.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ VertxWebRouterBuildItem initializeRouter(VertxHttpRecorder recorder,
ShutdownContextBuildItem shutdown) {

RuntimeValue<Router> httpRouteRouter = recorder.initializeRouter(vertx.getVertx());
RuntimeValue<io.vertx.mutiny.ext.web.Router> mutinyRouter = recorder.createMutinyRouter(httpRouteRouter);
RuntimeValue<Router> frameworkRouter = null;
RuntimeValue<Router> mainRouter = null;

Expand Down Expand Up @@ -182,7 +183,7 @@ VertxWebRouterBuildItem initializeRouter(VertxHttpRecorder recorder,
}
}

return new VertxWebRouterBuildItem(httpRouteRouter, mainRouter, frameworkRouter);
return new VertxWebRouterBuildItem(httpRouteRouter, mainRouter, frameworkRouter, mutinyRouter);
}

@BuildStep
Expand Down Expand Up @@ -258,6 +259,7 @@ ServiceStartBuildItem finalizeRouter(
recorder.finalizeRouter(beanContainer.getValue(),
defaultRoute.map(DefaultRouteBuildItem::getRoute).orElse(null),
listOfFilters, vertx.getVertx(), lrc, mainRouter, httpRouteRouter.getHttpRouter(),
httpRouteRouter.getMutinyRouter(),
httpRootPathBuildItem.getRootPath(),
launchMode.getLaunchMode(),
!requireBodyHandlerBuildItems.isEmpty(), bodyHandler, httpConfiguration, gracefulShutdownFilter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@ public final class VertxWebRouterBuildItem extends SimpleBuildItem {
private RuntimeValue<Router> httpRouter;
private RuntimeValue<Router> mainRouter;
private RuntimeValue<Router> frameworkRouter;
private RuntimeValue<io.vertx.mutiny.ext.web.Router> mutinyRouter;

VertxWebRouterBuildItem(RuntimeValue<Router> httpRouter, RuntimeValue<Router> mainRouter,
RuntimeValue<Router> frameworkRouter) {
RuntimeValue<Router> frameworkRouter, RuntimeValue<io.vertx.mutiny.ext.web.Router> mutinyRouter) {
this.httpRouter = httpRouter;
this.mainRouter = mainRouter;
this.frameworkRouter = frameworkRouter;
this.mutinyRouter = mutinyRouter;
}

public RuntimeValue<Router> getHttpRouter() {
return httpRouter;
}

public RuntimeValue<io.vertx.mutiny.ext.web.Router> getMutinyRouter() {
return mutinyRouter;
}

/**
* Will be {@code null} if `${quarkus.http.root-path}` is {@literal /}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,21 @@ public class UserRouteRegistrationTest {
@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(BeanRegisteringRouteUsingObserves.class, BeanRegisteringRouteUsingInject.class));
.addClasses(BeanRegisteringRouteUsingObserves.class,
BeanRegisteringRouteUsingObservesWithMutinyRouter.class,
BeanRegisteringRouteUsingInject.class,
BeanRegisteringRouteUsingInjectWithMutinyRouter.class));

@Test
public void test() {
assertThat(RestAssured.get("/observes").asString()).isEqualTo("observers - ok");
assertThat(RestAssured.get("/observes-mutiny").asString()).isEqualTo("observers mutiny - ok");
assertThat(RestAssured.get("/inject").asString()).isEqualTo("inject - ok");
assertThat(RestAssured.get("/inject-mutiny").asString()).isEqualTo("inject mutiny - ok");
assertThat(given().body("test").contentType("text/plain").post("/body").asString()).isEqualTo("test");
assertThat(given().body("test mutiny").contentType("text/plain").post("/body-mutiny").asString())
.isEqualTo("test mutiny");

}

@ApplicationScoped
Expand All @@ -41,6 +49,15 @@ public void register(@Observes Router router) {

}

@ApplicationScoped
static class BeanRegisteringRouteUsingObservesWithMutinyRouter {

public void register(@Observes io.vertx.mutiny.ext.web.Router router) {
router.route("/observes-mutiny").handler(rc -> rc.response().endAndForget("observers mutiny - ok"));
}

}

@ApplicationScoped
static class BeanRegisteringRouteUsingInject {

Expand All @@ -55,4 +72,18 @@ public void register(@Observes StartupEvent ignored) {
}

}

@ApplicationScoped
static class BeanRegisteringRouteUsingInjectWithMutinyRouter {

@Inject
io.vertx.mutiny.ext.web.Router router;

public void register(@Observes StartupEvent ignored) {
router.route("/inject-mutiny").handler(rc -> rc.response().endAndForget("inject mutiny - ok"));
router.route("/body-mutiny").consumes("text/plain").handler(io.vertx.mutiny.ext.web.handler.BodyHandler.create())
.handler(rc -> rc.response().endAndForget(rc.getBodyAsString()));
}

}
}
4 changes: 4 additions & 0 deletions extensions/vertx-http/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx</artifactId>
</dependency>
<dependency>
<groupId>io.smallrye.reactive</groupId>
<artifactId>smallrye-mutiny-vertx-web</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
public class RouterProducer {

private volatile Router router;
private volatile io.vertx.mutiny.ext.web.Router mutinyRouter;

void initialize(Router router) {
void initialize(Router router, io.vertx.mutiny.ext.web.Router mutinyRouter) {
this.router = router;
this.mutinyRouter = mutinyRouter;
}

// Note that we need a client proxy because if a bean also @Observes Router a null value would be injected
Expand All @@ -22,4 +24,9 @@ Router produceRouter() {
return router;
}

@Produces
io.vertx.mutiny.ext.web.Router produceMutinyRouter() {
return mutinyRouter;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ public RuntimeValue<Router> initializeRouter(final Supplier<Vertx> vertxRuntimeV
return new RuntimeValue<>(router);
}

public RuntimeValue<io.vertx.mutiny.ext.web.Router> createMutinyRouter(final RuntimeValue<Router> router) {
return new RuntimeValue<>(new io.vertx.mutiny.ext.web.Router(router.getValue()));
}

public void startServer(Supplier<Vertx> vertx, ShutdownContext shutdown,
HttpBuildTimeConfig httpBuildTimeConfig, HttpConfiguration httpConfiguration,
LaunchMode launchMode,
Expand Down Expand Up @@ -271,7 +275,8 @@ public void mountFrameworkRouter(RuntimeValue<Router> mainRouter, RuntimeValue<R
public void finalizeRouter(BeanContainer container, Consumer<Route> defaultRouteHandler,
List<Filter> filterList, Supplier<Vertx> vertx,
LiveReloadConfig liveReloadConfig, Optional<RuntimeValue<Router>> mainRouterRuntimeValue,
RuntimeValue<Router> httpRouterRuntimeValue, String rootPath, LaunchMode launchMode, boolean requireBodyHandler,
RuntimeValue<Router> httpRouterRuntimeValue, RuntimeValue<io.vertx.mutiny.ext.web.Router> mutinyRouter,
String rootPath, LaunchMode launchMode, boolean requireBodyHandler,
Handler<RoutingContext> bodyHandler, HttpConfiguration httpConfiguration,
GracefulShutdownFilter gracefulShutdownFilter, ShutdownConfig shutdownConfig,
Executor executor) {
Expand All @@ -289,6 +294,8 @@ public void finalizeRouter(BeanContainer container, Consumer<Route> defaultRoute

// Then, fire the resuming router
event.select(Router.class).fire(httpRouteRouter);
// Also fires the Mutiny one
event.select(io.vertx.mutiny.ext.web.Router.class).fire(mutinyRouter.getValue());

for (Filter filter : filterList) {
if (filter.getHandler() != null) {
Expand All @@ -301,7 +308,7 @@ public void finalizeRouter(BeanContainer container, Consumer<Route> defaultRoute
defaultRouteHandler.accept(httpRouteRouter.route().order(DEFAULT_ROUTE_ORDER));
}

container.instance(RouterProducer.class).initialize(httpRouteRouter);
container.instance(RouterProducer.class).initialize(httpRouteRouter, mutinyRouter.getValue());
httpRouteRouter.route().last().failureHandler(new QuarkusErrorHandler(launchMode.isDevOrTest()));

if (requireBodyHandler) {
Expand Down

0 comments on commit 0b8a134

Please sign in to comment.