Crates.io | ferrum-router |
lib.rs | ferrum-router |
version | 0.2.1 |
source | src |
created_at | 2018-02-12 18:11:04.040559 |
updated_at | 2018-02-12 19:12:12.10912 |
description | A router for the Ferrum framework. |
homepage | |
repository | https://github.com/ferrum-rs/ferrum-router |
max_upload_size | |
id | 50900 |
size | 59,320 |
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.
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))
}
}
Router is a part of Ferrum's core bundle.
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.
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.
To run benchmark tests, please use Rust nightly toolchain:
rustup default nightly
cargo bench --features "nightly"
MIT