Skip to content

stepankuzmin/rererouter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rererouter

Build Status Crates.io Status

Iron router with regex captures support.

Documentation

Documentation is here

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

Releases

No releases published

Sponsor this project

 

Packages

No packages published

Languages