#regex #router #web

yanked regex_router

An implementation of request routing via a singular grouped regex (with support for path parameter extraction)

1.0.2 Nov 25, 2021
0.1.0 Nov 23, 2021

#243 in #router

MIT license

13KB
174 lines

rs-regex-router

GitHub license Crates.io docs.rs

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.
  • A convenience macro for easily declaring routes.

Installation & Basic Usage

[dependencies]
regex_router = "1.0.2"

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 implementation for hyper can be found here.

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.

Dependencies

~2.2–3MB
~54K SLoC