Skip to content

Commit

Permalink
Add not found management on vertx
Browse files Browse the repository at this point in the history
  • Loading branch information
Dufgui committed Dec 16, 2019
1 parent 6873b49 commit 905272e
Show file tree
Hide file tree
Showing 8 changed files with 316 additions and 116 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.quarkus.vertx.http.deployment;

import io.quarkus.builder.item.MultiBuildItem;
import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext;

/**
* A handler that is applied to every route
*/
public final class FailureBuildItem extends MultiBuildItem {

private final Handler<RoutingContext> handler;

/**
* Creates a new instance of {@link FailureBuildItem}.
*
* @param handler the handler, if {@code null} the filter won't be used.
*/
public FailureBuildItem(Handler<RoutingContext> handler) {
this.handler = handler;
}

public Handler<RoutingContext> getHandler() {
return handler;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import io.quarkus.vertx.http.runtime.RouterProducer;
import io.quarkus.vertx.http.runtime.VertxHttpRecorder;
import io.quarkus.vertx.http.runtime.cors.CORSRecorder;
import io.quarkus.vertx.http.runtime.error.ErrorRecorder;
import io.quarkus.vertx.http.runtime.filters.Filter;
import io.vertx.core.impl.VertxImpl;
import io.vertx.ext.web.Router;
Expand All @@ -50,6 +51,12 @@ HttpRootPathBuildItem httpRoot(HttpBuildTimeConfig httpBuildTimeConfig) {
FilterBuildItem cors(CORSRecorder recorder, HttpConfiguration configuration) {
return new FilterBuildItem(recorder.corsHandler(configuration), FilterBuildItem.CORS);
}

@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
FailureBuildItem errors(ErrorRecorder recorder, HttpConfiguration configuration, LaunchModeBuildItem launchMode) {
return new FailureBuildItem(recorder.errorHandler(configuration, launchMode.getLaunchMode()));
}

@BuildStep
AdditionalBeanBuildItem additionalBeans() {
Expand Down Expand Up @@ -103,7 +110,7 @@ ServiceStartBuildItem finalizeRouter(
VertxHttpRecorder recorder, BeanContainerBuildItem beanContainer,
Optional<RequireVirtualHttpBuildItem> requireVirtual, InternalWebVertxBuildItem vertx,
LaunchModeBuildItem launchMode, ShutdownContextBuildItem shutdown,
List<DefaultRouteBuildItem> defaultRoutes, List<FilterBuildItem> filters,
List<DefaultRouteBuildItem> defaultRoutes, List<FilterBuildItem> filters, FailureBuildItem failure,
VertxWebRouterBuildItem router, EventLoopCountBuildItem eventLoopCount,
HttpBuildTimeConfig httpBuildTimeConfig, HttpConfiguration httpConfiguration,
BuildProducer<ReflectiveClassBuildItem> reflectiveClass, List<WebsocketSubProtocolsBuildItem> websocketSubProtocols)
Expand All @@ -126,7 +133,7 @@ ServiceStartBuildItem finalizeRouter(

recorder.finalizeRouter(beanContainer.getValue(),
defaultRoute.map(DefaultRouteBuildItem::getRoute).orElse(null),
listOfFilters, vertx.getVertx(), router.getRouter(), httpBuildTimeConfig.rootPath, launchMode.getLaunchMode());
listOfFilters, failure.getHandler(), vertx.getVertx(), router.getRouter(), httpBuildTimeConfig.rootPath, launchMode.getLaunchMode());

boolean startVirtual = requireVirtual.isPresent() || httpBuildTimeConfig.virtual;
if (startVirtual) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.quarkus.vertx.http.runtime.cors.CORSConfig;
import io.quarkus.vertx.http.runtime.error.FailureConfig;

@ConfigRoot(phase = ConfigPhase.RUN_TIME)
public class HttpConfiguration {
Expand Down Expand Up @@ -53,6 +54,11 @@ public class HttpConfiguration {
* The CORS config
*/
public CORSConfig cors;

/**
* The Failure config
*/
public FailureConfig failure;

/**
* The SSL config
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public void startServer(RuntimeValue<Vertx> vertxRuntimeValue, ShutdownContext s
}

public void finalizeRouter(BeanContainer container, Consumer<Route> defaultRouteHandler,
List<Filter> filterList, RuntimeValue<Vertx> vertx,
List<Filter> filterList, Handler<RoutingContext> failureHandler, RuntimeValue<Vertx> vertx,
RuntimeValue<Router> runtimeValue, String rootPath, LaunchMode launchMode) {
// install the default route at the end
Router router = runtimeValue.getValue();
Expand Down Expand Up @@ -198,7 +198,7 @@ public void finalizeRouter(BeanContainer container, Consumer<Route> defaultRoute
}

container.instance(RouterProducer.class).initialize(resumingRouter);
router.route().last().failureHandler(new QuarkusErrorHandler(launchMode.isDevOrTest()));
router.route().last().failureHandler(failureHandler);

if (rootPath.equals("/")) {
if (hotReplacementHandler != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.quarkus.vertx.http.runtime.error;

import io.quarkus.runtime.LaunchMode;
import io.quarkus.runtime.annotations.Recorder;
import io.quarkus.vertx.http.runtime.HttpConfiguration;
import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext;

@Recorder
public class ErrorRecorder {

public Handler<RoutingContext> errorHandler(HttpConfiguration configuration, LaunchMode launchMode) {
if (configuration.failure.handler.isPresent()) {
//todo manage handler from config
return null;
} else {
return new QuarkusErrorHandler(launchMode.isDevOrTest());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.quarkus.vertx.http.runtime.error;

import java.util.Optional;

import io.quarkus.runtime.annotations.ConfigGroup;
import io.quarkus.runtime.annotations.ConfigItem;

@ConfigGroup
public class FailureConfig {

/**
* Failure handler
*/
@ConfigItem
public Optional<String> handler;

@Override
public String toString() {
return "FailureConfig{" +
"handler=" + handler +
'}';
}
}
Loading

0 comments on commit 905272e

Please sign in to comment.