diff options
-rw-r--r-- | Cargo.lock | 1 | ||||
-rw-r--r-- | src/api/api_server.rs | 29 | ||||
-rw-r--r-- | src/api/helpers.rs | 27 | ||||
-rw-r--r-- | src/api/lib.rs | 1 |
4 files changed, 37 insertions, 21 deletions
@@ -429,7 +429,6 @@ dependencies = [ "bytes 0.4.12", "chrono", "crypto-mac", - "err-derive", "futures", "futures-util", "garage_model 0.1.1", diff --git a/src/api/api_server.rs b/src/api/api_server.rs index ec02572d..cdf8fa78 100644 --- a/src/api/api_server.rs +++ b/src/api/api_server.rs @@ -1,6 +1,7 @@ use std::collections::HashMap; use std::net::SocketAddr; use std::sync::Arc; +use std::convert::Infallible; use futures::future::Future; use hyper::server::conn::AddrStream; @@ -19,6 +20,7 @@ use crate::s3_delete::*; use crate::s3_get::*; use crate::s3_list::*; use crate::s3_put::*; +use crate::helpers::*; pub async fn run_api_server( garage: Arc<Garage>, @@ -46,29 +48,16 @@ pub async fn run_api_server( Ok(()) } -async fn handler( - garage: Arc<Garage>, - req: Request<Body>, - addr: SocketAddr, -) -> Result<Response<Body>, GarageError> { - info!("{} {} {}", addr, req.method(), req.uri()); +async fn handler(garage: Arc<Garage>, req: Request<Body>, client_addr: SocketAddr) -> Result<Response<Body>, Infallible> { + info!("{} {} {}", client_addr, req.method(), req.uri()); debug!("{:?}", req); - match handler_inner(garage, req).await { - Ok(x) => { - debug!("{} {:?}", x.status(), x.headers()); - Ok(x) - } - Err(e) => { - let body: Body = Body::from(format!("{}\n", e)); - let mut http_error = Response::new(body); - *http_error.status_mut() = e.http_status_code(); - warn!("Response: error {}, {}", e.http_status_code(), e); - Ok(http_error) - } - } + + controller(garage, req) + .await + .make_infallible() } -async fn handler_inner(garage: Arc<Garage>, req: Request<Body>) -> Result<Response<Body>, Error> { +async fn controller(garage: Arc<Garage>, req: Request<Body>) -> Result<Response<Body>, GarageError> { let path = req.uri().path().to_string(); let path = percent_encoding::percent_decode_str(&path).decode_utf8()?; diff --git a/src/api/helpers.rs b/src/api/helpers.rs new file mode 100644 index 00000000..f4d91788 --- /dev/null +++ b/src/api/helpers.rs @@ -0,0 +1,27 @@ +use std::convert::Infallible; +use std::net::SocketAddr; +use hyper::{Body, Response}; + +use garage_util::error::Error as GarageError; + +pub trait InfallibleResult { + fn make_infallible(self) -> Result<Response<Body>, Infallible>; +} + +impl InfallibleResult for Result<Response<Body>, GarageError> { + fn make_infallible(self) -> Result<Response<Body>, Infallible> { + match self { + Ok(x) => { + debug!("{} {:?}", x.status(), x.headers()); + Ok(x) + }, + Err(e) => { + let body: Body = Body::from(format!("{}\n", e)); + let mut http_error = Response::new(body); + *http_error.status_mut() = e.http_status_code(); + warn!("Response: error {}, {}", e.http_status_code(), e); + Ok(http_error) + } + } + } +} diff --git a/src/api/lib.rs b/src/api/lib.rs index 9bb07925..7327354f 100644 --- a/src/api/lib.rs +++ b/src/api/lib.rs @@ -13,3 +13,4 @@ pub mod s3_delete; pub mod s3_get; pub mod s3_list; pub mod s3_put; +pub mod helpers; |