-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from quasiyoke/feature/http-crate-1.0
Upgrade http crate to 1.0, fix examples
- Loading branch information
Showing
10 changed files
with
226 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,53 @@ | ||
use std::{convert::Infallible, net::SocketAddr}; | ||
|
||
use hyper::{server::conn::http1, service::service_fn}; | ||
use hyper_util::rt::TokioIo; | ||
use tokio::net::TcpListener; | ||
|
||
use dav_server::{fakels::FakeLs, localfs::LocalFs, DavHandler}; | ||
use std::convert::Infallible; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
env_logger::init(); | ||
let dir = "/tmp"; | ||
let addr = ([127, 0, 0, 1], 4918).into(); | ||
let addr: SocketAddr = ([127, 0, 0, 1], 4918).into(); | ||
|
||
let dav_server = DavHandler::builder() | ||
.filesystem(LocalFs::new(dir, false, false, false)) | ||
.locksystem(FakeLs::new()) | ||
.build_handler(); | ||
|
||
let make_service = hyper::service::make_service_fn(move |_| { | ||
let listener = TcpListener::bind(addr).await.unwrap(); | ||
|
||
println!("Listening {addr}"); | ||
|
||
// We start a loop to continuously accept incoming connections | ||
loop { | ||
let (stream, _) = listener.accept().await.unwrap(); | ||
let dav_server = dav_server.clone(); | ||
async move { | ||
let func = move |req| { | ||
let dav_server = dav_server.clone(); | ||
async move { Ok::<_, Infallible>(dav_server.handle(req).await) } | ||
}; | ||
Ok::<_, Infallible>(hyper::service::service_fn(func)) | ||
} | ||
}); | ||
|
||
println!("hyper example: listening on {:?} serving {}", addr, dir); | ||
let _ = hyper::Server::bind(&addr) | ||
.serve(make_service) | ||
.await | ||
.map_err(|e| eprintln!("server error: {}", e)); | ||
|
||
// Use an adapter to access something implementing `tokio::io` traits as if they implement | ||
// `hyper::rt` IO traits. | ||
let io = TokioIo::new(stream); | ||
|
||
// Spawn a tokio task to serve multiple connections concurrently | ||
tokio::task::spawn(async move { | ||
// Finally, we bind the incoming connection to our `hello` service | ||
if let Err(err) = http1::Builder::new() | ||
// `service_fn` converts our function in a `Service` | ||
.serve_connection( | ||
io, | ||
service_fn({ | ||
move |req| { | ||
let dav_server = dav_server.clone(); | ||
async move { Ok::<_, Infallible>(dav_server.handle(req).await) } | ||
} | ||
}), | ||
) | ||
.await | ||
{ | ||
eprintln!("Failed serving: {err:?}"); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.