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
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()) }
async fn main() -> Result<(), Box