Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. extern crate serde;
  2. extern crate serde_json;
  3.  
  4. extern crate futures;
  5. extern crate hyper;
  6. extern crate shio;
  7. use futures::{Future, Stream};
  8.  
  9. use shio::prelude::*;
  10. use hyper::Client;
  11.  
  12. #[macro_use]
  13. extern crate error_chain;
  14.  
  15. #[macro_use]
  16. extern crate serde_derive;
  17.  
  18. mod errors {
  19. error_chain! {
  20. foreign_links {
  21. Json(::serde_json::Error);
  22. Shio(::shio::errors::ListenError);
  23. Hyper(::hyper::Error);
  24. }
  25. }
  26. }
  27.  
  28. #[derive(Debug, Deserialize)]
  29. struct RequestBody {
  30. addr: String,
  31. }
  32.  
  33. #[derive(Debug, Deserialize)]
  34. struct IpJson {
  35. origin: String,
  36. }
  37.  
  38.  
  39. fn proxy(ctx: Context) -> BoxFuture<Response, hyper::Error> {
  40. // Additional work can be scheduled on the thread-local event loop,
  41. // as each handler receives a reference to it
  42. let client = Client::new(ctx.handle());
  43. ctx.body()
  44. .concat2()
  45. .from_err()
  46. .and_then(|data| {
  47. let body: RequestBody = serde_json::from_slice(&data)?;
  48. let url = body.addr.parse()?;
  49. Ok(url)
  50. })
  51. .and_then(move |url| client.get(url))
  52. .and_then(|res| res.body().concat2())
  53. .and_then(|body| {
  54. let json: IpJson = serde_json::from_slice(&body)?;
  55. Ok(Response::build().body(json.origin))
  56. })
  57. .into_box()
  58. }
  59.  
  60. fn main() {
  61. Shio::new(proxy).run(":7878").unwrap();
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement