Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. extern crate hyper; // 0.12.24
  2. extern crate futures; // 0.1.25
  3.  
  4. use futures::{Future, future};
  5. use hyper::{Request, Response, Body, body::Payload};
  6. use hyper::server::conn::AddrStream;
  7. use hyper::service::{MakeService, Service};
  8. use std::net::{IpAddr, Ipv4Addr, SocketAddr};
  9. use std::io;
  10.  
  11. // Base
  12.  
  13. struct BaseService(SocketAddr);
  14.  
  15. impl Service for BaseService {
  16. type ReqBody = hyper::Body;
  17. type ResBody = hyper::Body;
  18. type Error = io::Error;
  19. type Future = Box<Future<Item=Response<Self::ResBody>, Error=Self::Error> + Send>;
  20.  
  21. fn call(&mut self, _: Request<Self::ReqBody>) -> Self::Future {
  22. Box::new(future::ok(Response::new(Body::from(format!("Remote: {}", self.0)))))
  23. }
  24.  
  25. }
  26.  
  27. struct BaseMakeService {
  28.  
  29. }
  30.  
  31. impl MakeService<&AddrStream> for BaseMakeService {
  32. type ReqBody = hyper::Body;
  33. type ResBody = hyper::Body;
  34. type Error = io::Error;
  35. type MakeError = io::Error;
  36. type Service = BaseService;
  37. type Future = Box<Future<Item=Self::Service, Error=Self::MakeError> + Send>;
  38.  
  39. fn make_service(&mut self, ctx: &AddrStream) -> Self::Future {
  40. Box::new(future::ok(BaseService(ctx.remote_addr())))
  41. }
  42. }
  43.  
  44. // Wrapper
  45.  
  46. struct WrapperService<T>(T);
  47.  
  48. impl<T> Service for WrapperService<T> where
  49. T: Service,
  50. T::Future: Future<Item=Response<T::ResBody>, Error=T::Error> + Send + 'static
  51. {
  52. type ReqBody = T::ReqBody;
  53. type ResBody = T::ResBody;
  54. type Error = T::Error;
  55. type Future = Box<Future<Item=Response<Self::ResBody>, Error=Self::Error> + Send>;
  56.  
  57. fn call(&mut self, req: Request<Self::ReqBody>) -> Self::Future {
  58. Box::new(self.0.call(req))
  59. }
  60. }
  61.  
  62. struct WrapperMakeService<T>(T);
  63.  
  64. impl<T, Ctx, E, ME, S, IB, OB, F> MakeService<&Ctx> for WrapperMakeService<T> where
  65. T: for<'a> MakeService<&'a Ctx, Error=E, MakeError=ME, Service=S, ReqBody=IB, ResBody=OB, Future=F>,
  66. S: Service<Error=E, ReqBody=IB, ResBody=OB> + 'static,
  67. ME: Into<Box<std::error::Error + Send + Sync>>,
  68. E: Into<Box<std::error::Error + Send + Sync>>,
  69. F: Send + Future<Item=S, Error=ME> + 'static,
  70. S::Future: Send,
  71. IB: Payload,
  72. OB: Payload,
  73. {
  74. type ReqBody = IB;
  75. type ResBody = OB;
  76. type Error = E;
  77. type MakeError = ME;
  78. type Service = WrapperService<S>;
  79. type Future = Box<Future<Item=Self::Service, Error=Self::MakeError> + Send>;
  80.  
  81. fn make_service(&mut self, ctx: &Ctx) -> Self::Future {
  82. Box::new(self.0.make_service(ctx).map(|x| WrapperService(x)))
  83. }
  84. }
  85.  
  86. // Main
  87.  
  88. fn main() {
  89. let f = WrapperMakeService(BaseMakeService {});
  90.  
  91. let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
  92.  
  93. let server = hyper::server::Server::bind(&socket);
  94.  
  95. server.serve(f);
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement