fix(examples): fix types and add cfg::init

This commit is contained in:
Dmitriy Pleshevskiy 2020-01-12 16:52:03 +03:00
parent 8f967b4c37
commit d3cb855574

View file

@ -1,5 +1,3 @@
#![deny(warnings)]
#[macro_use] #[macro_use]
extern crate itconfig; extern crate itconfig;
@ -12,6 +10,8 @@ use hyper::{header, Body, Client, Method, Request, Response, Server, StatusCode}
config! { config! {
HYPER { HYPER {
PREFER_SCHEMA: String => "http",
HOST < ( HOST < (
ADDR => "127.0.0.1", ADDR => "127.0.0.1",
":", ":",
@ -19,7 +19,8 @@ config! {
), ),
JSON_API_URL < ( JSON_API_URL < (
"http://", HYPER_PREFER_SCHEMA,
"://",
HYPER_HOST, HYPER_HOST,
"/json_api", "/json_api",
), ),
@ -31,10 +32,10 @@ type GenericError = Box<dyn std::error::Error + Send + Sync>;
type HyperResult<T> = std::result::Result<T, GenericError>; type HyperResult<T> = std::result::Result<T, GenericError>;
static INDEX: &[u8] = b"<a href=\"test.html\">test.html</a>"; const INDEX: &'static [u8] = b"<a href=\"test.html\">test.html</a>";
static INTERNAL_SERVER_ERROR: &[u8] = b"Internal Server Error"; const INTERNAL_SERVER_ERROR: &'static [u8] = b"Internal Server Error";
static NOTFOUND: &[u8] = b"Not Found"; const NOTFOUND: &'static [u8] = b"Not Found";
static POST_DATA: &str = r#"{"original": "data"}"#; const POST_DATA: &'static str = r#"{"original": "data"}"#;
async fn client_request_response(client: &Client<HttpConnector>) -> HyperResult<Response<Body>> { async fn client_request_response(client: &Client<HttpConnector>) -> HyperResult<Response<Body>> {
@ -42,7 +43,7 @@ async fn client_request_response(client: &Client<HttpConnector>) -> HyperResult<
.method(Method::POST) .method(Method::POST)
.uri(cfg::HYPER::JSON_API_URL()) .uri(cfg::HYPER::JSON_API_URL())
.header(header::CONTENT_TYPE, "application/json") .header(header::CONTENT_TYPE, "application/json")
.body(POST_DATA.into()) .body(Body::from(POST_DATA))
.unwrap(); .unwrap();
let web_res = client.request(req).await?; let web_res = client.request(req).await?;
@ -104,7 +105,7 @@ async fn response_examples(
// Return 404 not found response. // Return 404 not found response.
Ok(Response::builder() Ok(Response::builder()
.status(StatusCode::NOT_FOUND) .status(StatusCode::NOT_FOUND)
.body(NOTFOUND.into()) .body(Body::from(NOTFOUND))
.unwrap()) .unwrap())
} }
} }
@ -112,6 +113,7 @@ async fn response_examples(
#[tokio::main] #[tokio::main]
async fn main() -> HyperResult<()> { async fn main() -> HyperResult<()> {
cfg::init();
pretty_env_logger::init(); pretty_env_logger::init();
let addr = cfg::HYPER::HOST().parse().unwrap(); let addr = cfg::HYPER::HOST().parse().unwrap();