From 2e4d3b6196adc6ffa8b2eab295de2985494b2a72 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Tue, 9 Nov 2021 00:47:09 +0300 Subject: [PATCH] chore: fix hyper example --- examples/hyper/Cargo.toml | 4 ++-- examples/hyper/src/main.rs | 36 +++++++++++++++++++----------------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/examples/hyper/Cargo.toml b/examples/hyper/Cargo.toml index 9469aad..1640f5a 100644 --- a/examples/hyper/Cargo.toml +++ b/examples/hyper/Cargo.toml @@ -9,9 +9,9 @@ publish = false [dependencies] itconfig = { path = "../../itconfig", features = ["macro"] } -hyper = "0.14.4" +hyper = { version = "0.14.4", features = ["full"] } serde_json = "1.0.62" -tokio = { version = "1.2.0", features = ["macros"] } +tokio = { version = "1.2.0", features = ["macros", "rt-multi-thread"] } bytes = "1.0.1" futures-util = { version = "0.3.13", default-features = false } pretty_env_logger = "0.4.0" diff --git a/examples/hyper/src/main.rs b/examples/hyper/src/main.rs index 3f18589..44b5589 100644 --- a/examples/hyper/src/main.rs +++ b/examples/hyper/src/main.rs @@ -1,4 +1,4 @@ -use bytes::buf::BufExt; +use bytes::Buf; use futures_util::{stream, StreamExt}; use hyper::client::HttpConnector; use hyper::service::{make_service_fn, service_fn}; @@ -6,10 +6,10 @@ use hyper::{header, Body, Client, Method, Request, Response, Server, StatusCode} use itconfig::config; config! { - HYPER { + hyper { PREFER_SCHEMA: String => "http", - HOST < ( + static HOST < ( ADDR => "127.0.0.1", ":", PORT => 8000, @@ -25,19 +25,21 @@ config! { } type GenericError = Box; -type HyperResult = std::result::Result; +type Result = std::result::Result; -const INDEX: &'static [u8] = b"test.html"; -const INTERNAL_SERVER_ERROR: &'static [u8] = b"Internal Server Error"; -const NOTFOUND: &'static [u8] = b"Not Found"; -const POST_DATA: &'static str = r#"{"original": "data"}"#; +static INDEX: &[u8] = b"test.html"; +static INTERNAL_SERVER_ERROR: &[u8] = b"Internal Server Error"; +static NOT_FOUND: &[u8] = b"Not Found"; +static POST_DATA: &str = r#"{"original": "data"}"#; + +async fn client_request_response(client: &Client) -> Result> { + let url = format!("{}/json_api", config::hyper::HOST()); -async fn client_request_response(client: &Client) -> HyperResult> { let req = Request::builder() .method(Method::POST) - .uri(config::HYPER::JSON_API_URL()) + .uri(url) .header(header::CONTENT_TYPE, "application/json") - .body(Body::from(POST_DATA)) + .body(POST_DATA.into()) .unwrap(); let web_res = client.request(req).await?; @@ -55,7 +57,7 @@ async fn client_request_response(client: &Client) -> HyperResult< Ok(Response::new(body)) } -async fn api_post_response(req: Request) -> HyperResult> { +async fn api_post_response(req: Request) -> Result> { // Aggregate the body... let whole_body = hyper::body::aggregate(req).await?; // Decode as JSON... @@ -71,7 +73,7 @@ async fn api_post_response(req: Request) -> HyperResult> { Ok(response) } -async fn api_get_response() -> HyperResult> { +async fn api_get_response() -> Result> { let data = vec!["foo", "bar"]; let res = match serde_json::to_string(&data) { Ok(json) => Response::builder() @@ -89,7 +91,7 @@ async fn api_get_response() -> HyperResult> { async fn response_examples( req: Request, client: Client, -) -> HyperResult> { +) -> Result> { match (req.method(), req.uri().path()) { (&Method::GET, "/") | (&Method::GET, "/index.html") => Ok(Response::new(INDEX.into())), (&Method::GET, "/test.html") => client_request_response(&client).await, @@ -99,18 +101,18 @@ async fn response_examples( // Return 404 not found response. Ok(Response::builder() .status(StatusCode::NOT_FOUND) - .body(Body::from(NOTFOUND)) + .body(NOT_FOUND.into()) .unwrap()) } } } #[tokio::main] -async fn main() -> HyperResult<()> { +async fn main() -> Result<()> { config::init(); pretty_env_logger::init(); - let addr = config::HYPER::HOST().parse().unwrap(); + let addr = config::hyper::HOST().parse().unwrap(); // Share a `Client` with all `Service`s let client = Client::new();