s3_signer/
lib.rs

1#[cfg(feature = "server")]
2mod error;
3pub mod multipart_upload;
4pub mod objects;
5#[cfg(feature = "server")]
6mod open_api;
7#[cfg(feature = "server")]
8mod s3_configuration;
9
10#[cfg(feature = "server")]
11pub use server::*;
12
13#[cfg(feature = "server")]
14mod server {
15  pub use crate::{error::Error, open_api::*, s3_configuration::S3Configuration};
16
17  use serde::Serialize;
18  use warp::{
19    hyper::{
20      header::{ACCESS_CONTROL_ALLOW_HEADERS, ACCESS_CONTROL_ALLOW_ORIGIN, CONTENT_TYPE, LOCATION},
21      Body, Response, StatusCode,
22    },
23    Filter, Rejection, Reply,
24  };
25
26  pub fn routes(
27    s3_configuration: &S3Configuration,
28  ) -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone {
29    crate::multipart_upload::routes(s3_configuration).or(crate::objects::routes(s3_configuration))
30  }
31
32  pub fn request_builder() -> warp::http::response::Builder {
33    warp::hyper::Response::builder()
34      .header(ACCESS_CONTROL_ALLOW_HEADERS, "*")
35      .header(ACCESS_CONTROL_ALLOW_ORIGIN, "*")
36  }
37
38  pub(crate) fn to_ok_json_response<T>(body_response: &T) -> Result<Response<Body>, Rejection>
39  where
40    T: Serialize + ?Sized,
41  {
42    let json = serde_json::to_string(body_response)
43      .map_err(|error| warp::reject::custom(Error::JsonError(error)))?;
44
45    request_builder()
46      .header(CONTENT_TYPE, "application/json")
47      .status(StatusCode::OK)
48      .body(json.into())
49      .map_err(|error| warp::reject::custom(Error::HttpError(error)))
50  }
51
52  pub(crate) fn to_redirect_response(url: &str) -> Result<Response<Body>, Rejection> {
53    request_builder()
54      .header(LOCATION, url)
55      .status(StatusCode::FOUND)
56      .body(Body::empty())
57      .map_err(|error| warp::reject::custom(Error::HttpError(error)))
58  }
59}