diff --git a/content/docs/middleware.md b/content/docs/middleware.md index 7bea23e..cdd74b5 100644 --- a/content/docs/middleware.md +++ b/content/docs/middleware.md @@ -127,11 +127,11 @@ into a response. {{< include-example example="middleware" file="errorhandler.rs" section="error-handler" >}} -[sessionobj]: https://docs.rs/actix-session/0.1.1/actix_session/struct.Session.html -[requestsession]: https://docs.rs/actix-session/0.1.1/actix_session/struct.Session.html -[cookiesession]: https://docs.rs/actix-session/0.1.1/actix_session/struct.CookieSession.html -[actixsession]: https://docs.rs/actix-session/0.1.1/actix_session/ +[sessionobj]: https://docs.rs/actix-session/0.3.0/actix_session/struct.Session.html +[requestsession]: https://docs.rs/actix-session/0.3.0/actix_session/struct.Session.html +[cookiesession]: https://docs.rs/actix-session/0.3.0/actix_session/struct.CookieSession.html +[actixsession]: https://docs.rs/actix-session/0.3.0/actix_session/ [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 +[servicetrait]: https://docs.rs/actix-web/2/actix_web/dev/trait.Service.html +[transformtrait]: https://docs.rs/actix-web/2/actix_web/dev/trait.Transform.html +[wrap_fn]: https://docs.rs/actix-web/2/actix_web/struct.App.html#method.wrap_fn diff --git a/examples/middleware/Cargo.toml b/examples/middleware/Cargo.toml index 3448545..dece5a1 100644 --- a/examples/middleware/Cargo.toml +++ b/examples/middleware/Cargo.toml @@ -4,8 +4,9 @@ version = "1.0.0" edition = "2018" [dependencies] -actix-web = "1.0" -actix-service = "0.4" -actix-session = "0.1" -futures = "0.1" +actix-web = "2.0" +actix-rt = "1.0" +actix-service = "1.0" +actix-session = "0.3" +futures = "0.3.1" env_logger = "0.6" diff --git a/examples/middleware/src/default_headers.rs b/examples/middleware/src/default_headers.rs index d998a15..f71377d 100644 --- a/examples/middleware/src/default_headers.rs +++ b/examples/middleware/src/default_headers.rs @@ -1,7 +1,8 @@ // use actix_web::{http, middleware, HttpResponse}; -pub fn main() { +#[actix_rt::main] +async fn main() -> std::io::Result<()> { use actix_web::{web, App, HttpServer}; HttpServer::new(|| { @@ -16,9 +17,8 @@ pub fn main() { ), ) }) - .bind("127.0.0.1:8088") - .unwrap() + .bind("127.0.0.1:8088")? .run() - .unwrap(); + .await } // diff --git a/examples/middleware/src/errorhandler.rs b/examples/middleware/src/errorhandler.rs index dacef6e..aa16b28 100644 --- a/examples/middleware/src/errorhandler.rs +++ b/examples/middleware/src/errorhandler.rs @@ -10,7 +10,8 @@ fn render_500(mut res: dev::ServiceResponse) -> Result std::io::Result<()> { use actix_web::{web, App, HttpServer}; HttpServer::new(|| { @@ -25,9 +26,8 @@ pub fn main() { .route(web::head().to(|| HttpResponse::MethodNotAllowed())), ) }) - .bind("127.0.0.1:8088") - .unwrap() + .bind("127.0.0.1:8088")? .run() - .unwrap(); + .await } // diff --git a/examples/middleware/src/logger.rs b/examples/middleware/src/logger.rs index 254e5d1..fd991d2 100644 --- a/examples/middleware/src/logger.rs +++ b/examples/middleware/src/logger.rs @@ -2,7 +2,8 @@ use actix_web::middleware::Logger; use env_logger; -pub fn main() { +#[actix_rt::main] +async fn main() -> std::io::Result<()> { use actix_web::{App, HttpServer}; std::env::set_var("RUST_LOG", "actix_web=info"); @@ -13,9 +14,8 @@ pub fn main() { .wrap(Logger::default()) .wrap(Logger::new("%a %{User-Agent}i")) }) - .bind("127.0.0.1:8088") - .unwrap() + .bind("127.0.0.1:8088")? .run() - .unwrap(); + .await } // diff --git a/examples/middleware/src/main.rs b/examples/middleware/src/main.rs index ba97d1d..50018cd 100644 --- a/examples/middleware/src/main.rs +++ b/examples/middleware/src/main.rs @@ -5,10 +5,13 @@ pub mod user_sessions; pub mod wrap_fn; // +use std::pin::Pin; +use std::task::{Context, Poll}; + use actix_service::{Service, Transform}; use actix_web::{dev::ServiceRequest, dev::ServiceResponse, Error}; -use futures::future::{ok, FutureResult}; -use futures::{Future, Poll}; +use futures::future::{ok, Ready}; +use futures::Future; // There are two steps in middleware processing. // 1. Middleware initialization, middleware factory gets called with @@ -30,7 +33,7 @@ where type Error = Error; type InitError = (); type Transform = SayHiMiddleware; - type Future = FutureResult; + type Future = Ready>; fn new_transform(&self, service: S) -> Self::Future { ok(SayHiMiddleware { service }) @@ -50,34 +53,40 @@ where type Request = ServiceRequest; type Response = ServiceResponse; type Error = Error; - type Future = Box>; + type Future = Pin>>>; - fn poll_ready(&mut self) -> Poll<(), Self::Error> { - self.service.poll_ready() + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { + self.service.poll_ready(cx) } fn call(&mut self, req: ServiceRequest) -> Self::Future { println!("Hi from start. You requested: {}", req.path()); - Box::new(self.service.call(req).and_then(|res| { + let fut = self.service.call(req); + + Box::pin(async move { + let res = fut.await?; + println!("Hi from response"); Ok(res) - })) + }) } } // -fn main() { +#[actix_rt::main] +async fn main() -> std::io::Result<()> { use actix_web::{web, App, HttpServer}; HttpServer::new(|| { App::new().wrap(SayHi).service( web::resource("/") - .to(|| "Hello, middleware! Check the console where the server is run."), + .to(|| async { + "Hello, middleware! Check the console where the server is run." + }), ) }) - .bind("127.0.0.1:8088") - .unwrap() + .bind("127.0.0.1:8088")? .run() - .unwrap(); + .await } diff --git a/examples/middleware/src/user_sessions.rs b/examples/middleware/src/user_sessions.rs index b4f99ff..261f5c2 100644 --- a/examples/middleware/src/user_sessions.rs +++ b/examples/middleware/src/user_sessions.rs @@ -2,7 +2,7 @@ use actix_session::{CookieSession, Session}; use actix_web::{web, App, Error, HttpResponse, HttpServer}; -pub fn index(session: Session) -> Result { +async fn index(session: Session) -> Result { // access session data if let Some(count) = session.get::("counter")? { session.set("counter", count + 1)?; @@ -16,7 +16,8 @@ pub fn index(session: Session) -> Result { ))) } -pub fn main() { +#[actix_rt::main] +async fn main() -> std::io::Result<()> { HttpServer::new(|| { App::new() .wrap( @@ -25,9 +26,8 @@ pub fn main() { ) .service(web::resource("/").to(index)) }) - .bind("127.0.0.1:8088") - .unwrap() + .bind("127.0.0.1:8088")? .run() - .unwrap(); + .await } // diff --git a/examples/middleware/src/wrap_fn.rs b/examples/middleware/src/wrap_fn.rs index 18c139e..a3f8f0a 100644 --- a/examples/middleware/src/wrap_fn.rs +++ b/examples/middleware/src/wrap_fn.rs @@ -1,9 +1,10 @@ // use actix_service::Service; use actix_web::{web, App}; -use futures::future::Future; +use futures::future::FutureExt; -fn main() { +#[actix_rt::main] +async fn main() { let app = App::new() .wrap_fn(|req, srv| { println!("Hi from start. You requested: {}", req.path()); @@ -14,7 +15,9 @@ fn main() { }) .route( "/index.html", - web::get().to(|| "Hello, middleware!"), + web::get().to(|| async { + "Hello, middleware!" + }), ); } //