1
0
mirror of https://github.com/actix/actix-website synced 2024-11-23 16:31:08 +01:00

Added documentation for from_fn and wrap in middleware page (#451)

* Added documentation for from_fn and wrap in middleware page

* Update docs/middleware.md

---------

Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
Ross 2024-09-11 22:51:39 +09:00 committed by GitHub
parent c3e64b5889
commit 50c3e75bc3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 31 additions and 0 deletions

View File

@ -27,6 +27,10 @@ Alternatively, for simple use cases, you can use [_wrap_fn_][wrap_fn] to create
<CodeBlock example="middleware" file="wrap_fn.rs" section="wrap-fn" />
You can also use [_from_fn_][from_fn] to in combination with [_wrap_][wrap] to create a function as middlware.
<CodeBlock example="middleware" file="from_fn.rs" section="from-fn" />
> Actix Web provides several useful middleware, such as _logging_, _user sessions_, _compress_, etc.
**Warning: if you use `wrap()` or `wrap_fn()` multiple times, the last occurrence will be executed first.**
@ -111,3 +115,5 @@ You can use the `ErrorHandlers::handler()` method to register a custom error han
[servicetrait]: https://docs.rs/actix-web/4/actix_web/dev/trait.Service.html
[transformtrait]: https://docs.rs/actix-web/4/actix_web/dev/trait.Transform.html
[wrap_fn]: https://docs.rs/actix-web/4/actix_web/struct.App.html#method.wrap_fn
[from_fn]: https://docs.rs/actix-web/4/actix_web/middleware/fn.from_fn.html
[wrap]: https://docs.rs/actix-web/4/actix_web/struct.App.html#method.wrap

View File

@ -0,0 +1,24 @@
#![allow(dead_code, unused_variables)]
// <from-fn>
use actix_web::{
body::MessageBody,
dev::{ServiceRequest, ServiceResponse},
middleware::{from_fn, Next},
App, Error,
};
async fn my_midleware(
req: ServiceRequest,
next: Next<impl MessageBody>,
) -> Result<ServiceResponse<impl MessageBody>, Error> {
// pre-processing
next.call(req).await
// post-processing
}
#[actix_web::main]
async fn main() {
let app = App::new().wrap(from_fn(my_midleware));
}
// </from-fn>

View File

@ -1,5 +1,6 @@
pub mod default_headers;
pub mod errorhandler;
pub mod from_fn;
pub mod logger;
pub mod user_sessions;
pub mod wrap_fn;