Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support regex URL patterns #130

Open
miedzinski opened this issue Nov 1, 2016 · 5 comments
Open

Support regex URL patterns #130

miedzinski opened this issue Nov 1, 2016 · 5 comments

Comments

@miedzinski
Copy link
Contributor

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
Copy link
Member

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
Copy link

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

@stepankuzmin
Copy link

Any news on that?

@ernestas-poskus
Copy link

I guess you will have to implement this yourself

@stepankuzmin
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants