aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock1
-rw-r--r--src/api/api_server.rs29
-rw-r--r--src/api/helpers.rs27
-rw-r--r--src/api/lib.rs1
4 files changed, 37 insertions, 21 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 6382d035..2b516664 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -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 9dc74dac..93bed8bb 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;
@@ -18,6 +19,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>,
@@ -45,29 +47,16 @@ pub async fn run_api_server(
Ok(())
}
-async fn handler(
- garage: Arc<Garage>,
- req: Request<Body>,
- addr: SocketAddr,
-) -> Result<Response<Body>, Error> {
- 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>, Error> {
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..19018151
--- /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;
+
+pub trait InfallibleResult {
+ fn make_infallible(self) -> Result<Response<Body>, Infallible>;
+}
+
+impl InfallibleResult for Result<Response<Body>, Error> {
+ 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 df2fd045..003780c1 100644
--- a/src/api/lib.rs
+++ b/src/api/lib.rs
@@ -11,3 +11,4 @@ pub mod s3_delete;
pub mod s3_get;
pub mod s3_list;
pub mod s3_put;
+pub mod helpers;