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
|
2019-06-06 15:30:54 -07:00
|
|
|
// 3. chaining futures into a single response used by an async endpoint
|
2019-04-09 10:36:37 -04:00
|
|
|
|
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-06-06 15:30:54 -07:00
|
|
|
use actix_web::{
|
|
|
|
client::Client,
|
|
|
|
error::ErrorBadRequest,
|
2019-06-07 13:30:14 -07:00
|
|
|
web::{self, BytesMut},
|
2019-06-06 15:30:54 -07:00
|
|
|
App, Error, HttpResponse, HttpServer,
|
|
|
|
};
|
2019-12-07 23:59:24 +06:00
|
|
|
use futures::StreamExt;
|
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
|
|
|
}
|
|
|
|
|
2019-06-06 15:30:54 -07:00
|
|
|
/// validate data, post json to httpbin, get it back in the response body, return deserialized
|
2019-12-07 23:59:24 +06:00
|
|
|
async fn step_x(data: SomeData, client: &Client) -> Result<SomeData, Error> {
|
|
|
|
// validate data
|
|
|
|
data.validate().map_err(ErrorBadRequest)?;
|
|
|
|
|
|
|
|
let mut res = client
|
2019-03-26 23:33:13 -07:00
|
|
|
.post("https://httpbin.org/post")
|
2019-04-04 14:38:07 +08:00
|
|
|
.send_json(&data)
|
2019-12-07 23:59:24 +06:00
|
|
|
.await
|
|
|
|
.map_err(Error::from)?; // <- convert SendRequestError to an Error
|
|
|
|
|
|
|
|
let mut body = BytesMut::new();
|
|
|
|
while let Some(chunk) = res.next().await {
|
|
|
|
body.extend_from_slice(&chunk?);
|
|
|
|
}
|
2019-06-06 15:30:54 -07:00
|
|
|
|
2019-12-07 23:59:24 +06:00
|
|
|
let body: HttpBinResponse = serde_json::from_slice(&body).unwrap();
|
|
|
|
Ok(body.json)
|
2018-05-11 08:05:03 -04:00
|
|
|
}
|
|
|
|
|
2019-12-07 23:59:24 +06:00
|
|
|
async fn create_something(
|
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>,
|
2019-12-07 23:59:24 +06:00
|
|
|
) -> Result<HttpResponse, Error> {
|
|
|
|
let some_data_2 = step_x(some_data.into_inner(), &client).await?;
|
|
|
|
let some_data_3 = step_x(some_data_2, &client).await?;
|
|
|
|
let d = step_x(some_data_3, &client).await?;
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok()
|
|
|
|
.content_type("application/json")
|
|
|
|
.body(serde_json::to_string(&d).unwrap()))
|
2018-05-11 08:05:03 -04:00
|
|
|
}
|
|
|
|
|
2019-12-07 23:59:24 +06:00
|
|
|
#[actix_rt::main]
|
|
|
|
async fn main() -> io::Result<()> {
|
2019-03-09 18:03:09 -08:00
|
|
|
std::env::set_var("RUST_LOG", "actix_web=info");
|
2018-04-27 15:30:29 -04:00
|
|
|
env_logger::init();
|
2019-04-09 10:36:37 -04:00
|
|
|
let endpoint = "127.0.0.1:8080";
|
2018-04-27 15:30:29 -04:00
|
|
|
|
2019-04-09 10:36:37 -04:00
|
|
|
println!("Starting server at: {:?}", endpoint);
|
2019-03-09 18:03:09 -08:00
|
|
|
HttpServer::new(|| {
|
2019-12-07 23:59:24 +06:00
|
|
|
App::new()
|
|
|
|
.data(Client::default())
|
|
|
|
.service(web::resource("/something").route(web::post().to(create_something)))
|
2019-03-09 18:03:09 -08:00
|
|
|
})
|
2019-04-09 10:36:37 -04:00
|
|
|
.bind(endpoint)?
|
2019-12-07 23:59:24 +06:00
|
|
|
.start()
|
|
|
|
.await
|
2018-04-27 15:30:29 -04:00
|
|
|
}
|