Skip to content
Snippets Groups Projects
Commit 0c607be9 authored by codecraft's avatar codecraft :crocodile:
Browse files

add a post handler

parent 87479800
No related branches found
No related tags found
1 merge request!1Initial feature merge
......@@ -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
}
}
......@@ -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();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment