SHARE
TWEET

Untitled

a guest Aug 19th, 2019 295 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #![feature(trait_alias)]
  2. use futures::future::{self, Future};
  3. use hyper::server::conn::AddrStream;
  4. use hyper::service::{make_service_fn, service_fn};
  5. use hyper::{Body, Method, Request, Response};
  6.  
  7. //
  8.  
  9. #[cfg_attr(rustfmt, rustfmt_skip)]
  10. pub trait RetFut = Future<Item = Response<Body>, Error = hyper::Error>;
  11.  
  12. #[derive(Debug, Clone)]
  13. pub struct Route {
  14.     pub method: Method,
  15. }
  16.  
  17. #[derive(Debug, Clone)]
  18. pub struct Router {
  19.     routes: Vec<Route>,
  20. }
  21. impl Router {
  22.     pub fn builder() -> RouterBuilder {
  23.         RouterBuilder::new()
  24.     }
  25.  
  26.     pub fn handle_req(&self, req: Request<Body>) -> impl RetFut {
  27.         match (req.method(), req.uri().path()) {
  28.             _ => future::ok(Response::new(Body::from("Hello world"))),
  29.         }
  30.     }
  31. }
  32.  
  33. #[derive(Debug, Default)]
  34. pub struct RouterBuilder {
  35.     routes: Vec<Route>,
  36. }
  37. impl RouterBuilder {
  38.     pub fn new() -> Self {
  39.         RouterBuilder { routes: vec![] }
  40.     }
  41.  
  42.     pub fn build(self) -> Router {
  43.         Router {
  44.             routes: self.routes,
  45.         }
  46.     }
  47. }
  48.  
  49. fn main() {
  50.     let addr = "0.0.0.0:8080".parse().unwrap();
  51.  
  52.     let make_svc = make_service_fn(move |_socket: &AddrStream| {
  53.         let router: Router = Router::builder().build();
  54.         service_fn(move |req: Request<Body>| router.handle_req(req))
  55.     });
  56.  
  57.     let server = hyper::Server::bind(&addr)
  58.         .serve(make_svc)
  59.         .map_err(|e| eprintln!("server error: {}", e));
  60.  
  61.     hyper::rt::run(server);
  62. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top