1
0
mirror of https://github.com/actix/examples synced 2025-06-28 09:50:36 +02:00

Restructure folders (#411)

This commit is contained in:
Daniel T. Rodrigues
2021-02-25 21:57:58 -03:00
committed by GitHub
parent 9db98162b2
commit c3407627d0
334 changed files with 127 additions and 120 deletions

View File

@ -0,0 +1,14 @@
[package]
name = "awc_examples"
version = "2.0.0"
authors = ["dowwie <dkcdkg@gmail.com>"]
edition = "2018"
[dependencies]
actix-web = { version = "3", features = ["openssl"] }
env_logger = "0.8"
futures = "0.3.1"
serde = { version = "1.0.43", features = ["derive"] }
serde_json = "1.0.16"
validator = "0.10"
validator_derive = "0.10"

View File

@ -0,0 +1,19 @@
This is a contrived example intended to illustrate a few important 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
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
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
Example query from the command line using httpie:
```echo '{"id":"1", "name": "JohnDoe"}' | http 127.0.0.1:8080/something```

View File

@ -0,0 +1,98 @@
// 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
// 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
// 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 async endpoint
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::io;
use actix_web::{
client::Client,
error::ErrorBadRequest,
web::{self, BytesMut},
App, Error, HttpResponse, HttpServer,
};
use futures::StreamExt;
use validator::Validate;
use validator_derive::Validate;
#[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>,
json: SomeData,
origin: String,
url: String,
}
/// validate data, post json to httpbin, get it back in the response body, return deserialized
async fn step_x(data: SomeData, client: &Client) -> Result<SomeData, Error> {
// validate data
data.validate().map_err(ErrorBadRequest)?;
let mut res = client
.post("https://httpbin.org/post")
.send_json(&data)
.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?);
}
let body: HttpBinResponse = serde_json::from_slice(&body).unwrap();
Ok(body.json)
}
async fn create_something(
some_data: web::Json<SomeData>,
client: web::Data<Client>,
) -> 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()))
}
#[actix_web::main]
async fn main() -> io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();
let endpoint = "127.0.0.1:8080";
println!("Starting server at: {:?}", endpoint);
HttpServer::new(|| {
App::new()
.data(Client::default())
.service(web::resource("/something").route(web::post().to(create_something)))
})
.bind(endpoint)?
.run()
.await
}