diff options
author | Quentin <quentin@deuxfleurs.fr> | 2020-11-11 21:17:34 +0100 |
---|---|---|
committer | Quentin <quentin@deuxfleurs.fr> | 2020-11-11 21:17:34 +0100 |
commit | 6076d869b14aa38059d54a2dece222ad7b9da3bc (patch) | |
tree | 87a53aaf5e6d46c13e3bbe1aa330552659093eee /src/web/web_server.rs | |
parent | 2765291796de1b94401e462dc5136fdfce867596 (diff) | |
download | garage-6076d869b14aa38059d54a2dece222ad7b9da3bc.tar.gz garage-6076d869b14aa38059d54a2dece222ad7b9da3bc.zip |
Build error
Diffstat (limited to 'src/web/web_server.rs')
-rw-r--r-- | src/web/web_server.rs | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/src/web/web_server.rs b/src/web/web_server.rs index 16b27cef..3cc0fa43 100644 --- a/src/web/web_server.rs +++ b/src/web/web_server.rs @@ -1,4 +1,5 @@ use std::borrow::Cow; +use std::convert::Infallible; use std::net::SocketAddr; use std::sync::Arc; @@ -24,7 +25,7 @@ pub async fn run_web_server( async move { Ok::<_, Error>(service_fn(move |req: Request<Body>| { let garage = garage.clone(); - handler(garage, req, client_addr) + handle_request(garage, req, client_addr) })) } }); @@ -37,11 +38,29 @@ pub async fn run_web_server( Ok(()) } -async fn handler( +async fn handle_request( garage: Arc<Garage>, req: Request<Body>, addr: SocketAddr, -) -> Result<Response<Body>, Error> { +) -> Result<Response<Body>, Infallible> { + info!("{} {} {}", addr, req.method(), req.uri()); + let res = serve_file(garage, req).await; + match &res { + Ok(r) => debug!("{} {:?}", r.status(), r.headers()), + Err(e) => warn!("Response: error {}, {}", e.http_status_code(), e), + } + + Ok(res.unwrap_or_else(error_to_res)) +} + +fn error_to_res(e: Error) -> Response<Body> { + let body: Body = Body::from(format!("{}\n", e)); + let mut http_error = Response::new(body); + *http_error.status_mut() = e.http_status_code(); + http_error +} + +async fn serve_file(garage: Arc<Garage>, req: Request<Body>) -> Result<Response<Body>, Error> { // Get http authority string (eg. [::1]:3902 or garage.tld:80) let authority = req .headers() |