Hyper Tree Router

Routing middleware for Hyper http library using Prefix tree (trie) for path finding. Gives the ability to define routes and handlers for given request path

Usage

To use the library add to your Cargo.toml

toml hyper = { version = "^0.14", features = ["http1", "server", "tcp"] } hyper-tree-router = "^0.1"

Then you can define handlers for a given route as follows. The below code creates two Route and creates a hyper server for them.

/ with hello_handler that will return "Hello world" when you open the server

/user/:user_id/home with the user_handler the url_params contains a hashmap of url parameters you can look up and respond to.

```rust use hyper::{ header::{CONTENTLENGTH, CONTENTTYPE}, Body, Request, Response, Server, }; use hypertreerouter::{Route, RouterBuilder, UrlParams};

fn plaintextresponse(body: String) -> Response { Response::builder() .header(CONTENTLENGTH, body.len() as u64) .header(CONTENTTYPE, "text/plain") .body(Body::from(body)) .expect("Failed to construct response") }

fn userhandler(urlparams: UrlParams, : Request) -> Response { let body = format!( "user: {}", urlparams .captures .get(":userid") .unwrapor(&"unknown user".tostring()) ); plaintext_response(body) }

fn hellohandler(: UrlParams, : Request) -> Response { plaintextresponse("Hello World".tostring()) }

[tokio::main]

async fn main() -> Result<(), Box> { let addr = "0.0.0.0:8080".parse().unwrap(); let router = RouterBuilder::new() .route(Route::url("/user/:userid/home").get(userhandler)) .route(Route::url("/").get(hello_hanlder)) .build(); let server = Server::bind(&addr).serve(router); println!("Listening on http://{}", addr); server.await?; Ok(()) } ```