use of io.vertx.ext.web.handler.BasicAuthHandler in project vertx-swagger by bobxwang.
the class RestApiVerticle method configRoute.
private void configRoute(final Router router) {
router.get("/logout").handler(context -> {
context.clearUser();
context.response().setStatusCode(302).putHeader("Location", "/").end();
});
AuthProvider authProvider = ChainAuth.create();
AuthHandler basicAuthHandler = BasicAuthHandler.create(authProvider);
router.route("/private/*").handler(basicAuthHandler);
Route route = router.route(HttpMethod.GET, "/private/path/");
route.handler(ctx -> ctx.response().end(new JsonObject().put("rs", new Date().toString()).encodePrettily()));
router.route(HttpMethod.GET, "/some/path/").handler(routingContext -> {
HttpServerResponse response = routingContext.response();
// 由于我们会在不同的处理器里写入响应,因此需要启用分块传输,仅当需要通过多个处理器输出响应时才需要
response.setChunked(true);
response.write("route1\n");
// 1 秒后调用下一个处理器
routingContext.vertx().setTimer(1000, tid -> routingContext.next());
});
router.route(HttpMethod.GET, "/some/path/").handler(routingContext -> {
HttpServerResponse response = routingContext.response();
response.write("route2\n");
// 1 秒后调用下一个处理器
routingContext.vertx().setTimer(1000, tid -> routingContext.next());
});
router.route(HttpMethod.GET, "/some/path/").handler(routingContext -> {
HttpServerResponse response = routingContext.response();
response.write("route3");
// 结束响应
routingContext.response().end();
});
router.route(HttpMethod.GET, "/long/time").blockingHandler(ctx -> {
// call other service maybe long time, so here we using block pattern
try {
Thread.sleep(5000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
ctx.response().end(new JsonObject().put("rs", new Date().toString()).encodePrettily());
});
// 如果您需要在一个阻塞处理器中处理一个 multipart 类型的表单数据,您需要首先使用一个非阻塞的处理器来调用 setExpectMultipart(true)
router.post("/some/longtime/endpoint").handler(ctx -> {
ctx.request().setExpectMultipart(true);
ctx.next();
}).blockingHandler(ctx -> {
// 执行某些阻塞操作
});
}
Aggregations