actix-web-async-await icon indicating copy to clipboard operation
actix-web-async-await copied to clipboard

App.resource example please

Open ddosoff opened this issue 6 years ago • 7 comments

Hi, could you give example how to use async fn with App.resource?

ddosoff avatar Jan 11 '19 08:01 ddosoff

Is it possible?

bbigras avatar Jan 28 '19 22:01 bbigras

Any news on this?

bbigras avatar Mar 19 '19 20:03 bbigras

I don't personally use .resource in my stuff. Sorry if it doesn't work in that case. Good news is Actix 1.0 natively supports async fn and you can toss this crate when that's out (soon as far as I can tell).

mehcode avatar Mar 19 '19 20:03 mehcode

Good news is Actix 1.0 natively supports async fn

Not sure this is correct, no mention of it anywhere in the repo. Struggling to port my async code to actix 1.0 at moment.

jesskfullwood avatar May 06 '19 21:05 jesskfullwood

As far as I can see actix 1.0 does not support async/await out of the box. I am not very well versed with Futures and I am struggling too: if this repo could get updated to the new actix version it would greatly simplify the story for async actix experiments.

LukeMathWalker avatar Jun 07 '19 13:06 LukeMathWalker

As far as I can see actix 1.0 does not support async/await out of the box.

I think the best we can do right now is to wrap the futures like this:

[dependencies]
futures-preview = { version = "0.3.0-alpha.16", features = ["compat"] }
futures01 = { package = "futures", version = "0.1" }
#![feature(async_await, await_macro)]

use futures::compat::{Compat, Compat01As03};

// I want to run this future    
async fn my_fn_async(_req: HttpRequest) -> Result<HttpResponse, actix_web::Error> {
    Ok(HttpResponse::Ok().finish())
}

// I need to wrap it
fn test(
    req: HttpRequest,
) -> impl futures01::future::Future<Item = HttpResponse, Error = actix_web::Error> {
    Compat::new(Box::pin(my_fn_async(req)))
}

fn main() {
    HttpServer::new(|| {
        App::new()
            .service(web::resource("/test").to_async(test))
    })
    .bind("127.0.0.1:8080").unwrap()
    .run()
}

bbigras avatar Jun 07 '19 14:06 bbigras

You might be interested in this example: https://github.com/actix/examples/pull/132

jesskfullwood avatar Jun 07 '19 14:06 jesskfullwood