https://github.com/junekelly/perl6-http-router-blind
A simple HTTP Router for Perl6
https://github.com/junekelly/perl6-http-router-blind
Last synced: 3 months ago
JSON representation
A simple HTTP Router for Perl6
- Host: GitHub
- URL: https://github.com/junekelly/perl6-http-router-blind
- Owner: JuneKelly
- License: mit
- Created: 2015-10-16T15:55:50.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2017-04-29T16:30:31.000Z (about 9 years ago)
- Last Synced: 2025-02-26T11:36:10.023Z (over 1 year ago)
- Language: Perl6
- Size: 17.6 KB
- Stars: 1
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# HTTP::Router::Blind
A simple, framework-agnostic HTTP Router for Perl6
[](https://travis-ci.org/ShaneKilkelly/perl6-http-router-blind)
## Example
With the HTTP::Easy server:
```perl6
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;
[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\/$=(.*)/, sub (%env, $params) {
my $id = $params;
[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, %env, %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`.