Crates.io | rererouter |
lib.rs | rererouter |
version | 0.1.1 |
source | src |
created_at | 2017-11-26 17:10:34.348322 |
updated_at | 2017-11-27 08:53:50.133454 |
description | Iron router with regex captures support. |
homepage | https://github.com/stepankuzmin/rererouter |
repository | https://github.com/stepankuzmin/rererouter |
max_upload_size | |
id | 40602 |
size | 9,144 |
Iron router with regex captures support.
Add this to your Cargo.toml
:
[dependencies]
rererouter = "0.1"
and this to your crate root:
extern crate rererouter;
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