From 23e4a1ee3fd0853194f6ce542a19505fdab2686d Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Wed, 17 Jul 2019 12:12:22 +0200 Subject: [PATCH] update api server to hyper git master This is now an example requiring no future-compat anymore. Signed-off-by: Wolfgang Bumiller --- api-test/Cargo.toml | 6 ++---- api-test/src/api.rs | 12 +++-------- api-test/src/main.rs | 47 ++++++++++++++++++++++---------------------- 3 files changed, 29 insertions(+), 36 deletions(-) diff --git a/api-test/Cargo.toml b/api-test/Cargo.toml index cec46bf5..a328638c 100644 --- a/api-test/Cargo.toml +++ b/api-test/Cargo.toml @@ -11,10 +11,8 @@ authors = [ bytes = "0.4" endian_trait = { version = "0.6", features = [ "arrays" ] } failure = "0.1" -futures-01 = { package = "futures", version = "0.1" } -futures-preview = { version = "0.3.0-alpha.16", features = [ "compat", "io-compat" ] } http = "0.1" -hyper = "0.12" +hyper = { version = "0.13.0-a.0", git = "https://github.com/hyperium/hyper" } proxmox = { path = "../proxmox" } serde_json = "1.0" -tokio = "0.1" +tokio = { version = "0.2", git = "https://github.com/tokio-rs/tokio" } diff --git a/api-test/src/api.rs b/api-test/src/api.rs index 74de0772..5682920c 100644 --- a/api-test/src/api.rs +++ b/api-test/src/api.rs @@ -2,9 +2,7 @@ use std::io; use std::path::Path; use failure::{bail, Error}; -use futures::compat::AsyncRead01CompatExt; -use futures::compat::Future01CompatExt; -use futures::io::AsyncReadExt; +use tokio::io::AsyncReadExt; use http::Response; use hyper::Body; @@ -50,10 +48,7 @@ async fn get_www(path: String) -> Result, Error> { bail!("illegal path"); } - let mut file = match tokio::fs::File::open(format!("{}/{}", www_dir(), path)) - .compat() - .await - { + let mut file = match tokio::fs::File::open(format!("{}/{}", www_dir(), path)).await { Ok(file) => file, Err(ref err) if err.kind() == io::ErrorKind::NotFound => { return Ok(http::Response::builder() @@ -61,8 +56,7 @@ async fn get_www(path: String) -> Result, Error> { .body(Body::from(format!("No such file or directory: {}", path)))?); } Err(e) => return Err(e.into()), - } - .compat(); + }; let mut data = Vec::new(); file.read_to_end(&mut data).await?; diff --git a/api-test/src/main.rs b/api-test/src/main.rs index f75a6586..a7476ed8 100644 --- a/api-test/src/main.rs +++ b/api-test/src/main.rs @@ -3,7 +3,7 @@ use failure::{format_err, Error}; use http::Request; use http::Response; -use hyper::service::service_fn; +use hyper::service::{make_service_fn, service_fn}; use hyper::{Body, Server}; use serde_json::Value; @@ -34,7 +34,27 @@ async fn route_request(request: Request) -> Result, E .await } -type BoxFut = Box, Error = hyper::Error> + Send>; +async fn main_do(www_dir: String) { + // Construct our SocketAddr to listen on... + let addr = ([0, 0, 0, 0], 3000).into(); + + // And a MakeService to handle each connection... + let service = make_service_fn(|_| { + async { + Ok::<_, hyper::Error>(service_fn(run_request)) + } + }); + + // Then bind and serve... + let server = Server::bind(&addr) + .serve(service); + + println!("Serving {} under http://localhost:3000/www/", www_dir); + + if let Err(e) = server.await { + eprintln!("server error: {}", e); + } +} fn main() { // We expect a path, where to find our files we expose via the www/ dir: @@ -51,25 +71,6 @@ fn main() { serde_json::to_string_pretty(&api::ROUTER.api_dump()).unwrap() ); - // Construct our SocketAddr to listen on... - let addr = ([0, 0, 0, 0], 3000).into(); - - // And a MakeService to handle each connection... - let make_service = || { - service_fn(|req| { - let fut: BoxFut = Box::new(futures::compat::Compat::new(Box::pin(run_request(req)))); - fut - }) - }; - - // Then bind and serve... - let server = { - use futures_01::Future; - Server::bind(&addr) - .serve(make_service) - .map_err(|err| eprintln!("server error: {}", err)) - }; - - println!("Serving {} under http://localhost:3000/www/", www_dir); - tokio::run(server); + let rt = tokio::runtime::Runtime::new().unwrap(); + rt.block_on(main_do(www_dir)); }