rererouter

Crates.iorererouter
lib.rsrererouter
version0.1.1
sourcesrc
created_at2017-11-26 17:10:34.348322
updated_at2017-11-27 08:53:50.133454
descriptionIron router with regex captures support.
homepagehttps://github.com/stepankuzmin/rererouter
repositoryhttps://github.com/stepankuzmin/rererouter
max_upload_size
id40602
size9,144
Stepan Kuzmin (stepankuzmin)

documentation

README

rererouter

Build Status Crates.io Status

Iron router with regex captures support.

Usage

Add this to your Cargo.toml:

[dependencies]
rererouter = "0.1"

and this to your crate root:

extern crate rererouter;

Example

extern crate iron;
extern crate regex;
extern crate rererouter;

use regex::Captures;
use iron::prelude::{Iron};
use iron::{status, Request, Response};
use rererouter::RouterBuilder;

fn main() {
    let mut router_builder = RouterBuilder::new();

    router_builder.get(r"/hello-(?P<name>\w*)", |_: &mut Request, captures: Captures| {
        let greeting = format!("Hello, {}!", &captures["name"]);
        Ok(Response::with((status::Ok, greeting)))
    });

    router_builder.post(r"/count-to-(?P<count>\d*)", |_: &mut Request, captures: Captures| {
        let count = format!("Let's count to {}!", &captures["count"]);
        Ok(Response::with((status::Ok, count)))
    });

    router_builder.not_found(|_: &mut Request| {
        Ok(Response::with((status::NotFound, "Not found")))
    });

    let router = router_builder.finalize();
    Iron::new(router).http("localhost:3000").unwrap();
}

Usage:

$ curl -i http://localhost:3000/hello-rererouter

HTTP/1.1 200 OK
Content-Length: 18
Content-Type: text/plain
Date: Mon, 27 Nov 2017 08:36:47 GMT

Hello, rererouter!
$ curl -i -X POST http://localhost:3000/count-to-10

HTTP/1.1 200 OK
Content-Length: 18
Content-Type: text/plain
Date: Mon, 27 Nov 2017 08:37:19 GMT

Let's count to 10!
$ curl -i http://localhost:3000/not-found

HTTP/1.1 404 Not Found
Content-Length: 9
Content-Type: text/plain
Date: Mon, 27 Nov 2017 08:42:30 GMT

Not found
Commit count: 10

cargo fmt