mirror of
https://github.com/tokio-rs/axum.git
synced 2024-12-29 15:49:16 +01:00
Replace own TowerToHyperService with upstream version (#2692)
This commit is contained in:
parent
34e11c50b5
commit
89202ac379
2 changed files with 9 additions and 57 deletions
|
@ -65,7 +65,7 @@ tower-service = "0.3"
|
||||||
axum-macros = { path = "../axum-macros", version = "0.4.1", optional = true }
|
axum-macros = { path = "../axum-macros", version = "0.4.1", optional = true }
|
||||||
base64 = { version = "0.21.0", optional = true }
|
base64 = { version = "0.21.0", optional = true }
|
||||||
hyper = { version = "1.1.0", optional = true }
|
hyper = { version = "1.1.0", optional = true }
|
||||||
hyper-util = { version = "0.1.3", features = ["tokio", "server"], optional = true }
|
hyper-util = { version = "0.1.3", features = ["tokio", "server", "service"], optional = true }
|
||||||
multer = { version = "3.0.0", optional = true }
|
multer = { version = "3.0.0", optional = true }
|
||||||
serde_json = { version = "1.0", features = ["raw_value"], optional = true }
|
serde_json = { version = "1.0", features = ["raw_value"], optional = true }
|
||||||
serde_path_to_error = { version = "0.1.8", optional = true }
|
serde_path_to_error = { version = "0.1.8", optional = true }
|
||||||
|
|
|
@ -7,9 +7,7 @@ use std::{
|
||||||
io,
|
io,
|
||||||
marker::PhantomData,
|
marker::PhantomData,
|
||||||
net::SocketAddr,
|
net::SocketAddr,
|
||||||
pin::Pin,
|
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
task::{Context, Poll},
|
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -18,13 +16,12 @@ use futures_util::{pin_mut, FutureExt};
|
||||||
use hyper::body::Incoming;
|
use hyper::body::Incoming;
|
||||||
use hyper_util::rt::{TokioExecutor, TokioIo};
|
use hyper_util::rt::{TokioExecutor, TokioIo};
|
||||||
#[cfg(any(feature = "http1", feature = "http2"))]
|
#[cfg(any(feature = "http1", feature = "http2"))]
|
||||||
use hyper_util::server::conn::auto::Builder;
|
use hyper_util::{server::conn::auto::Builder, service::TowerToHyperService};
|
||||||
use pin_project_lite::pin_project;
|
|
||||||
use tokio::{
|
use tokio::{
|
||||||
net::{TcpListener, TcpStream},
|
net::{TcpListener, TcpStream},
|
||||||
sync::watch,
|
sync::watch,
|
||||||
};
|
};
|
||||||
use tower::util::{Oneshot, ServiceExt};
|
use tower::ServiceExt as _;
|
||||||
use tower_service::Service;
|
use tower_service::Service;
|
||||||
|
|
||||||
/// Serve the service with the supplied listener.
|
/// Serve the service with the supplied listener.
|
||||||
|
@ -243,11 +240,10 @@ where
|
||||||
remote_addr,
|
remote_addr,
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.unwrap_or_else(|err| match err {});
|
.unwrap_or_else(|err| match err {})
|
||||||
|
.map_request(|req: Request<Incoming>| req.map(Body::new));
|
||||||
|
|
||||||
let hyper_service = TowerToHyperService {
|
let hyper_service = TowerToHyperService::new(tower_service);
|
||||||
service: tower_service,
|
|
||||||
};
|
|
||||||
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
match Builder::new(TokioExecutor::new())
|
match Builder::new(TokioExecutor::new())
|
||||||
|
@ -404,11 +400,10 @@ where
|
||||||
remote_addr,
|
remote_addr,
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.unwrap_or_else(|err| match err {});
|
.unwrap_or_else(|err| match err {})
|
||||||
|
.map_request(|req: Request<Incoming>| req.map(Body::new));
|
||||||
|
|
||||||
let hyper_service = TowerToHyperService {
|
let hyper_service = TowerToHyperService::new(tower_service);
|
||||||
service: tower_service,
|
|
||||||
};
|
|
||||||
|
|
||||||
let signal_tx = Arc::clone(&signal_tx);
|
let signal_tx = Arc::clone(&signal_tx);
|
||||||
|
|
||||||
|
@ -518,49 +513,6 @@ mod private {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone)]
|
|
||||||
struct TowerToHyperService<S> {
|
|
||||||
service: S,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S> hyper::service::Service<Request<Incoming>> for TowerToHyperService<S>
|
|
||||||
where
|
|
||||||
S: tower_service::Service<Request> + Clone,
|
|
||||||
{
|
|
||||||
type Response = S::Response;
|
|
||||||
type Error = S::Error;
|
|
||||||
type Future = TowerToHyperServiceFuture<S, Request>;
|
|
||||||
|
|
||||||
fn call(&self, req: Request<Incoming>) -> Self::Future {
|
|
||||||
let req = req.map(Body::new);
|
|
||||||
TowerToHyperServiceFuture {
|
|
||||||
future: self.service.clone().oneshot(req),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pin_project! {
|
|
||||||
struct TowerToHyperServiceFuture<S, R>
|
|
||||||
where
|
|
||||||
S: tower_service::Service<R>,
|
|
||||||
{
|
|
||||||
#[pin]
|
|
||||||
future: Oneshot<S, R>,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S, R> Future for TowerToHyperServiceFuture<S, R>
|
|
||||||
where
|
|
||||||
S: tower_service::Service<R>,
|
|
||||||
{
|
|
||||||
type Output = Result<S::Response, S::Error>;
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
||||||
self.project().future.poll(cx)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// An incoming stream.
|
/// An incoming stream.
|
||||||
///
|
///
|
||||||
/// Used with [`serve`] and [`IntoMakeServiceWithConnectInfo`].
|
/// Used with [`serve`] and [`IntoMakeServiceWithConnectInfo`].
|
||||||
|
|
Loading…
Reference in a new issue