Add RoutingDsl::{serve, into_make_service} (#8)

This commit is contained in:
David Pedersen
2021-06-12 21:44:40 +02:00
committed by GitHub
parent c9c507aece
commit b3bc4e024c
10 changed files with 118 additions and 49 deletions
+5 -5
View File
@@ -25,7 +25,7 @@
//!
//! let app = route("/users", post(create_user));
//! # async {
//! # hyper::Server::bind(&"".parse().unwrap()).serve(tower::make::Shared::new(app)).await;
//! # app.serve(&"".parse().unwrap()).await.unwrap();
//! # };
//! ```
//!
@@ -60,7 +60,7 @@
//!
//! let app = route("/foo", get(handler));
//! # async {
//! # hyper::Server::bind(&"".parse().unwrap()).serve(tower::make::Shared::new(app)).await;
//! # app.serve(&"".parse().unwrap()).await.unwrap();
//! # };
//! ```
//!
@@ -85,7 +85,7 @@
//!
//! let app = route("/foo", get(handler));
//! # async {
//! # hyper::Server::bind(&"".parse().unwrap()).serve(tower::make::Shared::new(app)).await;
//! # app.serve(&"".parse().unwrap()).await.unwrap();
//! # };
//! ```
//!
@@ -107,7 +107,7 @@
//!
//! let app = route("/users", post(create_user));
//! # async {
//! # hyper::Server::bind(&"".parse().unwrap()).serve(tower::make::Shared::new(app)).await;
//! # app.serve(&"".parse().unwrap()).await.unwrap();
//! # };
//! ```
//!
@@ -126,7 +126,7 @@
//!
//! let app = route("/users", post(create_user));
//! # async {
//! # hyper::Server::bind(&"".parse().unwrap()).serve(tower::make::Shared::new(app)).await;
//! # app.serve(&"".parse().unwrap()).await.unwrap();
//! # };
//! ```
+29 -18
View File
@@ -33,9 +33,8 @@
//! let app = route("/", get(|| async { "Hello, World!" }));
//!
//! // run it with hyper on localhost:3000
//! let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
//! Server::bind(&addr)
//! .serve(Shared::new(app))
//! app
//! .serve(&"0.0.0.0:3000".parse().unwrap())
//! .await
//! .unwrap();
//! }
@@ -63,7 +62,7 @@
//! // `GET /foo` called
//! }
//! # async {
//! # hyper::Server::bind(&"".parse().unwrap()).serve(tower::make::Shared::new(app)).await;
//! # app.serve(&"".parse().unwrap()).await.unwrap();
//! # };
//! ```
//!
@@ -142,7 +141,7 @@
//! .route("/result", get(result))
//! .route("/response", get(response));
//! # async {
//! # hyper::Server::bind(&"".parse().unwrap()).serve(tower::make::Shared::new(app)).await;
//! # app.serve(&"".parse().unwrap()).await.unwrap();
//! # };
//! ```
//!
@@ -175,7 +174,7 @@
//! // ...
//! }
//! # async {
//! # hyper::Server::bind(&"".parse().unwrap()).serve(tower::make::Shared::new(app)).await;
//! # app.serve(&"".parse().unwrap()).await.unwrap();
//! # };
//! ```
//!
@@ -195,7 +194,7 @@
//! // ...
//! }
//! # async {
//! # hyper::Server::bind(&"".parse().unwrap()).serve(tower::make::Shared::new(app)).await;
//! # app.serve(&"".parse().unwrap()).await.unwrap();
//! # };
//! ```
//!
@@ -233,7 +232,7 @@
//! // ...
//! }
//! # async {
//! # hyper::Server::bind(&"".parse().unwrap()).serve(tower::make::Shared::new(app)).await;
//! # app.serve(&"".parse().unwrap()).await.unwrap();
//! # };
//! ```
//!
@@ -248,7 +247,7 @@
//! // ...
//! }
//! # async {
//! # hyper::Server::bind(&"".parse().unwrap()).serve(tower::make::Shared::new(app)).await;
//! # app.serve(&"".parse().unwrap()).await.unwrap();
//! # };
//! ```
//!
@@ -279,7 +278,7 @@
//!
//! async fn handler() {}
//! # async {
//! # hyper::Server::bind(&"".parse().unwrap()).serve(tower::make::Shared::new(app)).await;
//! # app.serve(&"".parse().unwrap()).await.unwrap();
//! # };
//! ```
//!
@@ -299,7 +298,7 @@
//!
//! async fn post_foo() {}
//! # async {
//! # hyper::Server::bind(&"".parse().unwrap()).serve(tower::make::Shared::new(app)).await;
//! # app.serve(&"".parse().unwrap()).await.unwrap();
//! # };
//! ```
//!
@@ -349,7 +348,7 @@
//!
//! async fn handle() {}
//! # async {
//! # hyper::Server::bind(&"".parse().unwrap()).serve(tower::make::Shared::new(app)).await;
//! # app.serve(&"".parse().unwrap()).await.unwrap();
//! # };
//! ```
//!
@@ -375,7 +374,7 @@
//!
//! async fn other_handle() {}
//! # async {
//! # hyper::Server::bind(&"".parse().unwrap()).serve(tower::make::Shared::new(app)).await;
//! # app.serve(&"".parse().unwrap()).await.unwrap();
//! # };
//! ```
//!
@@ -428,7 +427,7 @@
//! );
//! });
//! # async {
//! # hyper::Server::bind(&"".parse().unwrap()).serve(tower::make::Shared::new(app)).await;
//! # app.serve(&"".parse().unwrap()).await.unwrap();
//! # };
//! ```
//!
@@ -459,7 +458,7 @@
//! // ...
//! }
//! # async {
//! # hyper::Server::bind(&"".parse().unwrap()).serve(tower::make::Shared::new(app)).await;
//! # app.serve(&"".parse().unwrap()).await.unwrap();
//! # };
//! ```
//!
@@ -494,7 +493,7 @@
//! )
//! );
//! # async {
//! # hyper::Server::bind(&"".parse().unwrap()).serve(tower::make::Shared::new(app)).await;
//! # app.serve(&"".parse().unwrap()).await.unwrap();
//! # };
//! ```
//!
@@ -518,7 +517,7 @@
//! let app = route("/", get(|_: Request<Body>| async { /* ... */ }))
//! .nest("/api", api_routes());
//! # async {
//! # hyper::Server::bind(&"".parse().unwrap()).serve(tower::make::Shared::new(app)).await;
//! # app.serve(&"".parse().unwrap()).await.unwrap();
//! # };
//! ```
//!
@@ -538,14 +537,26 @@
//! })
//! );
//! # async {
//! # hyper::Server::bind(&"".parse().unwrap()).serve(tower::make::Shared::new(app)).await;
//! # app.serve(&"".parse().unwrap()).await.unwrap();
//! # };
//! ```
//!
//! # Features
//!
//! tower-web uses a set of [feature flags] to reduce the amount of compiled and
//! optional dependencies.
//!
//! The following optional features are available:
//!
//! - `ws`: Enables WebSockets support.
//! - `hyper-h1`: Enables hyper's `http1` feature. On by default.
//! - `hyper-h2`: Enables hyper's `http2` feature.
//!
//! [tower]: https://crates.io/crates/tower
//! [tower-http]: https://crates.io/crates/tower-http
//! [tokio]: http://crates.io/crates/tokio
//! [hyper]: http://crates.io/crates/hyper
//! [feature flags]: https://doc.rust-lang.org/cargo/reference/features.html#the-features-section
#![doc(html_root_url = "https://docs.rs/tower-http/0.1.0")]
#![warn(
+59 -4
View File
@@ -1,6 +1,7 @@
//! Routing between [`Service`]s.
use crate::{body::BoxBody, response::IntoResponse, ResultExt};
use async_trait::async_trait;
use bytes::Bytes;
use futures_util::{future, ready};
use http::{Method, Request, Response, StatusCode, Uri};
@@ -80,6 +81,7 @@ pub struct Route<S, F> {
/// Trait for building routers.
// TODO(david): this name isn't great
#[async_trait]
pub trait RoutingDsl: crate::sealed::Sealed + Sized {
/// Add another route to the router.
///
@@ -196,7 +198,7 @@ pub trait RoutingDsl: crate::sealed::Sealed + Sized {
/// // wont be sent through `ConcurrencyLimit`
/// .route("/bar", get(third_handler));
/// # async {
/// # hyper::Server::bind(&"".parse().unwrap()).serve(tower::make::Shared::new(app)).await;
/// # app.serve(&"".parse().unwrap()).await.unwrap();
/// # };
/// ```
///
@@ -228,6 +230,59 @@ pub trait RoutingDsl: crate::sealed::Sealed + Sized {
{
Layered(layer.layer(self))
}
/// Convert this router into a [`MakeService`], that is a [`Service`] who's
/// response is another service.
///
/// This is useful when running your application with hyper's
/// [`Server`](hyper::server::Server):
///
/// ```
/// use tower_web::prelude::*;
///
/// let app = route("/", get(|| async { "Hi!" }));
///
/// # async {
/// hyper::Server::bind(&"0.0.0.0:3000".parse().unwrap())
/// .serve(app.into_make_service())
/// .await
/// .expect("server failed");
/// # };
/// ```
///
/// [`MakeService`]: tower::make::MakeService
fn into_make_service(self) -> tower::make::Shared<Self>
where
Self: Clone,
{
tower::make::Shared::new(self)
}
/// Serve this router with [hyper] on the given address.
///
/// Uses [`hyper::server::Server`]'s default configuration. Creating a
/// [`hyper::server::Server`] manually is recommended if different
/// configuration is needed. In that case [`into_make_service`] can be used
/// to easily serve this router.
///
/// [hyper]: http://crates.io/crates/hyper
/// [`into_make_service`]: RoutingDsl::into_make_service
#[cfg(any(feature = "hyper-h1", feature = "hyper-h2"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "hyper-h1", feature = "hyper-h2"))))]
async fn serve<B>(self, addr: &std::net::SocketAddr) -> Result<(), hyper::Error>
where
Self: Service<Request<Body>, Response = Response<B>, Error = Infallible>
+ Clone
+ Send
+ 'static,
Self::Future: Send,
B: http_body::Body<Data = Bytes> + Send + Sync + 'static,
B::Error: Into<BoxError> + Send + Sync + 'static,
{
hyper::server::Server::bind(addr)
.serve(self.into_make_service())
.await
}
}
impl<S, F> RoutingDsl for Route<S, F> {}
@@ -662,7 +717,7 @@ where
///
/// let app = nest("/api", users_api).route("/careers", get(careers));
/// # async {
/// # hyper::Server::bind(&"".parse().unwrap()).serve(tower::make::Shared::new(app)).await;
/// # app.serve(&"".parse().unwrap()).await.unwrap();
/// # };
/// ```
///
@@ -683,7 +738,7 @@ where
///
/// let app = nest("/:version/api", users_api);
/// # async {
/// # hyper::Server::bind(&"".parse().unwrap()).serve(tower::make::Shared::new(app)).await;
/// # app.serve(&"".parse().unwrap()).await.unwrap();
/// # };
/// ```
///
@@ -702,7 +757,7 @@ where
///
/// let app = nest("/public", get(serve_dir_service));
/// # async {
/// # hyper::Server::bind(&"".parse().unwrap()).serve(tower::make::Shared::new(app)).await;
/// # app.serve(&"".parse().unwrap()).await.unwrap();
/// # };
/// ```
///
+2 -2
View File
@@ -20,7 +20,7 @@
//! let app = route("/old", service::get(redirect_service))
//! .route("/new", handler::get(handler));
//! # async {
//! # hyper::Server::bind(&"".parse().unwrap()).serve(tower::make::Shared::new(app)).await;
//! # app.serve(&"".parse().unwrap()).await.unwrap();
//! # };
//! ```
//!
@@ -527,7 +527,7 @@ pub trait ServiceExt<B>: Service<Request<Body>, Response = Response<B>> {
/// );
/// #
/// # async {
/// # hyper::Server::bind(&"".parse().unwrap()).serve(tower::make::Shared::new(app)).await;
/// # app.serve(&"".parse().unwrap()).await.unwrap();
/// # };
/// ```
fn handle_error<F, Res>(self, f: F) -> HandleError<Self, F>