Permalink
Browse files

First attack at placeholders in routes. Segfaults

  • Loading branch information...
1 parent 2acf8e1 commit 5a2b52a2151f3b13f8bad60690c56418a73a2930 @tadzik tadzik committed Dec 27, 2011
Showing with 29 additions and 2 deletions.
  1. +4 −0 examples/app.pl
  2. +25 −2 lib/Bailador.pm
View
@@ -9,6 +9,10 @@
"about me"
};
+get '/hello/:name' => sub ($name) {
+ "Hello $name!"
+};
+
# regexes, as usual
get /foo(.+)/ => sub ($x) {
"regexes! I got $x"
View
@@ -10,13 +10,36 @@ my %routes;
my $current-request = Bailador::Request.new;
my $current-response = Bailador::Response.new;
+sub route_to_regex($route) {
+ $route.split('/').map({
+ my $r = $_;
+ if $_.substr(0, 1) eq ':' {
+ $r = q{(<-[\/\.]>+)};
+ }
+ $r
+ }).join("'/'");
+}
+
+multi parse_route(Str $route) {
+ my $r = route_to_regex($route);
+ say $r.perl;
+ return / ^ <$r> $ /
+}
+
+multi parse_route($route) {
+ # do nothing
+ $route
+}
+
sub get(Pair $x) is export {
- %routes<GET>.push: $x;
+ my $p = parse_route($x.key) => $x.value;
+ %routes<GET>.push: $p;
return $x;
}
sub post(Pair $x) is export {
- %routes<POST>.push: $x;
+ my $p = parse_route($x.key) => $x.value;
+ %routes<POST>.push: $p;
return $x;
}

0 comments on commit 5a2b52a

Please sign in to comment.