Skip to content

Commit

Permalink
[ISSUES#83] extra body param part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
847850277 committed Nov 30, 2024
1 parent 3a90de3 commit e3bf9da
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
[workspace]
members = [
"mario-core", "mario-macro",
"mario-rs-examples", "sync-core", "other-examples", "sync-examples", "hyper-examples", "axum-like", "axum-like-examples", "axum-examples",
"mario-rs-examples", "sync-core", "other-examples", "sync-examples", "hyper-examples", "axum-like", "axum-like-examples", "axum-examples", "hyper-examples-1",
]
resolver = "2"

Expand Down
10 changes: 10 additions & 0 deletions hyper-examples-1/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "hyper-examples-1"
version.workspace = true
edition.workspace = true

[dependencies]
tokio = { version = "1", features = ["full"] }
hyper = { version = "0.14", features = ["full"] }
http-body = "0.4"
bytes = "1"
14 changes: 14 additions & 0 deletions hyper-examples-1/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub fn add(left: u64, right: u64) -> u64 {
left + right
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
37 changes: 37 additions & 0 deletions hyper-examples-1/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use bytes::Bytes;
use http_body::Body as HttpBody;
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Request, Response, Server};
use tokio::runtime::Runtime;

async fn handle_request(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
let mut body = req.into_body();
let mut data = Vec::new();

while let Some(chunk) = body.data().await {
let chunk = chunk?;
data.extend_from_slice(&chunk);
}

let body_string = String::from_utf8_lossy(&data);
println!("Received body: {}", body_string);

Ok(Response::new(Body::from("Received")))
}

fn main() {
let rt = Runtime::new().unwrap();
rt.block_on(async {
let make_svc =
make_service_fn(|_conn| async { Ok::<_, hyper::Error>(service_fn(handle_request)) });

let addr = ([127, 0, 0, 1], 8080).into();
let server = Server::bind(&addr).serve(make_svc);

println!("Listening on http://{}", addr);

if let Err(e) = server.await {
eprintln!("server error: {}", e);
}
});
}
2 changes: 0 additions & 2 deletions mario-core/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use crate::error::Error;
use crate::request::Request;
use crate::response::Response;
use std::fmt::Debug;
use std::future::Future;
use std::pin::Pin;

pub trait Endpoint: Debug + Sync + Send {
fn call(&self, req: &Request) -> Result<Response<String>, Error>;
Expand Down
19 changes: 18 additions & 1 deletion mario-rs-examples/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use http_body_util::Full;
use http_body_util::{BodyExt, Full};
use log::info;
use mario_core::error::Error;
use mario_core::extra::{FailedToDeserializeQueryString, Query};
Expand All @@ -9,8 +9,11 @@ use mario_core::server::Server;
use mario_core::service::Service;
use mario_macro::handler;
use std::future::Future;
use std::ops::Deref;
use std::pin::Pin;
use std::sync::Arc;
use warp::hyper;
use warp::hyper::body;
use warp::hyper::body::Bytes;

async fn example() -> Response<String> {
Expand Down Expand Up @@ -129,6 +132,18 @@ impl Endpoint for ExtraMulti_2Example {
}
}

#[derive(Debug, Default)]
pub struct ExtraPostExample;

impl Endpoint for ExtraPostExample {
fn call(&self, req: &mario_core::request::Request) -> Result<Response<String>, Error> {
dbg!(req);
let body = &req.body;
dbg!(body);
Ok(Response::new("not param".to_string()))
}
}

#[tokio::main]
pub async fn main() {
// init trace log
Expand All @@ -151,6 +166,8 @@ pub async fn main() {

router.get("/hello_world_3", Arc::new(world));

router.post("/hello_world_param", Arc::new(ExtraPostExample));

server.service.set_routes(router);

server.start_server().await;
Expand Down

0 comments on commit e3bf9da

Please sign in to comment.