Function axum::middleware::from_fn
source · pub fn from_fn<F, T>(f: F) -> FromFnLayer<F, (), T>
Expand description
Create a middleware from an async function.
from_fn
requires the function given to
- Be an
async fn
. - Take one or more extractors as the first arguments.
- Take
Next<B>
as the final argument. - Return something that implements
IntoResponse
.
Note that this function doesn’t support extracting State
. For that, use from_fn_with_state
.
§Example
use axum::{
Router,
http::{self, Request},
routing::get,
response::Response,
middleware::{self, Next},
};
async fn my_middleware<B>(
request: Request<B>,
next: Next<B>,
) -> Response {
// do something with `request`...
let response = next.run(request).await;
// do something with `response`...
response
}
let app = Router::new()
.route("/", get(|| async { /* ... */ }))
.layer(middleware::from_fn(my_middleware));
§Running extractors
use axum::{
Router,
extract::TypedHeader,
http::StatusCode,
headers::authorization::{Authorization, Bearer},
http::Request,
middleware::{self, Next},
response::Response,
routing::get,
};
async fn auth<B>(
// run the `TypedHeader` extractor
TypedHeader(auth): TypedHeader<Authorization<Bearer>>,
// you can also add more extractors here but the last
// extractor must implement `FromRequest` which
// `Request` does
request: Request<B>,
next: Next<B>,
) -> Result<Response, StatusCode> {
if token_is_valid(auth.token()) {
let response = next.run(request).await;
Ok(response)
} else {
Err(StatusCode::UNAUTHORIZED)
}
}
fn token_is_valid(token: &str) -> bool {
// ...
}
let app = Router::new()
.route("/", get(|| async { /* ... */ }))
.route_layer(middleware::from_fn(auth));