More docs

This commit is contained in:
David Pedersen
2021-06-08 22:04:28 +02:00
parent 1f8b39f05d
commit 1cf78fa807
6 changed files with 392 additions and 58 deletions
+30 -20
View File
@@ -12,6 +12,11 @@
//! - Macro free core. Macro frameworks have their place but tower-web focuses
//! on providing a core that is macro free.
//!
//! # Compatibility
//!
//! tower-web is designed to work with [tokio] and [hyper]. Runtime and
//! transport layer independence is not a goal, at least for the time being.
//!
//! # Example
//!
//! The "Hello, World!" of tower-web is:
@@ -31,8 +36,10 @@
//!
//! // run it with hyper on localhost:3000
//! let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
//! let server = Server::bind(&addr).serve(Shared::new(app));
//! server.await.unwrap();
//! Server::bind(&addr)
//! .serve(Shared::new(app))
//! .await
//! .unwrap();
//! }
//! ```
//!
@@ -109,7 +116,7 @@
//! Html("<h1>Hello, World!</h1>")
//! }
//!
//! // `Json` gives a content-type of `application/json` and works with my type
//! // `Json` gives a content-type of `application/json` and works with any type
//! // that implements `serde::Serialize`
//! async fn json(req: Request<Body>) -> Json<Value> {
//! Json(json!({ "data": 42 }))
@@ -150,8 +157,8 @@
//! implements [`FromRequest`](crate::extract::FromRequest) can be used as an
//! extractor.
//!
//! [`extract::Json`] is an extractor that consumes the request body and
//! deserializes as as JSON into some target type:
//! For example, [`extract::Json`] is an extractor that consumes the request body and
//! deserializes it as JSON into some target type:
//!
//! ```rust,no_run
//! use tower_web::prelude::*;
@@ -186,7 +193,7 @@
//! let app = route("/users/:id", post(create_user));
//!
//! async fn create_user(req: Request<Body>, params: extract::UrlParams<(Uuid,)>) {
//! let (user_id,) = params.0;
//! let user_id: Uuid = (params.0).0;
//!
//! // ...
//! }
@@ -243,7 +250,7 @@
//! tower-web is designed to take full advantage of the tower and tower-http
//! ecosystem of middleware:
//!
//! ## To individual handlers
//! ## Applying middleware to individual handlers
//!
//! A middleware can be applied to a single handler like so:
//!
@@ -262,7 +269,7 @@
//! # };
//! ```
//!
//! ## To groups of routes
//! ## Applying middleware to groups of routes
//!
//! Middleware can also be applied to a group of routes like so:
//!
@@ -293,7 +300,8 @@
//! [`IntoResponse`](response::IntoResponse), even if its a `Result`.
//!
//! However middleware might add new failure cases that has to be handled. For
//! that tower-web provides a `handle_error` combinator:
//! that tower-web provides a [`handle_error`](handler::Layered::handle_error)
//! combinator:
//!
//! ```rust,no_run
//! use tower_web::prelude::*;
@@ -331,27 +339,27 @@
//! # };
//! ```
//!
//! The closure passed to `handle_error` must return something that implements
//! `IntoResponse`.
//! The closure passed to [`handle_error`](handler::Layered::handle_error) must
//! return something that implements [`IntoResponse`](response::IntoResponse).
//!
//! `handle_error` is also available on a group of routes with middleware
//! applied:
//! [`handle_error`](routing::Layered::handle_error) is also available on a
//! group of routes with middleware applied:
//!
//! ```rust,no_run
//! use tower_web::prelude::*;
//! use tower::{
//! BoxError, timeout::{TimeoutLayer, error::Elapsed},
//! };
//! use std::{borrow::Cow, time::Duration};
//! use http::StatusCode;
//! use tower::{BoxError, timeout::TimeoutLayer};
//! use std::time::Duration;
//!
//! let app = route("/", get(handle))
//! .route("/foo", post(other_handle))
//! .layer(TimeoutLayer::new(Duration::from_secs(30)))
//! .handle_error(|error: BoxError| {
//! // ...
//! });
//!
//! async fn handle(req: Request<Body>) {}
//!
//! async fn other_handle(req: Request<Body>) {}
//! # async {
//! # hyper::Server::bind(&"".parse().unwrap()).serve(tower::make::Shared::new(app)).await;
//! # };
@@ -481,7 +489,7 @@
//!
//! # Nesting applications
//!
//! Applications can be nested by calling `nest`:
//! Applications can be nested by calling [`nest`](routing::nest):
//!
//! ```rust,no_run
//! use tower_web::{prelude::*, routing::BoxRoute, body::BoxBody};
@@ -500,7 +508,7 @@
//! # };
//! ```
//!
//! `nest` can also be used to serve static files from a directory:
//! [`nest`](routing::nest) can also be used to serve static files from a directory:
//!
//! ```rust,no_run
//! use tower_web::{prelude::*, service::ServiceExt, routing::nest};
@@ -522,6 +530,8 @@
//!
//! [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
// #![doc(html_root_url = "https://docs.rs/tower-http/0.1.0")]
#![warn(