Routing for paths delimited by a forward slash, with the ability to capture specified path segments.
Routing for the Hyper server:
```rust extern crate hyper; extern crate futures; extern crate path_router;
use hyper::server::{Http, Request, Response}; use hyper::header::ContentLength; use futures::future::{FutureResult, ok}; use std::sync::Arc;
type Handler = fn(Request, Vec<(&str, String)>) -> Response;
fn main() {
struct WebService<'a, T> {
routes: Arc
let mut routes: path_router::Tree<Handler> = path_router::Tree::new();
routes.add("GET/echo/:text", echo_handler);
routes.add("GET/reverse/:text", reverse_handler);
let routes_r = Arc::new(routes);
let addr = "127.0.0.1:3000".parse().unwrap();
let server = Http::new().bind(&addr, move || {
Ok(WebService {
routes: routes_r.clone()
})
}).unwrap();
server.run().unwrap();
}
fn echohandler(req: Request, captures: Vec<(&str, String)>) -> Response { let text = captures.iter().find(|c| c.0 == "text").unwrap().1.toowned(); Response::new() .withheader(ContentLength(text.len() as u64)) .with_body(text) }
fn reversehandler(req: Request, captures: Vec<(&str, String)>) -> Response { let text = captures.iter().find(|c| c.0 == "text").unwrap().1.toowned(); let reversed: String = text.chars().rev().collect(); Response::new() .withheader(ContentLength(text.len() as u64)) .with_body(reversed) } ```