#hyper #router #regexset

reset-router

A RegexSet based path router for Hyper

11 unstable releases (3 breaking)

0.6.4 Dec 29, 2018
0.6.3 Nov 21, 2018
0.5.0 Oct 2, 2018
0.3.0 Nov 1, 2017
0.2.3 Aug 24, 2017

#62 in HTTP server

Download history 17/week @ 2018-11-07 28/week @ 2018-11-14 36/week @ 2018-11-21 31/week @ 2018-11-28 18/week @ 2018-12-05 23/week @ 2018-12-12 47/week @ 2018-12-19 11/week @ 2018-12-26 22/week @ 2019-01-02 13/week @ 2019-01-09 7/week @ 2019-01-16 1/week @ 2019-01-23 1/week @ 2019-01-30 1/week @ 2019-02-06 16/week @ 2019-02-13

87 downloads per month

MIT license

20KB
380 lines


lib.rs:

reset-router

A RegexSet based router for use with async Hyper.

Provides optional attribute based routing with Rust 1.30's proc_macro_attributes.

Your handler functions should look like:

Fn(http::Request<hyper::Body>) -> I
where
    I: IntoFuture<Item = S, Error = E>,
    I::Future: 'static + Send,
    S: Into<http::Response<hyper::Body>>,
    E: Into<http::Response<hyper::Body>>,

Pretty straightforward! You can return something as simple as Ok(Response::new()). You don't have to worry about futures unless you need to read the request body or interact with other future-aware things.

Usage:


use reset_router::RequestExtensions;

// Example handler from Rocket
#[get(r"^/hello/([^/]+)/(\d+)$")]
pub fn hello(req: Request) -> Result<Response> {    
    let (name, age) = req.parsed_captures::<(String, u8)>()?;
    Ok(::http::Response::builder()
        .status(200)
        .body(format!("Hello, {} year old named {}!", age, name).into())
        .unwrap())
}

fn main() {
    let router = reset_router::Router::build()
        .add_routes(routes![hello])
        .finish()
        .unwrap();

    let addr = "0.0.0.0:3000".parse().unwrap();

    let server =
        hyper::Server::bind(&addr).serve(router).map_err(|e| eprintln!("server error: {}", e));

    hyper::rt::run(server);
}

RequestExtensions provides easy access to your route regex captures, as well as access to an optional State object. See simple.rs for an example.

If you prefer to keep all your path regexes in one place, of if you want to use closures, you can still use the old style:


// Use this custom bitflags instead of http::Method for easy `BitOr` style method combinations
use reset_router::bits::Method;

let router = reset_router::Router::build()
    .add(Method::GET | method::POST, r"^/hello/([^/]+)/(\d+)$", hello)
    .finish()
    .unwrap();

License: MIT

Dependencies

~6MB
~113K SLoC