1
0
mirror of https://github.com/actix/examples synced 2025-02-08 20:06:07 +01:00

161 lines
5.4 KiB
Rust
Raw Normal View History

2018-04-27 15:30:29 -04:00
// This is a contrived example intended to illustrate actix-web features.
// *Imagine* that you have a process that involves 3 steps. The steps here
// are dumb in that they do nothing other than call an
2018-05-08 11:08:43 -07:00
// httpbin endpoint that returns the json that was posted to it. The intent
// here is to illustrate how to chain these steps together as futures and return
2018-04-27 15:30:29 -04:00
// a final result in a response.
//
// Actix-web features illustrated here include:
// 1. handling json input param
// 2. validating user-submitted parameters using the 'validator' crate
// 2. actix-web client features:
// - POSTing json body
// 3. chaining futures into a single response used by an asynch endpoint
2018-05-11 08:05:03 -04:00
//
2018-05-20 21:03:29 -07:00
// There are 2 versions in this example, one that uses Boxed Futures and the
// other that uses Impl Future, available since rustc v1.26.
2018-04-27 15:30:29 -04:00
#[macro_use]
extern crate validator_derive;
2019-03-09 18:03:09 -08:00
#[macro_use]
extern crate serde_derive;
2018-04-27 15:30:29 -04:00
use std::collections::HashMap;
2019-03-09 18:03:09 -08:00
use std::io;
2019-03-26 23:33:13 -07:00
use actix_web::client::Client;
2019-03-26 04:29:00 +01:00
use actix_web::web::BytesMut;
use actix_web::{web, App, Error, HttpResponse, HttpServer};
use futures::{Future, Stream};
2018-05-20 21:03:29 -07:00
use validator::Validate;
2018-04-27 15:30:29 -04:00
#[derive(Debug, Validate, Deserialize, Serialize)]
struct SomeData {
#[validate(length(min = "1", max = "1000000"))]
id: String,
#[validate(length(min = "1", max = "100"))]
name: String,
}
#[derive(Debug, Deserialize)]
struct HttpBinResponse {
args: HashMap<String, String>,
data: String,
files: HashMap<String, String>,
form: HashMap<String, String>,
headers: HashMap<String, String>,
2018-05-08 11:08:43 -07:00
json: SomeData,
2018-04-27 15:30:29 -04:00
origin: String,
2018-05-08 11:08:43 -07:00
url: String,
2018-04-27 15:30:29 -04:00
}
2018-05-11 08:05:03 -04:00
// -----------------------------------------------------------------------
2018-05-20 21:03:29 -07:00
// v1 uses Boxed Futures, which were the only option prior to rustc v1.26
2018-05-11 08:05:03 -04:00
// -----------------------------------------------------------------------
2018-04-27 15:30:29 -04:00
/// post json to httpbin, get it back in the response body, return deserialized
2019-03-26 23:33:13 -07:00
fn step_x_v1(
data: SomeData,
client: &Client,
) -> Box<Future<Item = SomeData, Error = Error>> {
2018-04-27 15:30:29 -04:00
Box::new(
2019-03-26 23:33:13 -07:00
client
.post("https://httpbin.org/post")
.send_json(data)
2019-03-09 18:03:09 -08:00
.map_err(Error::from) // <- convert SendRequestError to an Error
2019-03-26 04:29:00 +01:00
.and_then(|resp| {
resp // <- this is MessageBody type, resolves to complete body
2019-03-09 18:03:09 -08:00
.from_err() // <- convert PayloadError to an Error
2019-03-26 04:29:00 +01:00
.fold(BytesMut::new(), |mut acc, chunk| {
acc.extend_from_slice(&chunk);
Ok::<_, Error>(acc)
})
.map(|body| {
let body: HttpBinResponse =
2019-03-09 18:03:09 -08:00
serde_json::from_slice(&body).unwrap();
2019-03-26 04:29:00 +01:00
body.json
2018-04-27 15:30:29 -04:00
})
2019-03-09 18:03:09 -08:00
}),
2018-04-27 15:30:29 -04:00
)
}
2018-05-11 08:05:03 -04:00
fn create_something_v1(
2019-03-09 18:03:09 -08:00
some_data: web::Json<SomeData>,
2019-03-26 23:33:13 -07:00
client: web::Data<Client>,
2018-05-08 11:08:43 -07:00
) -> Box<Future<Item = HttpResponse, Error = Error>> {
2019-03-26 23:33:13 -07:00
Box::new(
step_x_v1(some_data.into_inner(), &client).and_then(move |some_data_2| {
step_x_v1(some_data_2, &client).and_then(move |some_data_3| {
step_x_v1(some_data_3, &client).and_then(|d| {
Ok(HttpResponse::Ok()
.content_type("application/json")
.body(serde_json::to_string(&d).unwrap())
.into())
})
2018-04-27 15:30:29 -04:00
})
2019-03-26 23:33:13 -07:00
}),
)
2018-04-27 15:30:29 -04:00
}
2018-05-11 08:05:03 -04:00
// ---------------------------------------------------------------
2018-05-20 21:03:29 -07:00
// v2 uses impl Future, available as of rustc v1.26
2018-05-11 08:05:03 -04:00
// ---------------------------------------------------------------
/// post json to httpbin, get it back in the response body, return deserialized
2019-03-26 23:33:13 -07:00
fn step_x_v2(
data: SomeData,
client: &Client,
) -> impl Future<Item = SomeData, Error = Error> {
client
.post("https://httpbin.org/post")
.send_json(data)
2019-03-09 18:03:09 -08:00
.map_err(Error::from) // <- convert SendRequestError to an Error
2019-03-26 04:29:00 +01:00
.and_then(|resp| {
resp.from_err()
.fold(BytesMut::new(), |mut acc, chunk| {
acc.extend_from_slice(&chunk);
Ok::<_, Error>(acc)
})
.map(|body| {
let body: HttpBinResponse = serde_json::from_slice(&body).unwrap();
body.json
2018-05-11 08:05:03 -04:00
})
2019-03-09 18:03:09 -08:00
})
2018-05-11 08:05:03 -04:00
}
2018-05-20 21:03:29 -07:00
fn create_something_v2(
2019-03-09 18:03:09 -08:00
some_data: web::Json<SomeData>,
2019-03-26 23:33:13 -07:00
client: web::Data<Client>,
2018-05-20 21:03:29 -07:00
) -> impl Future<Item = HttpResponse, Error = Error> {
2019-03-26 23:33:13 -07:00
step_x_v2(some_data.into_inner(), &client).and_then(move |some_data_2| {
step_x_v2(some_data_2, &client).and_then(move |some_data_3| {
step_x_v2(some_data_3, &client).and_then(|d| {
2018-05-20 21:03:29 -07:00
Ok(HttpResponse::Ok()
2018-05-11 08:05:03 -04:00
.content_type("application/json")
.body(serde_json::to_string(&d).unwrap())
2018-05-20 21:03:29 -07:00
.into())
2018-05-11 08:05:03 -04:00
})
})
2018-05-20 21:03:29 -07:00
})
2018-05-11 08:05:03 -04:00
}
2019-03-09 18:03:09 -08:00
fn main() -> io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info");
2018-04-27 15:30:29 -04:00
env_logger::init();
2019-03-09 18:03:09 -08:00
HttpServer::new(|| {
2018-05-20 21:03:29 -07:00
App::new()
2019-03-26 23:33:13 -07:00
.data(Client::default())
2019-03-09 18:03:09 -08:00
.service(
web::resource("/something_v1")
.route(web::post().to(create_something_v1)),
)
.service(
web::resource("/something_v2")
.route(web::post().to_async(create_something_v2)),
)
})
.bind("127.0.0.1:8088")?
.run()
2018-04-27 15:30:29 -04:00
}