-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
80 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters