diff --git a/core/http/src/handling/routes.rs b/core/http/src/handling/routes.rs index fd3512d0e6a5ab2f2ceb7a30eb70631c6af7c050..3d8b405d96dfa14760396a543b6543a80e4f9a65 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 0d90d268fb4618cc7fc8a8d4d150591dca47fc65..dd6cf0650036560bf186b6bfde56194c995f4afc 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(); }