Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate serde;
- extern crate serde_json;
- extern crate futures;
- extern crate hyper;
- extern crate shio;
- use futures::{Future, Stream};
- use shio::prelude::*;
- use hyper::Client;
- #[macro_use]
- extern crate error_chain;
- #[macro_use]
- extern crate serde_derive;
- mod errors {
- error_chain! {
- foreign_links {
- Json(::serde_json::Error);
- Shio(::shio::errors::ListenError);
- Hyper(::hyper::Error);
- }
- }
- }
- #[derive(Debug, Deserialize)]
- struct RequestBody {
- addr: String,
- }
- #[derive(Debug, Deserialize)]
- struct IpJson {
- origin: String,
- }
- fn proxy(ctx: Context) -> BoxFuture<Response, hyper::Error> {
- // Additional work can be scheduled on the thread-local event loop,
- // as each handler receives a reference to it
- let client = Client::new(ctx.handle());
- ctx.body()
- .concat2()
- .from_err()
- .and_then(|data| {
- let body: RequestBody = serde_json::from_slice(&data)?;
- let url = body.addr.parse()?;
- Ok(url)
- })
- .and_then(move |url| client.get(url))
- .and_then(|res| res.body().concat2())
- .and_then(|body| {
- let json: IpJson = serde_json::from_slice(&body)?;
- Ok(Response::build().body(json.origin))
- })
- .into_box()
- }
- fn main() {
- Shio::new(proxy).run(":7878").unwrap();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement