use std::convert::Infallible;
use std::fmt;
use std::future::Future;
use std::net::IpAddr;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use bytes::Bytes;
use http::request::Parts;
use http::{Request, Response};
use http_body_util::{BodyExt, Limited};
use serde::Serialize;
use serde_json::Value;
use tower_service::Service;
use fetchlet::AppHandle;
use super::{BoxError, Error, PublicOrigin, RequestContext};
type ContextFactory = Arc<dyn Fn(&Parts) -> Result<Value, Error> + Send + Sync>;
type AddressFactory = Arc<dyn Fn(&Parts) -> Option<IpAddr> + Send + Sync>;
type TrustedContextFactory = Arc<dyn Fn(&Parts) -> fetchlet::Context + Send + Sync>;
type ErrorCallback = Arc<dyn Fn(&Error) + Send + Sync>;
#[derive(Clone)]
pub struct FetchletService {
app: AppHandle,
origin: PublicOrigin,
context: Option<ContextFactory>,
client_address: Option<AddressFactory>,
trusted_context: Option<TrustedContextFactory>,
on_error: Option<ErrorCallback>,
stream_request_bodies: bool,
}
impl FetchletService {
pub fn new(app: AppHandle, public_origin: &str) -> Result<Self, Error> {
Ok(Self {
app,
origin: PublicOrigin::parse(public_origin)?,
context: None,
client_address: None,
trusted_context: None,
on_error: None,
stream_request_bodies: false,
})
}
#[cfg(feature = "axum-websocket")]
pub(crate) fn app(&self) -> &AppHandle {
&self.app
}
pub fn client_address(
mut self,
address: impl Fn(&Parts) -> Option<IpAddr> + Send + Sync + 'static,
) -> Self {
self.client_address = Some(Arc::new(address));
self
}
pub fn context<C, F>(mut self, context: F) -> Self
where
C: Serialize,
F: Fn(&Parts) -> C + Send + Sync + 'static,
{
self.context = Some(Arc::new(move |parts| {
serde_json::to_value(context(parts)).map_err(Error::Context)
}));
self
}
pub fn trusted_context(
mut self,
context: impl Fn(&Parts) -> fetchlet::Context + Send + Sync + 'static,
) -> Self {
self.trusted_context = Some(Arc::new(context));
self
}
pub fn on_error(mut self, callback: impl Fn(&Error) + Send + Sync + 'static) -> Self {
self.on_error = Some(Arc::new(callback));
self
}
pub fn stream_request_bodies(mut self) -> Self {
self.stream_request_bodies = true;
self
}
pub async fn fetch<B>(&self, request: Request<B>) -> Result<Response<fetchlet::Body>, Error>
where
B: http_body::Body<Data = Bytes> + Send + 'static,
B::Error: Into<BoxError> + fmt::Display,
{
let (mut parts, body) = request.into_parts();
parts.uri = self.origin.apply(parts.uri)?;
let context = self.context_value(&parts)?;
if let Some(trusted) = &self.trusted_context {
let trusted = trusted(&parts);
parts.extensions.insert(trusted);
}
let body = if self.stream_request_bodies {
fetchlet::Body::from_http_body(body)
} else {
let limit = self.app.limits().request_body_bytes.unwrap_or(usize::MAX);
let collected = Limited::new(body, limit)
.collect()
.await
.map_err(Error::Body)?;
fetchlet::Body::from(collected.to_bytes())
};
let request = Request::from_parts(parts, body);
Ok(self.app.fetch_with_context(request, &context).await?)
}
fn context_value(&self, parts: &Parts) -> Result<Value, Error> {
if let Some(context) = &self.context {
return context(parts);
}
let address = self
.client_address
.as_ref()
.and_then(|address| address(parts));
serde_json::to_value(RequestContext::new(address)).map_err(Error::Context)
}
pub fn error_response(&self, error: &Error) -> Response<fetchlet::Body> {
if let Some(callback) = &self.on_error {
callback(error);
}
error.response()
}
}
impl<B> Service<Request<B>> for FetchletService
where
B: http_body::Body<Data = Bytes> + Send + 'static,
B::Error: Into<BoxError> + fmt::Display,
{
type Response = Response<fetchlet::Body>;
type Error = Infallible;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Infallible>> + Send>>;
fn poll_ready(&mut self, _context: &mut Context<'_>) -> Poll<Result<(), Infallible>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, request: Request<B>) -> Self::Future {
let service = self.clone();
Box::pin(async move {
Ok(match service.fetch(request).await {
Ok(response) => response,
Err(error) => service.error_response(&error),
})
})
}
}