Document adding middleware to multiple groups of routes (#293)

This commit is contained in:
David Pedersen 2021-08-31 09:07:57 +02:00 committed by GitHub
parent c082107fb6
commit e698586193
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 32 additions and 4 deletions

View File

@ -647,14 +647,42 @@
//! };
//! use tower::limit::ConcurrencyLimitLayer;
//!
//! async fn handler() {}
//!
//! let app = Router::new()
//! .route("/", get(get_slash))
//! .route("/foo", post(post_foo))
//! .route("/", get(handler))
//! .route("/foo", post(handler))
//! .layer(ConcurrencyLimitLayer::new(100));
//! # async {
//! # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap();
//! # };
//! ```
//!
//! Note that [`Router::layer`] applies the middleware to all previously added
//! routes, of that particular `Router`. If you need multiple groups of routes
//! with different middleware build them separately and combine them with
//! [`Router::or`]:
//!
//! ```rust,no_run
//! use axum::{
//! handler::{get, post},
//! Router,
//! };
//! use tower::limit::ConcurrencyLimitLayer;
//! # type MyAuthLayer = tower::layer::util::Identity;
//!
//! async fn handler() {}
//!
//! let foo = Router::new()
//! .route("/", get(handler))
//! .route("/foo", post(handler))
//! .layer(ConcurrencyLimitLayer::new(100));
//!
//! async fn get_slash() {}
//! let bar = Router::new()
//! .route("/requires-auth", get(handler))
//! .layer(MyAuthLayer::new());
//!
//! async fn post_foo() {}
//! let app = foo.or(bar);
//! # async {
//! # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap();
//! # };