No description, website, or topics provided.
Switch branches/tags
Clone or download
Latest commit d1b3cef Nov 22, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
reset-router-macros docs Nov 21, 2018
reset-router docs Nov 21, 2018
.gitignore refresh Aug 12, 2017
.rustfmt.toml update readme Nov 20, 2018
Cargo.toml new Nov 20, 2018
README.md update example link Nov 20, 2018

README.md

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