From 91b1b0bc506bf5169d71d9ff2a378e0a39614b40 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Sun, 6 Jun 2021 20:39:54 +0200 Subject: [PATCH] Update docs with nesting --- README.md | 26 +++++++++++++++++--------- src/lib.rs | 30 +++++++++++++++++++++--------- 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 2594ed0c..787f7a24 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ ergonimics and modularity. ### Goals -- Ease of use. Build web apps in Rust should be as easy as `async fn +- Ease of use. Building web apps in Rust should be as easy as `async fn handle(Request) -> Response`. - Solid foundation. tower-web is built on top of tower and makes it easy to plug in any middleware from the [tower] and [tower-http] ecosystem. @@ -14,13 +14,6 @@ tower middleware can handle the rest. - Macro free core. Macro frameworks have their place but tower-web focuses on providing a core that is macro free. -### Non-goals - -- Runtime independent. tower-web is designed to work with tokio and hyper -and focused on bringing a good to experience to that stack. -- Speed. tower-web is a of course a fast framework, and wont be the -bottleneck in your app, but the goal is not to top the benchmarks. - ## Example The "Hello, World!" of tower-web is: @@ -454,7 +447,22 @@ See the [`service`] module for more details. ## Nesting applications -TODO +Applications can be nested by calling `nest`: + +```rust +use tower_web::{prelude::*, routing::BoxRoute, body::BoxBody}; +use tower_http::services::ServeFile; +use http::Response; +use std::convert::Infallible; +use tower::{service_fn, BoxError}; + +fn api_routes() -> BoxRoute { + route("/users", get(|_: Request| async { /* ... */ })).boxed() +} + +let app = route("/", get(|_: Request| async { /* ... */ })) + .nest("/api", api_routes()); +``` [tower]: https://crates.io/crates/tower [tower-http]: https://crates.io/crates/tower-http diff --git a/src/lib.rs b/src/lib.rs index 3664a01e..0c94d5a6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,7 @@ //! //! ## Goals //! -//! - Ease of use. Build web apps in Rust should be as easy as `async fn +//! - Ease of use. Building web apps in Rust should be as easy as `async fn //! handle(Request) -> Response`. //! - Solid foundation. tower-web is built on top of tower and makes it easy to //! plug in any middleware from the [tower] and [tower-http] ecosystem. @@ -12,13 +12,6 @@ //! - Macro free core. Macro frameworks have their place but tower-web focuses //! on providing a core that is macro free. //! -//! ## Non-goals -//! -//! - Runtime independent. tower-web is designed to work with tokio and hyper -//! and focused on bringing a good to experience to that stack. -//! - Speed. tower-web is a of course a fast framework, and wont be the -//! bottleneck in your app, but the goal is not to top the benchmarks. -//! //! # Example //! //! The "Hello, World!" of tower-web is: @@ -500,7 +493,26 @@ //! //! # Nesting applications //! -//! TODO +//! Applications can be nested by calling `nest`: +//! +//! ```rust,no_run +//! use tower_web::{prelude::*, routing::BoxRoute, body::BoxBody}; +//! use tower_http::services::ServeFile; +//! use http::Response; +//! use std::convert::Infallible; +//! use tower::{service_fn, BoxError}; +//! +//! fn api_routes() -> BoxRoute { +//! route("/users", get(|_: Request| async { /* ... */ })).boxed() +//! } +//! +//! let app = route("/", get(|_: Request| async { /* ... */ })) +//! .nest("/api", api_routes()); +//! # +//! # async { +//! # hyper::Server::bind(&"".parse().unwrap()).serve(tower::make::Shared::new(app)).await; +//! # }; +//! ``` //! //! [tower]: https://crates.io/crates/tower //! [tower-http]: https://crates.io/crates/tower-http