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

95 lines
2.9 KiB
Rust
Raw Normal View History

2022-02-06 08:13:24 +00:00
// This is a contrived example intended to illustrate Actix Web features.
2018-04-27 15:30:29 -04:00
// *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.
//
2022-02-06 08:13:24 +00:00
// Actix Web features illustrated here include:
2018-04-27 15:30:29 -04:00
// 1. handling json input param
// 2. validating user-submitted parameters using the 'validator' crate
2022-02-06 08:13:24 +00:00
// 2. `awc` client features:
2018-04-27 15:30:29 -04:00
// - POSTing json body
// 3. chaining futures into a single response used by an async endpoint
2022-02-02 01:51:25 +00:00
use std::io;
use actix_web::{
error::ErrorBadRequest,
2022-02-02 01:51:25 +00:00
http::header::ContentType,
2019-06-07 13:30:14 -07:00
web::{self, BytesMut},
App, Error, HttpResponse, HttpServer,
};
use awc::Client;
2019-12-07 23:59:24 +06:00
use futures::StreamExt;
use serde::{Deserialize, Serialize};
2018-05-20 21:03:29 -07:00
use validator::Validate;
use validator_derive::Validate;
2018-04-27 15:30:29 -04:00
#[derive(Debug, Validate, Deserialize, Serialize)]
struct SomeData {
2020-04-05 02:26:11 +09:00
#[validate(length(min = 1, max = 1000000))]
2018-04-27 15:30:29 -04:00
id: String,
2020-04-05 02:26:11 +09:00
#[validate(length(min = 1, max = 100))]
2018-04-27 15:30:29 -04:00
name: String,
}
2022-02-02 00:44:58 +00:00
#[allow(dead_code)] // it is debug printed
2018-04-27 15:30:29 -04:00
#[derive(Debug, Deserialize)]
struct HttpBinResponse {
2018-05-08 11:08:43 -07:00
json: SomeData,
2018-04-27 15:30:29 -04:00
}
/// validate data, post json to httpbin, get it back in the response body, return deserialized
2022-02-02 01:51:25 +00:00
async fn step_x(data: SomeData, client: &Client) -> actix_web::Result<SomeData> {
2019-12-07 23:59:24 +06:00
// validate data
data.validate().map_err(ErrorBadRequest)?;
let mut res = client
2019-03-26 23:33:13 -07:00
.post("https://httpbin.org/post")
.send_json(&data)
2019-12-07 23:59:24 +06:00
.await
// <- convert SendRequestError to an InternalError, a type that implements the ResponseError trait
.map_err(actix_web::error::ErrorInternalServerError)?; // <- convert it into an actix_web::Error
2019-12-07 23:59:24 +06:00
let mut body = BytesMut::new();
while let Some(chunk) = res.next().await {
body.extend_from_slice(&chunk?);
}
2019-12-07 23:59:24 +06:00
let body: HttpBinResponse = serde_json::from_slice(&body).unwrap();
2022-02-02 00:44:58 +00:00
println!("{:?}", body);
2019-12-07 23:59:24 +06:00
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()
2022-02-02 01:51:25 +00:00
.content_type(ContentType::json())
2019-12-07 23:59:24 +06:00
.body(serde_json::to_string(&d).unwrap()))
2018-05-11 08:05:03 -04:00
}
2020-09-12 16:49:45 +01:00
#[actix_web::main]
2019-12-07 23:59:24 +06:00
async fn main() -> io::Result<()> {
2022-02-02 01:51:25 +00:00
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
2022-02-06 08:19:35 +00:00
log::info!("starting HTTP server at http://localhost:8080");
2018-04-27 15:30:29 -04:00
2019-03-09 18:03:09 -08:00
HttpServer::new(|| {
2019-12-07 23:59:24 +06:00
App::new()
.app_data(web::Data::new(Client::default()))
2022-02-02 01:51:25 +00:00
.service(web::resource("/").route(web::post().to(create_something)))
2019-03-09 18:03:09 -08:00
})
2022-02-02 01:51:25 +00:00
.bind(("127.0.0.1", 8080))?
2019-12-25 20:48:33 +04:00
.run()
2019-12-07 23:59:24 +06:00
.await
2018-04-27 15:30:29 -04:00
}