Created
June 30, 2019 23:38
-
-
Save nirasan/4becf84eccc2bdada838a9633fb60fb7 to your computer and use it in GitHub Desktop.
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
use hyper::service::service_fn_ok; | |
use hyper::{Body, Request, Response, Server, Client}; | |
use std::io::{self, Write}; | |
use hyper::rt::{self, Stream}; | |
use futures::future::{self, Future}; | |
const PHRASE: &str = "Hello, World!"; | |
fn hello_world(_req: Request<Body>) -> Response<Body> { | |
rt::spawn(rt::lazy(|| { | |
let client = Client::new(); | |
let uri = "http://httpbin.org/ip".parse().unwrap(); | |
client | |
.get(uri) | |
.map_err(|err| { | |
println!("Error: {}", err); | |
}) | |
.and_then(|res| { | |
res | |
.into_body() | |
.concat2() | |
.map_err(|err| { | |
println!("Error: {}", err); | |
}) | |
.map(|chunk| { | |
let v = chunk.to_vec(); | |
let s = String::from_utf8_lossy(&v).to_string(); | |
println!("Body {}", s); | |
}) | |
}) | |
})); | |
Response::new(Body::from(PHRASE)) | |
} | |
fn main() { | |
// This is our socket address... | |
let addr = ([127, 0, 0, 1], 3000).into(); | |
// A `Service` is needed for every connection, so this | |
// creates one from our `hello_world` function. | |
let new_svc = || { | |
// service_fn_ok converts our function into a `Service` | |
service_fn_ok(hello_world) | |
}; | |
let server = Server::bind(&addr) | |
.serve(new_svc) | |
.map_err(|e| eprintln!("server error: {}", e)); | |
// Run this server for... forever! | |
hyper::rt::run(server); | |
} |
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
[package] | |
name = "hyper-test" | |
version = "0.1.0" | |
authors = ["nirasan <niranirao@gmail.com>"] | |
edition = "2018" | |
[dependencies] | |
hyper = "0.12" | |
reqwest = "0.9" | |
tokio = "0.1.21" | |
futures = "0.1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment