use std::{
convert::Infallible,
fmt,
future::ready,
marker::PhantomData,
net::SocketAddr,
task::{Context, Poll},
};
use tower::{Layer, Service};
use tower_http::add_extension::{AddExtension, AddExtensionLayer};
use crate::request::connect_info::ConnectInfo;
pub struct IntoMakeServiceWithConnectInfo<S, C> {
inner: S,
_connect_info: PhantomData<fn() -> C>,
}
impl<S, C> IntoMakeServiceWithConnectInfo<S, C> {
pub fn new(svc: S) -> Self {
Self {
inner: svc,
_connect_info: PhantomData,
}
}
}
impl<S, C> fmt::Debug for IntoMakeServiceWithConnectInfo<S, C>
where
S: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("IntoMakeServiceWithConnectInfo")
.field("inner", &self.inner)
.finish()
}
}
impl<S, C> Clone for IntoMakeServiceWithConnectInfo<S, C>
where
S: Clone,
{
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
_connect_info: PhantomData,
}
}
}
pub trait Connected<T>: Clone {
fn connect_info(target: T) -> Self;
}
impl Connected<SocketAddr> for SocketAddr {
fn connect_info(target: SocketAddr) -> Self {
target
}
}
impl<'a, L> Connected<crate::serve::IncomingStream<'a, L>> for SocketAddr
where
L: crate::serve::Listener<Addr = SocketAddr>,
{
fn connect_info(target: crate::serve::IncomingStream<'a, L>) -> Self {
*target.remote_addr()
}
}
impl<S, C, T> Service<T> for IntoMakeServiceWithConnectInfo<S, C>
where
S: Clone,
C: Connected<T>,
{
type Response = AddExtension<S, ConnectInfo<C>>;
type Error = Infallible;
type Future = ResponseFuture<S, C>;
#[inline]
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, target: T) -> Self::Future {
let connect_info = ConnectInfo(C::connect_info(target));
let svc = AddExtensionLayer::new(connect_info).layer(self.inner.clone());
ResponseFuture::new(ready(Ok(svc)))
}
}
opaque_future! {
pub type ResponseFuture<S, C> =
std::future::Ready<Result<AddExtension<S, ConnectInfo<C>>, Infallible>>;
}