Skip to content

Commit 789cb0f

Browse files
committed
Update middleware
1 parent 94c8c84 commit 789cb0f

File tree

8 files changed

+57
-44
lines changed

8 files changed

+57
-44
lines changed

content/docs/middleware.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ into a response.
127127

128128
{{< include-example example="middleware" file="errorhandler.rs" section="error-handler" >}}
129129

130-
[sessionobj]: https://docs.rs/actix-session/0.1.1/actix_session/struct.Session.html
131-
[requestsession]: https://docs.rs/actix-session/0.1.1/actix_session/struct.Session.html
132-
[cookiesession]: https://docs.rs/actix-session/0.1.1/actix_session/struct.CookieSession.html
133-
[actixsession]: https://docs.rs/actix-session/0.1.1/actix_session/
130+
[sessionobj]: https://docs.rs/actix-session/0.3.0/actix_session/struct.Session.html
131+
[requestsession]: https://docs.rs/actix-session/0.3.0/actix_session/struct.Session.html
132+
[cookiesession]: https://docs.rs/actix-session/0.3.0/actix_session/struct.CookieSession.html
133+
[actixsession]: https://docs.rs/actix-session/0.3.0/actix_session/
134134
[envlogger]: https://docs.rs/env_logger/*/env_logger/
135-
[servicetrait]: https://docs.rs/actix-web/1.0.2/actix_web/dev/trait.Service.html
136-
[transformtrait]: https://docs.rs/actix-web/1.0.2/actix_web/dev/trait.Transform.html
137-
[wrap_fn]: https://docs.rs/actix-web/1.0.5/actix_web/struct.App.html#method.wrap_fn
135+
[servicetrait]: https://docs.rs/actix-web/2/actix_web/dev/trait.Service.html
136+
[transformtrait]: https://docs.rs/actix-web/2/actix_web/dev/trait.Transform.html
137+
[wrap_fn]: https://docs.rs/actix-web/2/actix_web/struct.App.html#method.wrap_fn

examples/middleware/Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ version = "1.0.0"
44
edition = "2018"
55

66
[dependencies]
7-
actix-web = "1.0"
8-
actix-service = "0.4"
9-
actix-session = "0.1"
10-
futures = "0.1"
7+
actix-web = "2.0"
8+
actix-rt = "1.0"
9+
actix-service = "1.0"
10+
actix-session = "0.3"
11+
futures = "0.3.1"
1112
env_logger = "0.6"

examples/middleware/src/default_headers.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// <default-headers>
22
use actix_web::{http, middleware, HttpResponse};
33

4-
pub fn main() {
4+
#[actix_rt::main]
5+
async fn main() -> std::io::Result<()> {
56
use actix_web::{web, App, HttpServer};
67

78
HttpServer::new(|| {
@@ -16,9 +17,8 @@ pub fn main() {
1617
),
1718
)
1819
})
19-
.bind("127.0.0.1:8088")
20-
.unwrap()
20+
.bind("127.0.0.1:8088")?
2121
.run()
22-
.unwrap();
22+
.await
2323
}
2424
// </default-headers>

examples/middleware/src/errorhandler.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ fn render_500<B>(mut res: dev::ServiceResponse<B>) -> Result<ErrorHandlerRespons
1010
Ok(ErrorHandlerResponse::Response(res))
1111
}
1212

13-
pub fn main() {
13+
#[actix_rt::main]
14+
async fn main() -> std::io::Result<()> {
1415
use actix_web::{web, App, HttpServer};
1516

1617
HttpServer::new(|| {
@@ -25,9 +26,8 @@ pub fn main() {
2526
.route(web::head().to(|| HttpResponse::MethodNotAllowed())),
2627
)
2728
})
28-
.bind("127.0.0.1:8088")
29-
.unwrap()
29+
.bind("127.0.0.1:8088")?
3030
.run()
31-
.unwrap();
31+
.await
3232
}
3333
// </error-handler>

examples/middleware/src/logger.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
use actix_web::middleware::Logger;
33
use env_logger;
44

5-
pub fn main() {
5+
#[actix_rt::main]
6+
async fn main() -> std::io::Result<()> {
67
use actix_web::{App, HttpServer};
78

89
std::env::set_var("RUST_LOG", "actix_web=info");
@@ -13,9 +14,8 @@ pub fn main() {
1314
.wrap(Logger::default())
1415
.wrap(Logger::new("%a %{User-Agent}i"))
1516
})
16-
.bind("127.0.0.1:8088")
17-
.unwrap()
17+
.bind("127.0.0.1:8088")?
1818
.run()
19-
.unwrap();
19+
.await
2020
}
2121
// </logger>

examples/middleware/src/main.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ pub mod user_sessions;
55
pub mod wrap_fn;
66

77
// <simple>
8+
use std::pin::Pin;
9+
use std::task::{Context, Poll};
10+
811
use actix_service::{Service, Transform};
912
use actix_web::{dev::ServiceRequest, dev::ServiceResponse, Error};
10-
use futures::future::{ok, FutureResult};
11-
use futures::{Future, Poll};
13+
use futures::future::{ok, Ready};
14+
use futures::Future;
1215

1316
// There are two steps in middleware processing.
1417
// 1. Middleware initialization, middleware factory gets called with
@@ -30,7 +33,7 @@ where
3033
type Error = Error;
3134
type InitError = ();
3235
type Transform = SayHiMiddleware<S>;
33-
type Future = FutureResult<Self::Transform, Self::InitError>;
36+
type Future = Ready<Result<Self::Transform, Self::InitError>>;
3437

3538
fn new_transform(&self, service: S) -> Self::Future {
3639
ok(SayHiMiddleware { service })
@@ -50,34 +53,40 @@ where
5053
type Request = ServiceRequest;
5154
type Response = ServiceResponse<B>;
5255
type Error = Error;
53-
type Future = Box<dyn Future<Item = Self::Response, Error = Self::Error>>;
56+
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;
5457

55-
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
56-
self.service.poll_ready()
58+
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
59+
self.service.poll_ready(cx)
5760
}
5861

5962
fn call(&mut self, req: ServiceRequest) -> Self::Future {
6063
println!("Hi from start. You requested: {}", req.path());
6164

62-
Box::new(self.service.call(req).and_then(|res| {
65+
let fut = self.service.call(req);
66+
67+
Box::pin(async move {
68+
let res = fut.await?;
69+
6370
println!("Hi from response");
6471
Ok(res)
65-
}))
72+
})
6673
}
6774
}
6875
// </simple>
6976

70-
fn main() {
77+
#[actix_rt::main]
78+
async fn main() -> std::io::Result<()> {
7179
use actix_web::{web, App, HttpServer};
7280

7381
HttpServer::new(|| {
7482
App::new().wrap(SayHi).service(
7583
web::resource("/")
76-
.to(|| "Hello, middleware! Check the console where the server is run."),
84+
.to(|| async {
85+
"Hello, middleware! Check the console where the server is run."
86+
}),
7787
)
7888
})
79-
.bind("127.0.0.1:8088")
80-
.unwrap()
89+
.bind("127.0.0.1:8088")?
8190
.run()
82-
.unwrap();
91+
.await
8392
}

examples/middleware/src/user_sessions.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use actix_session::{CookieSession, Session};
33
use actix_web::{web, App, Error, HttpResponse, HttpServer};
44

5-
pub fn index(session: Session) -> Result<HttpResponse, Error> {
5+
async fn index(session: Session) -> Result<HttpResponse, Error> {
66
// access session data
77
if let Some(count) = session.get::<i32>("counter")? {
88
session.set("counter", count + 1)?;
@@ -16,7 +16,8 @@ pub fn index(session: Session) -> Result<HttpResponse, Error> {
1616
)))
1717
}
1818

19-
pub fn main() {
19+
#[actix_rt::main]
20+
async fn main() -> std::io::Result<()> {
2021
HttpServer::new(|| {
2122
App::new()
2223
.wrap(
@@ -25,9 +26,8 @@ pub fn main() {
2526
)
2627
.service(web::resource("/").to(index))
2728
})
28-
.bind("127.0.0.1:8088")
29-
.unwrap()
29+
.bind("127.0.0.1:8088")?
3030
.run()
31-
.unwrap();
31+
.await
3232
}
3333
// </user-session>

examples/middleware/src/wrap_fn.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// <wrap-fn>
22
use actix_service::Service;
33
use actix_web::{web, App};
4-
use futures::future::Future;
4+
use futures::future::FutureExt;
55

6-
fn main() {
6+
#[actix_rt::main]
7+
async fn main() {
78
let app = App::new()
89
.wrap_fn(|req, srv| {
910
println!("Hi from start. You requested: {}", req.path());
@@ -14,7 +15,9 @@ fn main() {
1415
})
1516
.route(
1617
"/index.html",
17-
web::get().to(|| "Hello, middleware!"),
18+
web::get().to(|| async {
19+
"Hello, middleware!"
20+
}),
1821
);
1922
}
2023
// </wrap-fn>

0 commit comments

Comments
 (0)