1
0
mirror of https://github.com/actix/examples synced 2024-11-23 22:41:07 +01:00

clean up json-validation

This commit is contained in:
Rob Ede 2022-02-02 01:51:25 +00:00
parent 5f3bdfcbe1
commit 6058625ecb
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
3 changed files with 45 additions and 32 deletions

50
Cargo.lock generated
View File

@ -1315,19 +1315,6 @@ dependencies = [
"tokio 1.16.1",
]
[[package]]
name = "awc_examples"
version = "1.0.0"
dependencies = [
"actix-web 3.3.3",
"env_logger 0.8.4",
"futures",
"serde 1.0.136",
"serde_json",
"validator",
"validator_derive",
]
[[package]]
name = "awc_https"
version = "1.0.0"
@ -3229,6 +3216,21 @@ dependencies = [
"serde_json",
]
[[package]]
name = "json-validation"
version = "1.0.0"
dependencies = [
"actix-web 4.0.0-rc.1",
"awc 3.0.0-beta.20",
"env_logger 0.9.0",
"futures",
"log",
"serde 1.0.136",
"serde_json",
"validator",
"validator_derive",
]
[[package]]
name = "json_decode_error"
version = "1.0.0"
@ -6712,9 +6714,9 @@ dependencies = [
[[package]]
name = "validator"
version = "0.10.1"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e60fadf92c22236de4028ceb0b8af50ed3430d41ad43d7a7d63b6bd1a8f47c38"
checksum = "6d0f08911ab0fee2c5009580f04615fa868898ee57de10692a45da0c3bcc3e5e"
dependencies = [
"idna",
"lazy_static",
@ -6723,21 +6725,33 @@ dependencies = [
"serde_derive",
"serde_json",
"url",
"validator_types",
]
[[package]]
name = "validator_derive"
version = "0.10.1"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d577dfb8ca9440a5c0b053d5a19b68f5c92ef57064bac87c8205c3f6072c20f"
checksum = "d85135714dba11a1bd0b3eb1744169266f1a38977bf4e3ff5e2e1acb8c2b7eee"
dependencies = [
"if_chain",
"lazy_static",
"proc-macro-error",
"proc-macro2",
"quote",
"regex",
"syn",
"validator",
"validator_types",
]
[[package]]
name = "validator_types"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ded9d97e1d42327632f5f3bae6403c04886e2de3036261ef42deebd931a6a291"
dependencies = [
"proc-macro2",
"syn",
]
[[package]]

View File

@ -1,13 +1,15 @@
[package]
name = "awc_examples"
name = "json-validation"
version = "1.0.0"
edition = "2021"
[dependencies]
actix-web = { version = "4.0.0-beta.21", features = ["openssl"] }
awc = "3.0.0-beta.19"
env_logger = "0.9"
futures = "0.3.1"
log = "0.4"
serde = { version = "1.0.43", features = ["derive"] }
serde_json = "1.0.16"
validator = "0.14"

View File

@ -12,16 +12,17 @@
// - POSTing json body
// 3. chaining futures into a single response used by an async endpoint
use std::io;
use actix_web::{
error::ErrorBadRequest,
http::StatusCode,
http::header::ContentType,
web::{self, BytesMut},
App, Error, HttpResponse, HttpServer,
};
use awc::Client;
use futures::StreamExt;
use serde::{Deserialize, Serialize};
use std::io;
use validator::Validate;
use validator_derive::Validate;
@ -40,10 +41,7 @@ struct HttpBinResponse {
}
/// 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, actix_web::error::Error> {
async fn step_x(data: SomeData, client: &Client) -> actix_web::Result<SomeData> {
// validate data
data.validate().map_err(ErrorBadRequest)?;
@ -75,23 +73,22 @@ async fn create_something(
let d = step_x(some_data_3, &client).await?;
Ok(HttpResponse::Ok()
.content_type("application/json")
.content_type(ContentType::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";
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
log::info!("starting HTTP serer at http://localhost:8080");
println!("Starting server at: {:?}", endpoint);
HttpServer::new(|| {
App::new()
.app_data(web::Data::new(Client::default()))
.service(web::resource("/something").route(web::post().to(create_something)))
.service(web::resource("/").route(web::post().to(create_something)))
})
.bind(endpoint)?
.bind(("127.0.0.1", 8080))?
.run()
.await
}