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", "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]] [[package]]
name = "awc_https" name = "awc_https"
version = "1.0.0" version = "1.0.0"
@ -3229,6 +3216,21 @@ dependencies = [
"serde_json", "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]] [[package]]
name = "json_decode_error" name = "json_decode_error"
version = "1.0.0" version = "1.0.0"
@ -6712,9 +6714,9 @@ dependencies = [
[[package]] [[package]]
name = "validator" name = "validator"
version = "0.10.1" version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e60fadf92c22236de4028ceb0b8af50ed3430d41ad43d7a7d63b6bd1a8f47c38" checksum = "6d0f08911ab0fee2c5009580f04615fa868898ee57de10692a45da0c3bcc3e5e"
dependencies = [ dependencies = [
"idna", "idna",
"lazy_static", "lazy_static",
@ -6723,21 +6725,33 @@ dependencies = [
"serde_derive", "serde_derive",
"serde_json", "serde_json",
"url", "url",
"validator_types",
] ]
[[package]] [[package]]
name = "validator_derive" name = "validator_derive"
version = "0.10.1" version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d577dfb8ca9440a5c0b053d5a19b68f5c92ef57064bac87c8205c3f6072c20f" checksum = "d85135714dba11a1bd0b3eb1744169266f1a38977bf4e3ff5e2e1acb8c2b7eee"
dependencies = [ dependencies = [
"if_chain", "if_chain",
"lazy_static", "lazy_static",
"proc-macro-error",
"proc-macro2", "proc-macro2",
"quote", "quote",
"regex", "regex",
"syn", "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]] [[package]]

View File

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

View File

@ -12,16 +12,17 @@
// - POSTing json body // - POSTing json body
// 3. chaining futures into a single response used by an async endpoint // 3. chaining futures into a single response used by an async endpoint
use std::io;
use actix_web::{ use actix_web::{
error::ErrorBadRequest, error::ErrorBadRequest,
http::StatusCode, http::header::ContentType,
web::{self, BytesMut}, web::{self, BytesMut},
App, Error, HttpResponse, HttpServer, App, Error, HttpResponse, HttpServer,
}; };
use awc::Client; use awc::Client;
use futures::StreamExt; use futures::StreamExt;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::io;
use validator::Validate; use validator::Validate;
use validator_derive::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 /// validate data, post json to httpbin, get it back in the response body, return deserialized
async fn step_x( async fn step_x(data: SomeData, client: &Client) -> actix_web::Result<SomeData> {
data: SomeData,
client: &Client,
) -> Result<SomeData, actix_web::error::Error> {
// validate data // validate data
data.validate().map_err(ErrorBadRequest)?; data.validate().map_err(ErrorBadRequest)?;
@ -75,23 +73,22 @@ async fn create_something(
let d = step_x(some_data_3, &client).await?; let d = step_x(some_data_3, &client).await?;
Ok(HttpResponse::Ok() Ok(HttpResponse::Ok()
.content_type("application/json") .content_type(ContentType::json())
.body(serde_json::to_string(&d).unwrap())) .body(serde_json::to_string(&d).unwrap()))
} }
#[actix_web::main] #[actix_web::main]
async fn main() -> io::Result<()> { async fn main() -> io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info"); env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
env_logger::init();
let endpoint = "127.0.0.1:8080"; log::info!("starting HTTP serer at http://localhost:8080");
println!("Starting server at: {:?}", endpoint);
HttpServer::new(|| { HttpServer::new(|| {
App::new() App::new()
.app_data(web::Data::new(Client::default())) .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() .run()
.await .await
} }