mirror of
https://git.proxmox.com/git/proxmox
synced 2025-05-28 08:39:13 +00:00
update api server to hyper git master
This is now an example requiring no future-compat anymore. Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
parent
3f8feff618
commit
23e4a1ee3f
@ -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" }
|
||||
|
@ -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<Response<Body>, 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<Response<Body>, 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?;
|
||||
|
@ -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<Body>) -> Result<http::Response<Body>, E
|
||||
.await
|
||||
}
|
||||
|
||||
type BoxFut = Box<dyn futures_01::Future<Item = Response<Body>, 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));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user