]> git.seodisparate.com - EN605.607.81.SP22_ASDM_Project/commitdiff
Have backend always return JSON String
authorStephen Seo <seo.disparate@gmail.com>
Wed, 16 Mar 2022 04:09:07 +0000 (13:09 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Wed, 16 Mar 2022 04:09:07 +0000 (13:09 +0900)
back_end/Cargo.lock
back_end/Cargo.toml
back_end/src/main.rs

index 93786c0be07f3d0bf076d0156d0865847f10cc25..96f829b1d4f868fa757e2714b2f3d943dff61141 100644 (file)
@@ -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",
index dc40e30fe1838a8df1b3717e63f3dbda9f352132..a2da49b2bab8e0b06c06cea8c52f8f5e1603c4fe 100644 (file)
@@ -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"
index 65a9759ea980729576786a279eda248c4f0a1213..47ed010d030ec4ffde4b998862d5f451a06c6033 100644 (file)
@@ -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;