rs-regex-router
An implementation of request routing via a singular grouped regex (with support for path parameter extraction).
Features
- Design approach based upon this article.
- Implemented over a generic handler type allowing use with varying webserver crates.
- Extraction of path parameters which are then mapped as key/value pairs.
- Convenience macros for easily declaring routes and obtaining path parameter values.
Installation & Basic Usage
[dependencies]
regex_router = "1.0.0"
An example using a unit handler type:
use regex_router::{RouterBuilder, route};
...
let mut builder = RouterBuilder::<()>::new();
route!(builder; r"/example";; "GET" => ());
route!(builder; r"/test";; "GET" => ());
let router = builder.build().unwrap();
An example declaring path parameters:
let mut builder = RouterBuilder::<()>::new();
route!(builder; r"/example/(\d+)"; "var1"; "GET" => ());
route!(builder; r"/test/(\d+)/(\d+)"; "var1", "var2"; "GET" => ());
let router = builder.build().unwrap();
Dispatching against a router:
match router.dispatch("GET", "/example/500") {
Some(route_match) => {
// Call handler and return response.
}
None => {
// No route match. Return 404.
}
};
An example hyper
implementation can be found here: TBC
Issues & Support
Whether you're wanting to report a bug you've come across during use of this crate or are seeking general help/assistance, please utilise the issues tracker and provide as much detail as possible (eg. recreation steps).
I try to respond to issues within a reasonable timeframe.