1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
//! Shuttle service integration for the Tower framework.
//!
//! ## Example
//!
//! ```rust,no_run
//! use std::convert::Infallible;
//! use std::future::Future;
//! use std::pin::Pin;
//! use std::task::{Context, Poll};
//!
//! #[derive(Clone)]
//! struct HelloWorld;
//!
//! impl tower::Service<hyper::Request<hyper::Body>> for HelloWorld {
//! type Response = hyper::Response<hyper::Body>;
//! type Error = Infallible;
//! type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + Sync>>;
//!
//! fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
//! Poll::Ready(Ok(()))
//! }
//!
//! fn call(&mut self, _req: hyper::Request<hyper::Body>) -> Self::Future {
//! let body = hyper::Body::from("Hello, world!");
//! let resp = hyper::Response::builder()
//! .status(200)
//! .body(body)
//! .expect("Unable to create the `hyper::Response` object");
//!
//! let fut = async { Ok(resp) };
//!
//! Box::pin(fut)
//! }
//! }
//!
//! #[shuttle_runtime::main]
//! async fn tower() -> shuttle_tower::ShuttleTower<HelloWorld> {
//! let service = HelloWorld;
//!
//! Ok(service.into())
//! }
//! ```
use shuttle_runtime::{CustomError, Error};
use std::net::SocketAddr;
/// A wrapper type for [tower::Service] so we can implement [shuttle_runtime::Service] for it.
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,
{
/// Takes the service that is returned by the user in their [shuttle_runtime::main] function
/// and binds to an address passed in by shuttle.
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)
}
}
/// Shuttle service return type for the Tower framework.
///
/// ## Example
///
/// ```rust,no_run
/// # use std::convert::Infallible;
/// # use std::future::Future;
/// # use std::pin::Pin;
/// # use std::task::{Context, Poll};
///
/// #[derive(Clone)]
/// struct HelloWorld;
///
/// impl tower::Service<hyper::Request<hyper::Body>> for HelloWorld {
/// type Response = hyper::Response<hyper::Body>;
/// type Error = Infallible;
/// type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + Sync>>;
///
/// fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
/// Poll::Ready(Ok(()))
/// }
///
/// fn call(&mut self, _req: hyper::Request<hyper::Body>) -> Self::Future {
/// let body = hyper::Body::from("Hello, world!");
/// let resp = hyper::Response::builder()
/// .status(200)
/// .body(body)
/// .expect("Unable to create the `hyper::Response` object");
/// let fut = async { Ok(resp) };
/// Box::pin(fut)
/// }
/// }
///
/// #[shuttle_runtime::main]
/// async fn tower() -> shuttle_tower::ShuttleTower<HelloWorld> {
/// let service = HelloWorld;
///
/// Ok(service.into())
/// }
/// ```
pub type ShuttleTower<T> = Result<TowerService<T>, Error>;