Java Code Examples for io.vertx.ext.web.Router#mountSubRouter()
The following examples show how to use
io.vertx.ext.web.Router#mountSubRouter() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebVerticle.java From djl-demo with Apache License 2.0 | 5 votes |
@Override public void start() { HttpServer server = vertx.createHttpServer(); Router router = Router.router(vertx); router.route().handler(BodyHandler.create()); SockJSHandler sockJSHandler = SockJSHandler.create(vertx); BridgeOptions options = new BridgeOptions(); options.addInboundPermitted(new PermittedOptions().setAddress(ADDRESS_TRAINER_REQUEST)); options.addOutboundPermitted(new PermittedOptions().setAddress(ADDRESS_TRAINER)); // Event bus router.mountSubRouter("/api/eventbus", sockJSHandler.bridge(options)); // Static content (UI) router.route("/*").handler(StaticHandler.create()); router.route("/*").handler(rc -> { if (!rc.currentRoute().getPath().startsWith("/api")) { rc.reroute("/index.html"); } }); server.requestHandler(router).listen(port, http -> { if (http.succeeded()) { LOGGER.info("HTTP server started: http://localhost:{0}", String.valueOf(port)); } else { LOGGER.info("HTTP server failed on port {0}", String.valueOf(port)); } }); }
Example 2
Source File: VaadinVerticle.java From vertx-vaadin with MIT License | 5 votes |
private Future<Router> startupHttpServer(VertxVaadin vertxVaadin) { String mountPoint = vertxVaadin.config().mountPoint(); HttpServerOptions serverOptions = new HttpServerOptions().setCompressionSupported(true); Router router = Router.router(vertx); router.mountSubRouter(mountPoint, vertxVaadin.router()); httpServer = vertx.createHttpServer(serverOptions).requestHandler(router); Promise<HttpServer> promise = Promise.promise(); Future<HttpServer> future = promise.future(); future.setHandler(event -> { if (event.succeeded()) { log.info("Started vaadin verticle " + getClass().getName() + " on port " + event.result().actualPort()); } else { log.error("Cannot start http server", event.cause()); } }); httpPort().setHandler(event -> { if (event.succeeded()) { httpServer.listen(event.result(), promise); } else { promise.fail(event.cause()); } }); return future.map(router); }
Example 3
Source File: RouteWithRegExTest.java From rest.vertx with Apache License 2.0 | 5 votes |
@BeforeAll static void start() { before(); TestRegExRest testRest = new TestRegExRest(); Router router = RestRouter.register(vertx, testRest); router = router.mountSubRouter("/sub", router); vertx.createHttpServer() .requestHandler(router) .listen(PORT); }
Example 4
Source File: RouteSubPathTest.java From rest.vertx with Apache License 2.0 | 5 votes |
@BeforeAll static void start() { before(); TestPathRest testRest = new TestPathRest(); Router router = RestRouter.register(vertx, testRest); router.mountSubRouter("/sub", router); vertx.createHttpServer() .requestHandler(router) .listen(PORT); }
Example 5
Source File: RouteWithQueryTest.java From rest.vertx with Apache License 2.0 | 5 votes |
@BeforeAll static void start() { before(); TestQueryRest testRest = new TestQueryRest(); Router router = RestRouter.register(vertx, testRest); router.mountSubRouter("/sub", router); vertx.createHttpServer() .requestHandler(router) .listen(PORT); }
Example 6
Source File: HystrixDashboardVerticle.java From standalone-hystrix-dashboard with MIT License | 5 votes |
/** * Create the routes for dashboard app * * @return A {@link Router} with all the routes needed for the app. */ private Router createRoutes() { final Router hystrixRouter = Router.router(vertx); // proxy stream handler hystrixRouter.get("/proxy.stream").handler(HystrixDashboardProxyConnectionHandler.create()); // proxy the eureka apps listing hystrixRouter.get("/eureka").handler(HystrixDashboardProxyEurekaAppsListingHandler.create(vertx)); hystrixRouter.route("/*") .handler(StaticHandler.create() .setCachingEnabled(true) .setCacheEntryTimeout(1000L * 60 * 60 * 24)); final Router mainRouter = Router.router(vertx); // if send a route without the trailing '/' some problems will occur, so i redirect the guy using the trailing '/' mainRouter.route("/hystrix-dashboard") .handler(context -> { if (context.request().path().endsWith("/")) { context.next(); } else { context.response() .setStatusCode(HttpResponseStatus.MOVED_PERMANENTLY.code()) .putHeader(HttpHeaders.LOCATION, "/hystrix-dashboard/") .end(); } }); mainRouter.mountSubRouter("/hystrix-dashboard", hystrixRouter); return mainRouter; }
Example 7
Source File: HttpTermServerSubRouterTest.java From vertx-shell with Apache License 2.0 | 5 votes |
@Override protected TermServer createServer(TestContext context, HttpTermOptions options) { HttpServer httpServer = vertx.createHttpServer(new HttpServerOptions().setPort(8080)); Router router = Router.router(vertx); Router subRouter = Router.router(vertx); router.mountSubRouter("/sub", subRouter); httpServer.requestHandler(router); Async async = context.async(); httpServer.listen(8080, context.asyncAssertSuccess(s -> { async.complete(); })); async.awaitSuccess(20000); return TermServer.createHttpTermServer(vertx, subRouter, options); }
Example 8
Source File: ShellExamples.java From vertx-shell with Apache License 2.0 | 4 votes |
public void shellServer(Vertx vertx, Router router) { ShellServer server = ShellServer.create(vertx); // <1> Router shellRouter = Router.router(vertx); // <2> router.mountSubRouter("/shell", shellRouter); TermServer httpTermServer = TermServer.createHttpTermServer(vertx, router); TermServer sshTermServer = TermServer.createSSHTermServer(vertx); // <3> server.registerTermServer(httpTermServer); // <4> server.registerTermServer(sshTermServer); server.registerCommandResolver(CommandResolver.baseCommands(vertx)); // <5> server.listen(); // <6> }
Example 9
Source File: RestApiVerticle.java From vertx-mqtt-broker with Apache License 2.0 | 4 votes |
@Override public void start() throws Exception { JsonObject restServerConf = config().getJsonObject("rest_server", new JsonObject()); int httpPort = restServerConf.getInteger("port", 2883); String mqttAddress = MQTTSession.ADDRESS; HttpServer server = vertx.createHttpServer(); Router router = Router.router(vertx); long size1mb = 1024*1024 ; //bytes router.route().handler(BodyHandler.create().setBodyLimit(size1mb)); // http://<host:port>/pubsub/publish?channel=<channel1>&qos=0&retained=0 // qos: MOST_ONE, LEAST_ONE, EXACTLY_ONC router.post("/pubsub/publish").handler( req -> { MultiMap headers = req.request().headers(); MultiMap params = req.request().params(); String tenant; if(headers.contains("tenant")) { tenant = headers.get("tenant"); } else { tenant = params.get("tenant"); } String topic; if(params.contains("topic")) { topic = req.request().params().get("topic"); } else if (params.contains("channel")) { topic = req.request().params().get("channel"); } else { throw new IllegalArgumentException("parameter 'topic' is required"); } String qos = req.request().params().get("qos"); String retained = req.request().params().get("retained"); PublishMessage msg = new PublishMessage(); msg.setMessageID(1); msg.setTopicName(topic); if ( qos != null) { AbstractMessage.QOSType theqos = AbstractMessage.QOSType.valueOf(qos); msg.setQos(theqos); } if (retained != null) msg.setRetainFlag(true); try { Buffer body = req.getBody(); byte[] payload = body.getBytes(); msg.setPayload(ByteBuffer.wrap(payload)); MQTTEncoder enc = new MQTTEncoder(); DeliveryOptions opt = new DeliveryOptions() .addHeader(MQTTSession.TENANT_HEADER, tenant); vertx.eventBus().publish(mqttAddress, enc.enc(msg), opt); req.response().setStatusCode(200); } catch (Throwable e) { logger.error(e.getMessage(), e); req.response().setStatusCode(500); req.response().setStatusMessage(e.getMessage()); } req.response().end(); }); router.exceptionHandler(event -> { logger.error(event.getMessage(), event.getCause()); }); // JWT AUTH SPAuthHandler spAuthHandler = SPAuthHandler.create(vertx); Router mainRouter = Router.router(vertx); mainRouter.route("/sp/*") .handler(spAuthHandler::validateJWTToken) // .handler(spAuthHandler::validateTenant) ; mainRouter.route("/api/v2/*") .handler(spAuthHandler::validateJWTToken) // .handler(spAuthHandler::validateTenant) ; // retrocompatilità con vecchie api mainRouter.mountSubRouter("/sp", router); // nuovi path per le nuove api mainRouter.mountSubRouter("/api/v2", router); mainRouter.mountSubRouter("/", router); mainRouter.mountSubRouter("/api/1.2", router); mainRouter.route().handler( ctx -> ctx.response().end() ); // JWT AUTH END server.requestHandler(mainRouter::accept).listen(httpPort, event -> { if (event.succeeded()) { logger.info("RestApiVerticle http server started on http://<host>:" + server.actualPort()); } else { logger.info("RestApiVerticle http server NOT started !"); } }); logger.info("RestApiVerticle started" ); }
Example 10
Source File: ITVertxWebTracing.java From brave with Apache License 2.0 | 4 votes |
@Override protected void init() { stop(); vertx = Vertx.vertx(new VertxOptions()); Router router = Router.router(vertx); router.route(HttpMethod.OPTIONS, "/").handler(ctx -> { ctx.response().end("bar"); }); router.route("/foo").handler(ctx -> { ctx.response().end("bar"); }); router.route("/async").handler(ctx -> { if (Tracing.currentTracer().currentSpan() == null) { throw new IllegalStateException("couldn't read current span!"); } ctx.request().endHandler(v -> ctx.response().end("bar")); }); router.route("/reroute").handler(ctx -> { ctx.reroute("/foo"); }); router.route("/rerouteAsync").handler(ctx -> { if (Tracing.currentTracer().currentSpan() == null) { throw new IllegalStateException("couldn't read current span!"); } ctx.reroute("/async"); }); router.route("/baggage").handler(ctx -> { ctx.response().end(BAGGAGE_FIELD.getValue()); }); router.route("/badrequest").handler(ctx -> { ctx.response().setStatusCode(400).end(); }); router.route("/child").handler(ctx -> { httpTracing.tracing().tracer().nextSpan().name("child").start().finish(); ctx.response().end("happy"); }); router.route("/exception").handler(ctx -> { ctx.fail(503, NOT_READY_ISE); }); router.route("/items/:itemId").handler(ctx -> { ctx.response().end(ctx.request().getParam("itemId")); }); router.route("/async_items/:itemId").handler(ctx -> { if (Tracing.currentTracer().currentSpan() == null) { throw new IllegalStateException("couldn't read current span!"); } ctx.request().endHandler(v -> ctx.response().end(ctx.request().getParam("itemId"))); }); Router subrouter = Router.router(vertx); subrouter.route("/items/:itemId").handler(ctx -> { ctx.response().end(ctx.request().getParam("itemId")); }); router.mountSubRouter("/nested", subrouter); router.route("/exceptionAsync").handler(ctx -> { ctx.request().endHandler(v -> ctx.fail(503, NOT_READY_ISE)); }); Handler<RoutingContext> routingContextHandler = VertxWebTracing.create(httpTracing).routingContextHandler(); router.route() .order(-1).handler(routingContextHandler) .failureHandler(routingContextHandler); server = vertx.createHttpServer(new HttpServerOptions().setPort(0).setHost("localhost")); CountDownLatch latch = new CountDownLatch(1); server.requestHandler(router::handle).listen(async -> { port = async.result().actualPort(); latch.countDown(); }); awaitFor10Seconds(latch, "server didn't start"); }