mirror of
https://github.com/actix/actix-website
synced 2024-11-24 00:41:07 +01:00
Update middleware
This commit is contained in:
parent
94c8c843b1
commit
789cb0fd1a
@ -127,11 +127,11 @@ into a response.
|
|||||||
|
|
||||||
{{< include-example example="middleware" file="errorhandler.rs" section="error-handler" >}}
|
{{< include-example example="middleware" file="errorhandler.rs" section="error-handler" >}}
|
||||||
|
|
||||||
[sessionobj]: https://docs.rs/actix-session/0.1.1/actix_session/struct.Session.html
|
[sessionobj]: https://docs.rs/actix-session/0.3.0/actix_session/struct.Session.html
|
||||||
[requestsession]: https://docs.rs/actix-session/0.1.1/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.1.1/actix_session/struct.CookieSession.html
|
[cookiesession]: https://docs.rs/actix-session/0.3.0/actix_session/struct.CookieSession.html
|
||||||
[actixsession]: https://docs.rs/actix-session/0.1.1/actix_session/
|
[actixsession]: https://docs.rs/actix-session/0.3.0/actix_session/
|
||||||
[envlogger]: https://docs.rs/env_logger/*/env_logger/
|
[envlogger]: https://docs.rs/env_logger/*/env_logger/
|
||||||
[servicetrait]: https://docs.rs/actix-web/1.0.2/actix_web/dev/trait.Service.html
|
[servicetrait]: https://docs.rs/actix-web/2/actix_web/dev/trait.Service.html
|
||||||
[transformtrait]: https://docs.rs/actix-web/1.0.2/actix_web/dev/trait.Transform.html
|
[transformtrait]: https://docs.rs/actix-web/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
|
[wrap_fn]: https://docs.rs/actix-web/2/actix_web/struct.App.html#method.wrap_fn
|
||||||
|
@ -4,8 +4,9 @@ version = "1.0.0"
|
|||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "1.0"
|
actix-web = "2.0"
|
||||||
actix-service = "0.4"
|
actix-rt = "1.0"
|
||||||
actix-session = "0.1"
|
actix-service = "1.0"
|
||||||
futures = "0.1"
|
actix-session = "0.3"
|
||||||
|
futures = "0.3.1"
|
||||||
env_logger = "0.6"
|
env_logger = "0.6"
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
// <default-headers>
|
// <default-headers>
|
||||||
use actix_web::{http, middleware, HttpResponse};
|
use actix_web::{http, middleware, HttpResponse};
|
||||||
|
|
||||||
pub fn main() {
|
#[actix_rt::main]
|
||||||
|
async fn main() -> std::io::Result<()> {
|
||||||
use actix_web::{web, App, HttpServer};
|
use actix_web::{web, App, HttpServer};
|
||||||
|
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
@ -16,9 +17,8 @@ pub fn main() {
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.bind("127.0.0.1:8088")
|
.bind("127.0.0.1:8088")?
|
||||||
.unwrap()
|
|
||||||
.run()
|
.run()
|
||||||
.unwrap();
|
.await
|
||||||
}
|
}
|
||||||
// </default-headers>
|
// </default-headers>
|
||||||
|
@ -10,7 +10,8 @@ fn render_500<B>(mut res: dev::ServiceResponse<B>) -> Result<ErrorHandlerRespons
|
|||||||
Ok(ErrorHandlerResponse::Response(res))
|
Ok(ErrorHandlerResponse::Response(res))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() {
|
#[actix_rt::main]
|
||||||
|
async fn main() -> std::io::Result<()> {
|
||||||
use actix_web::{web, App, HttpServer};
|
use actix_web::{web, App, HttpServer};
|
||||||
|
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
@ -25,9 +26,8 @@ pub fn main() {
|
|||||||
.route(web::head().to(|| HttpResponse::MethodNotAllowed())),
|
.route(web::head().to(|| HttpResponse::MethodNotAllowed())),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.bind("127.0.0.1:8088")
|
.bind("127.0.0.1:8088")?
|
||||||
.unwrap()
|
|
||||||
.run()
|
.run()
|
||||||
.unwrap();
|
.await
|
||||||
}
|
}
|
||||||
// </error-handler>
|
// </error-handler>
|
||||||
|
@ -2,7 +2,8 @@
|
|||||||
use actix_web::middleware::Logger;
|
use actix_web::middleware::Logger;
|
||||||
use env_logger;
|
use env_logger;
|
||||||
|
|
||||||
pub fn main() {
|
#[actix_rt::main]
|
||||||
|
async fn main() -> std::io::Result<()> {
|
||||||
use actix_web::{App, HttpServer};
|
use actix_web::{App, HttpServer};
|
||||||
|
|
||||||
std::env::set_var("RUST_LOG", "actix_web=info");
|
std::env::set_var("RUST_LOG", "actix_web=info");
|
||||||
@ -13,9 +14,8 @@ pub fn main() {
|
|||||||
.wrap(Logger::default())
|
.wrap(Logger::default())
|
||||||
.wrap(Logger::new("%a %{User-Agent}i"))
|
.wrap(Logger::new("%a %{User-Agent}i"))
|
||||||
})
|
})
|
||||||
.bind("127.0.0.1:8088")
|
.bind("127.0.0.1:8088")?
|
||||||
.unwrap()
|
|
||||||
.run()
|
.run()
|
||||||
.unwrap();
|
.await
|
||||||
}
|
}
|
||||||
// </logger>
|
// </logger>
|
||||||
|
@ -5,10 +5,13 @@ pub mod user_sessions;
|
|||||||
pub mod wrap_fn;
|
pub mod wrap_fn;
|
||||||
|
|
||||||
// <simple>
|
// <simple>
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use actix_service::{Service, Transform};
|
use actix_service::{Service, Transform};
|
||||||
use actix_web::{dev::ServiceRequest, dev::ServiceResponse, Error};
|
use actix_web::{dev::ServiceRequest, dev::ServiceResponse, Error};
|
||||||
use futures::future::{ok, FutureResult};
|
use futures::future::{ok, Ready};
|
||||||
use futures::{Future, Poll};
|
use futures::Future;
|
||||||
|
|
||||||
// There are two steps in middleware processing.
|
// There are two steps in middleware processing.
|
||||||
// 1. Middleware initialization, middleware factory gets called with
|
// 1. Middleware initialization, middleware factory gets called with
|
||||||
@ -30,7 +33,7 @@ where
|
|||||||
type Error = Error;
|
type Error = Error;
|
||||||
type InitError = ();
|
type InitError = ();
|
||||||
type Transform = SayHiMiddleware<S>;
|
type Transform = SayHiMiddleware<S>;
|
||||||
type Future = FutureResult<Self::Transform, Self::InitError>;
|
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
||||||
|
|
||||||
fn new_transform(&self, service: S) -> Self::Future {
|
fn new_transform(&self, service: S) -> Self::Future {
|
||||||
ok(SayHiMiddleware { service })
|
ok(SayHiMiddleware { service })
|
||||||
@ -50,34 +53,40 @@ where
|
|||||||
type Request = ServiceRequest;
|
type Request = ServiceRequest;
|
||||||
type Response = ServiceResponse<B>;
|
type Response = ServiceResponse<B>;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = Box<dyn Future<Item = Self::Response, Error = Self::Error>>;
|
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
self.service.poll_ready()
|
self.service.poll_ready(cx)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: ServiceRequest) -> Self::Future {
|
fn call(&mut self, req: ServiceRequest) -> Self::Future {
|
||||||
println!("Hi from start. You requested: {}", req.path());
|
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");
|
println!("Hi from response");
|
||||||
Ok(res)
|
Ok(res)
|
||||||
}))
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// </simple>
|
// </simple>
|
||||||
|
|
||||||
fn main() {
|
#[actix_rt::main]
|
||||||
|
async fn main() -> std::io::Result<()> {
|
||||||
use actix_web::{web, App, HttpServer};
|
use actix_web::{web, App, HttpServer};
|
||||||
|
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new().wrap(SayHi).service(
|
App::new().wrap(SayHi).service(
|
||||||
web::resource("/")
|
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")
|
.bind("127.0.0.1:8088")?
|
||||||
.unwrap()
|
|
||||||
.run()
|
.run()
|
||||||
.unwrap();
|
.await
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
use actix_session::{CookieSession, Session};
|
use actix_session::{CookieSession, Session};
|
||||||
use actix_web::{web, App, Error, HttpResponse, HttpServer};
|
use actix_web::{web, App, Error, HttpResponse, HttpServer};
|
||||||
|
|
||||||
pub fn index(session: Session) -> Result<HttpResponse, Error> {
|
async fn index(session: Session) -> Result<HttpResponse, Error> {
|
||||||
// access session data
|
// access session data
|
||||||
if let Some(count) = session.get::<i32>("counter")? {
|
if let Some(count) = session.get::<i32>("counter")? {
|
||||||
session.set("counter", count + 1)?;
|
session.set("counter", count + 1)?;
|
||||||
@ -16,7 +16,8 @@ pub fn index(session: Session) -> Result<HttpResponse, Error> {
|
|||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() {
|
#[actix_rt::main]
|
||||||
|
async fn main() -> std::io::Result<()> {
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new()
|
App::new()
|
||||||
.wrap(
|
.wrap(
|
||||||
@ -25,9 +26,8 @@ pub fn main() {
|
|||||||
)
|
)
|
||||||
.service(web::resource("/").to(index))
|
.service(web::resource("/").to(index))
|
||||||
})
|
})
|
||||||
.bind("127.0.0.1:8088")
|
.bind("127.0.0.1:8088")?
|
||||||
.unwrap()
|
|
||||||
.run()
|
.run()
|
||||||
.unwrap();
|
.await
|
||||||
}
|
}
|
||||||
// </user-session>
|
// </user-session>
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
// <wrap-fn>
|
// <wrap-fn>
|
||||||
use actix_service::Service;
|
use actix_service::Service;
|
||||||
use actix_web::{web, App};
|
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()
|
let app = App::new()
|
||||||
.wrap_fn(|req, srv| {
|
.wrap_fn(|req, srv| {
|
||||||
println!("Hi from start. You requested: {}", req.path());
|
println!("Hi from start. You requested: {}", req.path());
|
||||||
@ -14,7 +15,9 @@ fn main() {
|
|||||||
})
|
})
|
||||||
.route(
|
.route(
|
||||||
"/index.html",
|
"/index.html",
|
||||||
web::get().to(|| "Hello, middleware!"),
|
web::get().to(|| async {
|
||||||
|
"Hello, middleware!"
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
// </wrap-fn>
|
// </wrap-fn>
|
||||||
|
Loading…
Reference in New Issue
Block a user