mirror of
https://github.com/actix/actix-extras.git
synced 2025-06-25 09:59:21 +02:00
make possible to use async handler
This commit is contained in:
@ -2,8 +2,10 @@
|
||||
extern crate actix;
|
||||
extern crate actix_web;
|
||||
extern crate env_logger;
|
||||
extern crate futures;
|
||||
|
||||
use actix_web::*;
|
||||
use futures::stream::{once, Once};
|
||||
|
||||
/// somple handle
|
||||
fn index(req: &mut HttpRequest, _payload: Payload, state: &()) -> HttpResponse {
|
||||
@ -11,6 +13,18 @@ fn index(req: &mut HttpRequest, _payload: Payload, state: &()) -> HttpResponse {
|
||||
httpcodes::HTTPOk.into()
|
||||
}
|
||||
|
||||
/// somple handle
|
||||
fn index_async(req: &mut HttpRequest, _payload: Payload, state: &()) -> Once<actix_web::Frame, ()>
|
||||
{
|
||||
println!("{:?}", req);
|
||||
|
||||
once(Ok(HttpResponse::builder(StatusCode::OK)
|
||||
.content_type("text/html")
|
||||
.body(format!("Hello {}!", req.match_info().get("name").unwrap()))
|
||||
.unwrap()
|
||||
.into()))
|
||||
}
|
||||
|
||||
/// handle with path parameters like `/name/{name}/`
|
||||
fn with_param(req: &mut HttpRequest, _payload: Payload, state: &())
|
||||
-> HandlerResult<HttpResponse>
|
||||
@ -35,6 +49,8 @@ fn main() {
|
||||
.handler("/index.html", index)
|
||||
// with path parameters
|
||||
.resource("/user/{name}/", |r| r.handler(Method::GET, with_param))
|
||||
// async handler
|
||||
.resource("/async/{name}", |r| r.async(Method::GET, index_async))
|
||||
// redirect
|
||||
.resource("/", |r| r.handler(Method::GET, |req, _, _| {
|
||||
println!("{:?}", req);
|
||||
|
Reference in New Issue
Block a user