This cargo is a small extension to the great Hyper HTTP library. It basically is adds the ability to define routes to request handlers and then query for the handlers by request path.
To use the library just add:
toml
hyper = "^0.12"
hyper-routing = "^0.5"
to your dependencies.
```rust extern crate hyper; extern crate hyper_routing;
use hyper::header::{CONTENTLENGTH, CONTENTTYPE}; use hyper::{Request, Response, Body, Method}; use hyper::server::Server; use hyper::rt::Future; use hyper_routing::{Route, RouterBuilder, RouterService};
fn basichandler(: Request
) -> Response { let body = "Hello World"; Response::builder() .header(CONTENTLENGTH, body.len() as u64) .header(CONTENTTYPE, "text/plain") .body(Body::from(body)) .expect("Failed to construct the response") }fn routerservice() -> Result
Ok(RouterService::new(router))
}
fn main() { let addr = "0.0.0.0:8080".parse().unwrap(); let server = Server::bind(&addr) .serve(routerservice) .maperr(|e| eprintln!("server error: {}", e));
hyper::rt::run(server)
} ```
This code will start Hyper server and add use router to find handlers for request.
We create the Route
so that when we visit path /greet
the basic_handler
handler
will be called.
add
ed will be chosen.I've created this little tool to help myself learn Rust and to avoid using big frameworks like Iron or rustful. I just want to keep things simple.
Obviously I could make some errors or bad design choices so I'm waiting for your feedback! Please contact me at moriturius at GMail. You may also create an issue at project's bug tracker.