mirror of
https://github.com/actix/actix-website
synced 2024-12-18 01:43:59 +01:00
Add bit about wrap_fn to middlewares page (#104)
This commit is contained in:
parent
16b8702252
commit
a7c8116fcd
@ -29,6 +29,10 @@ The following demonstrates creating a simple middleware:
|
||||
|
||||
{{< include-example example="middleware" file="main.rs" section="simple" >}}
|
||||
|
||||
Alternatively, for simple use cases, you can use [*wrap_fn*][wrap_fn] to create small, ad-hoc middlewares:
|
||||
|
||||
{{< include-example example="middleware" file="wrap_fn.rs" section="wrap-fn" >}}
|
||||
|
||||
> Actix-web provides several useful middlewares, such as *logging*, *user sessions*,
|
||||
> *compress*, etc.
|
||||
|
||||
@ -130,3 +134,4 @@ into a response.
|
||||
[envlogger]: https://docs.rs/env_logger/*/env_logger/
|
||||
[servicetrait]: https://docs.rs/actix-web/1.0.2/actix_web/dev/trait.Service.html
|
||||
[transformtrait]: https://docs.rs/actix-web/1.0.2/actix_web/dev/trait.Transform.html
|
||||
[wrap_fn]: https://docs.rs/actix-web/1.0.5/actix_web/struct.App.html#method.wrap_fn
|
||||
|
@ -2,6 +2,7 @@ pub mod default_headers;
|
||||
pub mod errorhandler;
|
||||
pub mod logger;
|
||||
pub mod user_sessions;
|
||||
pub mod wrap_fn;
|
||||
|
||||
// <simple>
|
||||
use actix_service::{Service, Transform};
|
||||
|
20
examples/middleware/src/wrap_fn.rs
Normal file
20
examples/middleware/src/wrap_fn.rs
Normal file
@ -0,0 +1,20 @@
|
||||
// <wrap-fn>
|
||||
use actix_service::Service;
|
||||
use actix_web::{web, App};
|
||||
use futures::future::Future;
|
||||
|
||||
fn main() {
|
||||
let app = App::new()
|
||||
.wrap_fn(|req, srv| {
|
||||
println!("Hi from start. You requested: {}", req.path());
|
||||
srv.call(req).map(|res| {
|
||||
println!("Hi from response");
|
||||
res
|
||||
})
|
||||
})
|
||||
.route(
|
||||
"/index.html",
|
||||
web::get().to(|| "Hello, middleware!"),
|
||||
);
|
||||
}
|
||||
// </wrap-fn>
|
Loading…
Reference in New Issue
Block a user