Have backend always return JSON String
This commit is contained in:
parent
97ca4adecc
commit
b9921e6f47
3 changed files with 65 additions and 5 deletions
49
back_end/Cargo.lock
generated
49
back_end/Cargo.lock
generated
|
@ -133,11 +133,28 @@ dependencies = [
|
|||
name = "four_line_dropper_backend"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"futures",
|
||||
"serde_json",
|
||||
"tokio",
|
||||
"warp",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures"
|
||||
version = "0.3.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f73fe65f54d1e12b726f517d3e2135ca3125a437b6d998caf1962961f7172d9e"
|
||||
dependencies = [
|
||||
"futures-channel",
|
||||
"futures-core",
|
||||
"futures-executor",
|
||||
"futures-io",
|
||||
"futures-sink",
|
||||
"futures-task",
|
||||
"futures-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures-channel"
|
||||
version = "0.3.21"
|
||||
|
@ -154,6 +171,34 @@ version = "0.3.21"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0c09fd04b7e4073ac7156a9539b57a484a8ea920f79c7c675d05d289ab6110d3"
|
||||
|
||||
[[package]]
|
||||
name = "futures-executor"
|
||||
version = "0.3.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9420b90cfa29e327d0429f19be13e7ddb68fa1cccb09d65e5706b8c7a749b8a6"
|
||||
dependencies = [
|
||||
"futures-core",
|
||||
"futures-task",
|
||||
"futures-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures-io"
|
||||
version = "0.3.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fc4045962a5a5e935ee2fdedaa4e08284547402885ab326734432bed5d12966b"
|
||||
|
||||
[[package]]
|
||||
name = "futures-macro"
|
||||
version = "0.3.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "33c1e13800337f4d4d7a316bf45a567dbcb6ffe087f16424852d97e97a91f512"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures-sink"
|
||||
version = "0.3.21"
|
||||
|
@ -172,9 +217,13 @@ version = "0.3.21"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d8b7abd5d659d9b90c8cba917f6ec750a74e2dc23902ef9cd4cc8c8b22e6036a"
|
||||
dependencies = [
|
||||
"futures-channel",
|
||||
"futures-core",
|
||||
"futures-io",
|
||||
"futures-macro",
|
||||
"futures-sink",
|
||||
"futures-task",
|
||||
"memchr",
|
||||
"pin-project-lite",
|
||||
"pin-utils",
|
||||
"slab",
|
||||
|
|
|
@ -9,3 +9,5 @@ edition = "2021"
|
|||
tokio = { version = "1", features = ["full"] }
|
||||
warp = "0.3"
|
||||
serde_json = "1.0"
|
||||
bytes = "1.1"
|
||||
futures = "0.3.21"
|
||||
|
|
|
@ -1,15 +1,24 @@
|
|||
mod json_handlers;
|
||||
|
||||
use serde_json::Value;
|
||||
use warp::Filter;
|
||||
use warp::Rejection;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let route = warp::body::content_length_limit(1024 * 32)
|
||||
.and(warp::body::json())
|
||||
.map(|json_value: Value| {
|
||||
let result = json_handlers::handle_json(json_value);
|
||||
result.unwrap_or_else(|e| e)
|
||||
.and(warp::body::bytes())
|
||||
.and_then(|bytes: bytes::Bytes| async move {
|
||||
let body_str_result = std::str::from_utf8(bytes.as_ref());
|
||||
if let Ok(body_str) = body_str_result {
|
||||
let json_result = serde_json::from_str(body_str);
|
||||
if let Ok(json_value) = json_result {
|
||||
Ok(json_handlers::handle_json(json_value).unwrap_or_else(|e| e))
|
||||
} else {
|
||||
Ok(String::from("{\"type\": \"invalid_syntax\"}"))
|
||||
}
|
||||
} else {
|
||||
Ok::<String, Rejection>(String::from("{\"type\": \"invalid_syntax\"}"))
|
||||
}
|
||||
});
|
||||
|
||||
warp::serve(route).run(([0, 0, 0, 0], 1237)).await;
|
||||
|
|
Loading…
Reference in a new issue