ferrum-router

Crates.ioferrum-router
lib.rsferrum-router
version0.2.1
sourcesrc
created_at2018-02-12 18:11:04.040559
updated_at2018-02-12 19:12:12.10912
descriptionA router for the Ferrum framework.
homepage
repositoryhttps://github.com/ferrum-rs/ferrum-router
max_upload_size
id50900
size59,320
Alexander Mescheryakov (XX)

documentation

README

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

Commit count: 30

cargo fmt