Skip to content
Please note that GitHub no longer supports Internet Explorer.

We recommend upgrading to the latest Microsoft Edge, Google Chrome, or Firefox.

Learn more
Adds proc macro that converts async fn to a compatible actix-web request handler
Rust
Branch: master
Clone or download
Cannot retrieve the latest commit at this time.
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples/actix-web-example update to the latest actix-web and rust May 25, 2019
src migrate to latest nightly May 17, 2019
.gitignore initial commit Apr 20, 2019
Cargo.toml migrate to latest nightly May 17, 2019
README.md update to the latest actix-web and rust May 25, 2019
rust-toolchain migrate to latest nightly May 17, 2019

README.md

This crate allows using nightly async/await features with the latest actix-web version (1.0.0-rc).

Example

#![feature(await_macro, async_await)]

use actix_web::{get, web, App, Error, HttpResponse, HttpServer, Result};
use actix_web_async_compat::async_compat;
use futures03::{compat::Future01CompatExt as _, FutureExt as _, TryFutureExt as _};
use hyper::Client;
use serde::Deserialize;
use std::{
    io,
    time::{Duration, Instant},
};
use tokio::prelude::*;

#[get("/welcome")]
#[async_compat]
async fn index() -> Result<HttpResponse> {
    let client = Client::new();
    let uri = "http://httpbin.org/ip".parse().unwrap();

    let response = client
        .get(uri)
        .compat()
        .await
        .map_err(|e| HttpResponse::InternalServerError().body(format!("ERROR: {:?}", e)))?;

    println!("Response: {}", response.status());

    let mut body = response.into_body();

    body.for_each(|chunk| {
        io::stdout()
            .write_all(&chunk)
            .map_err(|e| panic!("example expects stdout is open, error={}", e))
    })
    .compat()
    .map_err(|e| HttpResponse::InternalServerError().body(format!("ERROR: {:?}", e)))
    .await?;

    Ok(HttpResponse::Ok().body("OK"))
}

#[derive(Debug, Deserialize)]
struct UserForm {
    name: String,
}

#[async_compat]
async fn index2(form: actix_web::web::Form<UserForm>) -> Result<HttpResponse> {
    dbg!(form);
    use tokio::timer::Delay;

    // Wait 2s
    Delay::new(Instant::now() + Duration::from_secs(2))
        .compat()
        .await?;

    Ok(HttpResponse::Ok().body("OK"))
}

fn main() {
    HttpServer::new(|| {
        App::new()
            .wrap(actix_web::middleware::Logger::default())
            .service(index)
            .service(web::resource("/welcome2").route(web::post().to_async(index2)))
    })
    .bind("127.0.0.1:8080")
    .unwrap()
    .run()
    .unwrap();
}

Please take a look into examples dir for more information.

You can’t perform that action at this time.