Skip to content

Instantly share code, notes, and snippets.

@jesskfullwood
Created August 25, 2018 13:36
Show Gist options
  • Save jesskfullwood/d836df0eeb8334b51a19a3424bcb84de to your computer and use it in GitHub Desktop.
Save jesskfullwood/d836df0eeb8334b51a19a3424bcb84de to your computer and use it in GitHub Desktop.
Actix-web + futures 0.3 + async example
cargo-features = ["rename-dependency"]
[package]
name = "actix-async"
version = "0.1.0"
[dependencies]
actix-web = "0.7.4"
futures-preview = { version = "0.3.0-alpha.3", features = ["tokio-compat"] }
futures1 = { version = "0.1", package = "futures" }
serde_derive = "1.0.75"
serde = "1.0.75"
#![feature(futures_api, async_await, await_macro, pin)]
extern crate actix_web;
extern crate futures;
extern crate futures1;
#[macro_use]
extern crate serde_derive;
extern crate serde;
use actix_web::{server, App, Error, HttpMessage, HttpRequest};
use futures::compat::{Future01CompatExt, TokioDefaultSpawn};
use futures::prelude::*;
use std::boxed::PinBox;
fn main() {
server::new(|| App::new().resource("/", |r| r.f(|req| wrap_fut3(index(req.clone())))))
.bind("127.0.0.1:8088")
.unwrap()
.run();
}
fn wrap_fut3<T, Ok, Error>(fut3: T) -> Box<dyn futures1::Future<Item = Ok, Error = Error>>
where
T: Future<Output = Result<Ok, Error>> + 'static,
{
let pin3: PinBox<T> = PinBox::new(fut3);
let fut1 = pin3.compat(TokioDefaultSpawn);
Box::new(fut1)
}
#[derive(Deserialize)]
struct User {
name: String,
age: u32,
}
async fn index(req: HttpRequest) -> Result<String, Error> {
let user = await!(req.json::<User>().compat())?;
Ok(format!("Hello {}, you are aged {}", user.name, user.age))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment