-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(service): add adapter for tower services
- Loading branch information
1 parent
11776bd
commit 9e63363
Showing
4 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use bytes::Bytes; | ||
use http::{Request, Response}; | ||
use http_body_util::Full; | ||
use hyper::body::Incoming; | ||
use hyper_util::{rt::TokioExecutor, server::conn::auto::Builder}; | ||
use tokio::net::TcpListener; | ||
use tower::BoxError; | ||
|
||
async fn handle(_request: Request<Incoming>) -> Result<Response<Full<Bytes>>, BoxError> { | ||
Ok(Response::new(Full::from("Hello, World!"))) | ||
} | ||
|
||
#[tokio::main(flavor = "current_thread")] | ||
async fn main() { | ||
let tower_service = tower::service_fn(handle); | ||
|
||
let listener = TcpListener::bind("0.0.0.0:3000").await.unwrap(); | ||
loop { | ||
let (tcp_stream, _remote_addr) = listener.accept().await.unwrap(); | ||
if let Err(err) = Builder::new(TokioExecutor::new()) | ||
.serve_connection_with_upgrades_tower(tcp_stream, tower_service) | ||
.await | ||
{ | ||
eprintln!("failed to serve connection: {err:#}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,5 +8,6 @@ pub mod client; | |
mod common; | ||
pub mod rt; | ||
pub mod server; | ||
pub mod service; | ||
|
||
mod error; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//! Service utilities. | ||
|
||
use pin_project_lite::pin_project; | ||
use std::{ | ||
future::Future, | ||
pin::Pin, | ||
task::{Context, Poll}, | ||
}; | ||
use tower::{util::Oneshot, ServiceExt}; | ||
|
||
/// A tower service converted into a hyper service. | ||
#[derive(Debug, Copy, Clone)] | ||
pub struct TowerToHyperService<S> { | ||
service: S, | ||
} | ||
|
||
impl<S> TowerToHyperService<S> { | ||
/// Create a new `TowerToHyperService` from a tower service. | ||
pub fn new(tower_service: S) -> Self { | ||
Self { | ||
service: tower_service, | ||
} | ||
} | ||
} | ||
|
||
impl<S, R> hyper::service::Service<R> for TowerToHyperService<S> | ||
where | ||
S: tower::Service<R> + Clone, | ||
{ | ||
type Response = S::Response; | ||
type Error = S::Error; | ||
type Future = TowerToHyperServiceFuture<S, R>; | ||
|
||
fn call(&self, req: R) -> Self::Future { | ||
TowerToHyperServiceFuture { | ||
future: self.service.clone().oneshot(req), | ||
} | ||
} | ||
} | ||
|
||
pin_project! { | ||
/// Response future for [`TowerToHyperService`]. | ||
pub struct TowerToHyperServiceFuture<S, R> | ||
where | ||
S: tower::Service<R>, | ||
{ | ||
#[pin] | ||
future: Oneshot<S, R>, | ||
} | ||
} | ||
|
||
impl<S, R> Future for TowerToHyperServiceFuture<S, R> | ||
where | ||
S: tower::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) | ||
} | ||
} |