iron / router

Router middleware for the Iron web framework.
165 stars 74 forks source link

Support regex URL patterns #130

Open miedzinski opened 8 years ago

miedzinski commented 8 years ago

It'd be great if iron router supported URL patterns as regexes. This way we would be able to declare patterns without any ambiguities and offload some param validation to router (e.g. make sure given ID is 3-digit). Coming from Python community, I am familiar with Django URL dispatch module, which I see as a great fit. Right now router uses router-recognizer module, which, IMHO, is quite complicated compared to plain regexes. I've heard Rust has excellent support for regular expressions, so there is some chance it wouldn't be that hard to write some POC implementation, perhaps with same or even better performance. Any thoughts?

untitaker commented 8 years ago

I'd much rather have the user register custom patterns as a "type" and reference those types by name in the URL pattern instead of using regexes directly. It is implemented like that in Flask: http://flask.pocoo.org/docs/0.11/quickstart/#variable-rules This makes URL patterns easier to read and also allows for parsing in the same step.

Hoverbear commented 8 years ago

The folks working on router-recognizer also might be open to changes. :)

stepankuzmin commented 7 years ago

Any news on that?

ernestas-poskus commented 7 years ago

I guess you will have to implement this yourself

stepankuzmin commented 7 years ago

If anyone is interested, I've made the crate rererouter that supports regex captures.

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.get(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)))
    });

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