A simple HTTP Router for Perl6
Perl6 Makefile
Latest commit 8d33bdd Oct 27, 2016 @ShaneKilkelly fix
Permalink
Failed to load latest commit information.
lib/HTTP/Router fix Oct 27, 2016
t Fix removed @*INC Dec 5, 2015
.gitignore update to new perl6 Dec 26, 2015
.travis.yml add a travis file Oct 17, 2015
LICENSE.txt add license file Oct 18, 2015
META.info Add mandatory "perl" META field Oct 26, 2016
Makefile fix Oct 27, 2016
README.md begin converting to use second parameter for $params Nov 1, 2015

README.md

HTTP::Router::Blind

A simple, framework-agnostic HTTP Router for Perl6

Build Status

Example

With the HTTP::Easy server:

use v6;
use HTTP::Easy::PSGI;
use HTTP::Router::Blind;

my $http = HTTP::Easy::PSGI.new(:port(8080));
my $router = HTTP::Router::Blind.new();

# simple string-match route
$router.get("/", sub (%env) {
    [200, ['Content-Type' => 'text/plain'], ["Home is where the heart is"]]
});

$router.get("/about", sub (%env) {
    [200, ['Content-Type' => 'text/plain'], ["About this site"]]
});

# string match with keyword params,
# the second parameter to the handler function is a hash of params
# extracted from the URL
$router.get("/user/:id", sub (%env, %params) {
    my $user-id = %params<id>;
    [200, ['Content-Type' => 'text/plain'], ["It's user $user-id"]]
});

# regex match, with named capture-group,
# will match a request like '/items/42253',
# the second parameter to the handler is the Regex match object;
$router.get(/\/items\/$<id>=(.*)/, sub (%env, $params) {
    my $id = $params<id>;
    [200, ['Content-Type' => 'text/plain'], ["got request for item $id"]]
});

# you can pass multiple handler functions
# which will be chained together in order
sub do-something-special (%env) { ...; return %env; }
$router.get('/secret', &do-something-special, sub (%env) {
    [200, ['Content-Type' => 'text/plain'], ["it's a secret"]]
});

# in our app function, we just call $router.dispatch
my $app = sub (%env) {
    $router.dispatch(%env<REQUEST_METHOD>, %env<REQUEST_URI>, %env);
};

$http.handle($app);

CHANGELOG

  • 2.0.0 : for keyword and regex matches, pass the matched params as a second parameter to the handler function, instead of setting %env<params>.