@Override public void start(Future<Void> startFuture) { Router router = Router.router(vertx); router.route().handler(BodyHandler.create()); router.get("/*").handler(this::log); // 全局日志处理,会执行 next() 到下一个 router.get("/").handler(this::handleRoot); // 首页 router.get("/favicon.ico").handler(c -> c.fail(404)); // 针对浏览器返回404 router.get("/log").handler(this::showLog); // 显示日志 router.routeWithRegex("/([a-z0-9/]*)\\.?(txt|json|png|svg|)") .handler(this::handleGushici); // 核心API调用 router.route().last().handler(c -> c.fail(404)) // 其他返回404 .failureHandler(this::returnError); // 对上面所有的错误进行处理 vertx .createHttpServer() .requestHandler(router::accept) .listen( config().getInteger("http.port", 8080), result -> { if (result.succeeded()) { startFuture.complete(); } else { startFuture.fail(result.cause()); } } ); }
/** * Specify this is the last route for the router. * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.Route last() { delegate.last(); return this; }
@Override public InternalEndpointRoute last() { route.last(); return this; }
/** * Specify this is the last route for the router. * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.Route last() { delegate.last(); return this; }
@Override public Route last() { return delegate.last(); }
@Test public void testLast() throws Exception { String path = "/blah"; Route route = router.route(path); router.route(path).handler(rc -> { rc.response().setChunked(true); rc.response().write("oranges"); rc.next(); }); router.route(path).handler(rc -> { rc.response().write("bananas"); rc.next(); }); route.last(); route.handler(rc -> { rc.response().write("apples"); rc.response().end(); }); testRequest(HttpMethod.GET, path, 200, "OK", "orangesbananasapples"); }
public void init() { LOG.info("Starting Vert.x Dispatcher at [{}:{}]", host, port); httpServer = vertx.createHttpServer(); router = Router.router(vertx); router.exceptionHandler(e -> LOG.error(e.getMessage(), e)); router.route().order(0).handler(bodyHandler.setBodyLimit(maxUploadSize)); router.route().last().handler(f -> { if (!f.response().ended() && f.response().bytesWritten() == 0) { f.response().end(ErrorMessages.buildJson(ErrorMessages.NOT_FOUND)); } }); CountDownLatch await = new CountDownLatch(1); httpServer.requestHandler(router::accept).listen(port, host, r -> await.countDown()); try { if (!await.await(30, TimeUnit.SECONDS)) { throw new RuntimeException("Failed to start Vert.x server!"); } } catch (Throwable t) { throw Exceptions.runtime(t); } isOpen = true; }
public RootRouter(Vertx vertx, RouterStorage storage) { this.storage = storage; this.vertx = vertx; this.router = Router.router(vertx); // Root handlersA router.route().handler(LoggerHandler.create()); // TODO add a dedicated error for api router that informs about // APPLICATION_JSON requirements. This may not be true for other // routes (eg. custom // routes) router.route().last().handler(DefaultNotFoundHandler.create()); router.route().failureHandler(FailureHandler.create()); this.apiRouter = new APIRouter(this); this.customRouter = new CustomRouter(this); }
@Test public void testLast() throws Exception { String path = "/blah"; Route route = router.route(path); router.route(path).handler(rc -> { rc.response().setChunked(true); rc.response().write("oranges"); rc.next(); }); router.route(path).handler(rc -> { rc.response().write("bananas"); rc.next(); }); route.last(); route.handler(rc -> { rc.response().write("apples"); rc.response().end(); }); testRequest(HttpMethod.GET, path, 200, "OK", "orangesbananasapples"); }