2 releases

Uses old Rust 2015

0.2.1 Feb 12, 2018
0.2.0 Feb 12, 2018

#1124 in HTTP server

MIT license

52KB
1K SLoC

Ferrum Router

Routing handler for the Ferrum web framework.

Ferrum Router is a fast, convenient, and flexible routing middleware for Ferrum. It allows complex glob regex patterns and named url parameters and also allows handlers to be any Handler, including all Chains.

Example

extern crate ferrum;
extern crate ferrum_router;

use ferrum::*;
use ferrum_router::{Router, Id};

fn main() {
    let mut router = Router::new();                     // Alternative syntax:
                                                        // let router = router!(
    router.get("/", handler, None);                     //     index: get "/" => handler,
    router.get("/{query}", handler, Id::some("query")); //     query: get "/{query}" => handler "query");

    Ferrum::new(router).http("localhost:3000").unwrap();

    fn handler(request: &mut Request) -> FerrumResult<Response> {
        let params = request.extensions.get::<Router>().unwrap();
        let query = params.get("query").map(|value| value.as_str()).unwrap_or("/");
        Ok(Response::new().with_content(query, mime::TEXT_PLAIN))
    }
}

Overview

Router is a part of Ferrum's core bundle.

  • Route client requests based on their paths
  • Parse parameters and provide them to other middleware/handlers

Installation

If you're using cargo, just add ferrum-router to your Cargo.toml.

[dependencies]

ferrum-router = "*"

Otherwise, cargo build, and the rlib will be in your target directory.

Examples

Check out the examples directory!

You can run an individual example using cargo run --example example-name. Note that for benchmarking you should make sure to use the --release flag, which will cause cargo to compile the entire toolchain with optimizations. Without --release you will get truly sad numbers.

Benches

To run benchmark tests, please use Rust nightly toolchain:

rustup default nightly
cargo bench --features "nightly"

License

MIT

Dependencies

~14MB
~287K SLoC