From 0c607be9fbb3538df5f52109cedb9ba30ef380fd Mon Sep 17 00:00:00 2001
From: Darius Auding <Darius.auding@gmx.de>
Date: Mon, 22 May 2023 22:47:26 +0200
Subject: [PATCH] add a post handler

---
 core/http/src/handling/routes.rs |  6 ++++++
 site/src/main.rs                 | 35 +++++++++++++++++++++++++++++++-
 2 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/core/http/src/handling/routes.rs b/core/http/src/handling/routes.rs
index fd3512d..3d8b405 100644
--- a/core/http/src/handling/routes.rs
+++ b/core/http/src/handling/routes.rs
@@ -69,3 +69,9 @@ pub struct Data {
     pub buffer: Vec<u8>,
     pub is_complete: bool,
 }
+
+impl Data {
+    pub fn is_empty(&self) -> bool {
+        self.buffer.len() == 0
+    }
+}
diff --git a/site/src/main.rs b/site/src/main.rs
index 0d90d26..dd6cf06 100644
--- a/site/src/main.rs
+++ b/site/src/main.rs
@@ -28,6 +28,30 @@ fn fileserver(path: &str) -> Result<NamedFile, Status> {
     NamedFile::open(PathBuf::from("static/".to_string() + path))
 }
 
+fn post_hi_handler(_request: Request, data: Data) -> Outcome<Response, Status, Data> {
+    if data.is_empty() {
+        return Outcome::Forward(data);
+    }
+    let data = if let Ok(str) = String::from_utf8(data.buffer) {
+        str
+    } else {
+        return Outcome::Failure(Status::BadRequest);
+    };
+    let dat = post_hi(data);
+    Outcome::Success(Response {
+        headers: vec![
+            format!("Content-Length: {}", dat.len()),
+            format!("Content-Type: text/plain"),
+        ],
+        status: Some(Status::Ok),
+        body: Box::new(dat),
+    })
+}
+
+fn post_hi(msg: String) -> String {
+    msg
+}
+
 fn main() {
     let fileserver = Route {
         format: None,
@@ -38,7 +62,16 @@ fn main() {
         rank: 0,
     };
 
+    let post_test = Route {
+        format: None,
+        handler: post_hi_handler,
+        name: Some("post_test"),
+        uri: "post",
+        method: Method::Post,
+        rank: 0,
+    };
+
     http::build("127.0.0.1:8000")
-        .mount("/", vec![fileserver])
+        .mount("/", vec![fileserver, post_test])
         .launch();
 }
-- 
GitLab