private static HttpResponse createResponse(HttpRequest req, Router<String> router) { RouteResult<String> routeResult = router.route(req.method(), req.uri()); String content = "router: \n" + router + "\n" + "req: " + req + "\n\n" + "routeResult: \n" + "target: " + routeResult.target() + "\n" + "pathParams: " + routeResult.pathParams() + "\n" + "queryParams: " + routeResult.queryParams() + "\n\n" + "allowedMethods: " + router.allowedMethods(req.uri()); FullHttpResponse res = new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.copiedBuffer(content, CharsetUtil.UTF_8) ); res.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain"); res.headers().set(HttpHeaderNames.CONTENT_LENGTH, res.content().readableBytes()); return res; }
public final void startWeb() { if (httpget.size() + httppost.size() >0) { executor.submit(() -> { final String addr = BotanUtils.envToOpt("HTTP_IP_ADDR").orElse("0.0.0.0"); final int port = Integer.valueOf(BotanUtils.envToOpt("HTTP_PORT").orElse("8080")); final Router<Route> router = new Router<>(); httpget.forEach(router::GET); httppost.forEach(router::POST); final NioEventLoopGroup bossGroup = new NioEventLoopGroup(1); final NioEventLoopGroup workerGroup = new NioEventLoopGroup(); try { final ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .childOption(ChannelOption.TCP_NODELAY, java.lang.Boolean.TRUE) .childOption(ChannelOption.SO_KEEPALIVE, java.lang.Boolean.TRUE) .channel(NioServerSocketChannel.class) .childHandler(new HttpRouterServerInitializer(router)); log.info("RESTful API: {}:{}", addr, port); final Channel ch = b.bind(addr, port).sync().channel(); ch.closeFuture().sync(); } catch (final Exception ignore) { // } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }); } }
private static HttpResponse createResponse(HttpRequest req, Router<Route> router, String body) { final RouteResult<Route> routeResult = router.route(req.method(), req.uri()); if (routeResult != null) { final BotanHttpResponse res = new BotanHttpResponse(); final Object obj = routeResult.target().handle(new BotanHttpRequest(routeResult, body), res); final String content; final String type; final HttpResponseStatus responseStatus; if (obj instanceof BotanHttpResponse) { content = ((BotanHttpResponse) obj).content(); type = ((BotanHttpResponse) obj).type(); responseStatus = HttpResponseStatus.OK; } else if (obj instanceof Integer) { responseStatus = HttpResponseStatus.valueOf((int) obj); return new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, responseStatus); } else { content = obj.toString(); type = res.type(); responseStatus = HttpResponseStatus.OK; } final FullHttpResponse response = new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, responseStatus, Unpooled.copiedBuffer(content, CharsetUtil.UTF_8)); response.headers().set(HttpHeaderNames.CONTENT_TYPE, type); response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes()); return response; } else { return new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND); } }
public static void main(String[] args) throws Exception { // This is an example router, it will be used at HttpRouterServerHandler. // // For simplicity of this example, route targets are just simple strings. // But you can make them classes, and at HttpRouterServerHandler once you // get a target class, you can create an instance of it and dispatch the // request to the instance etc. Router<String> router = new Router<String>() .GET("/", "Index page") .GET("/articles/:id", "Article show page") .notFound("404 Not Found"); System.out.println(router); NioEventLoopGroup bossGroup = new NioEventLoopGroup(1); NioEventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .childOption(ChannelOption.TCP_NODELAY, java.lang.Boolean.TRUE) .childOption(ChannelOption.SO_KEEPALIVE, java.lang.Boolean.TRUE) .channel(NioServerSocketChannel.class) .childHandler(new HttpRouterServerInitializer(router)); Channel ch = b.bind(PORT).sync().channel(); System.out.println("Server started: http://127.0.0.1:" + PORT + '/'); ch.closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
HttpRouterServerHandler(Router<Route> router) { this.router = router; }
HttpRouterServerInitializer(final Router<Route> router) { handler = new HttpRouterServerHandler(router); }
HttpRouterServerHandler(Router<String> router) { this.router = router; }
HttpRouterServerInitializer(Router<String> router) { handler = new HttpRouterServerHandler(router); }