use shuttle_runtime::{CustomError, Error};
use std::net::SocketAddr;
pub struct TowerService<T>(pub T);
#[shuttle_runtime::async_trait]
impl<T> shuttle_runtime::Service for TowerService<T>
where
T: tower::Service<hyper::Request<hyper::Body>, Response = hyper::Response<hyper::Body>>
+ Clone
+ Send
+ Sync
+ 'static,
T::Error: std::error::Error + Send + Sync,
T::Future: std::future::Future + Send + Sync,
{
async fn bind(mut self, addr: SocketAddr) -> Result<(), Error> {
let shared = tower::make::Shared::new(self.0);
hyper::Server::bind(&addr)
.serve(shared)
.await
.map_err(CustomError::new)?;
Ok(())
}
}
impl<T> From<T> for TowerService<T>
where
T: tower::Service<hyper::Request<hyper::Body>, Response = hyper::Response<hyper::Body>>
+ Clone
+ Send
+ Sync
+ 'static,
T::Error: std::error::Error + Send + Sync,
T::Future: std::future::Future + Send + Sync,
{
fn from(service: T) -> Self {
Self(service)
}
}
pub type ShuttleTower<T> = Result<TowerService<T>, Error>;