mirror of
https://github.com/actix/actix-extras.git
synced 2025-06-25 01:51:23 +02:00
move state to request object
This commit is contained in:
@ -12,7 +12,7 @@ use actix_web::middlewares::RequestSession;
|
||||
use futures::stream::{once, Once};
|
||||
|
||||
/// somple handle
|
||||
fn index(mut req: HttpRequest, state: &()) -> Result<HttpResponse> {
|
||||
fn index(mut req: HttpRequest) -> Result<HttpResponse> {
|
||||
println!("{:?}", req);
|
||||
if let Ok(ch) = req.payload_mut().readany() {
|
||||
if let futures::Async::Ready(Some(d)) = ch {
|
||||
@ -32,7 +32,7 @@ fn index(mut req: HttpRequest, state: &()) -> Result<HttpResponse> {
|
||||
}
|
||||
|
||||
/// somple handle
|
||||
fn index_async(req: HttpRequest, state: &()) -> Once<actix_web::Frame, Error>
|
||||
fn index_async(req: HttpRequest) -> Once<actix_web::Frame, Error>
|
||||
{
|
||||
println!("{:?}", req);
|
||||
|
||||
@ -44,7 +44,7 @@ fn index_async(req: HttpRequest, state: &()) -> Once<actix_web::Frame, Error>
|
||||
}
|
||||
|
||||
/// handle with path parameters like `/user/{name}/`
|
||||
fn with_param(req: HttpRequest, state: &()) -> Result<HttpResponse>
|
||||
fn with_param(req: HttpRequest) -> Result<HttpResponse>
|
||||
{
|
||||
println!("{:?}", req);
|
||||
|
||||
@ -75,7 +75,7 @@ fn main() {
|
||||
// async handler
|
||||
.resource("/async/{name}", |r| r.async(Method::GET, index_async))
|
||||
// redirect
|
||||
.resource("/", |r| r.handler(Method::GET, |req, _| {
|
||||
.resource("/", |r| r.handler(Method::GET, |req| {
|
||||
println!("{:?}", req);
|
||||
|
||||
Ok(httpcodes::HTTPFound
|
||||
|
@ -1,3 +1,4 @@
|
||||
#![cfg_attr(feature="cargo-clippy", allow(needless_pass_by_value))]
|
||||
//! There are two level of statfulness in actix-web. Application has state
|
||||
//! that is shared across all handlers within same Application.
|
||||
//! And individual handler can have state.
|
||||
@ -15,11 +16,11 @@ struct AppState {
|
||||
}
|
||||
|
||||
/// somple handle
|
||||
fn index(req: HttpRequest, state: &AppState) -> HttpResponse {
|
||||
fn index(req: HttpRequest<AppState>) -> HttpResponse {
|
||||
println!("{:?}", req);
|
||||
state.counter.set(state.counter.get() + 1);
|
||||
req.state().counter.set(req.state().counter.get() + 1);
|
||||
httpcodes::HTTPOk.with_body(
|
||||
format!("Num of requests: {}", state.counter.get()))
|
||||
format!("Num of requests: {}", req.state().counter.get()))
|
||||
}
|
||||
|
||||
/// `MyWebSocket` counts how many messages it receives from peer,
|
||||
@ -36,7 +37,7 @@ impl Route for MyWebSocket {
|
||||
/// Shared application state
|
||||
type State = AppState;
|
||||
|
||||
fn request(mut req: HttpRequest, ctx: &mut HttpContext<Self>) -> RouteResult<Self>
|
||||
fn request(mut req: HttpRequest<AppState>, ctx: &mut HttpContext<Self>) -> RouteResult<Self>
|
||||
{
|
||||
let resp = ws::handshake(&req)?;
|
||||
ctx.start(resp);
|
||||
@ -44,6 +45,7 @@ impl Route for MyWebSocket {
|
||||
Reply::async(MyWebSocket{counter: 0})
|
||||
}
|
||||
}
|
||||
|
||||
impl StreamHandler<ws::Message> for MyWebSocket {}
|
||||
impl Handler<ws::Message> for MyWebSocket {
|
||||
fn handle(&mut self, msg: ws::Message, ctx: &mut HttpContext<Self>)
|
||||
@ -64,7 +66,6 @@ impl Handler<ws::Message> for MyWebSocket {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
::std::env::set_var("RUST_LOG", "actix_web=info");
|
||||
let _ = env_logger::init();
|
||||
|
Reference in New Issue
Block a user