/
path-router.psgi
executable file
·46 lines (40 loc) · 1.11 KB
/
path-router.psgi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use MyApp::Blog;
use MyApp::Hello;
use Path::Router;
use Plack::App::Path::Router;
use Moose::Util::TypeConstraints;
sub dispatch {
my ($r) = @_;
my $mapping = $r->env->{'plack.router.match'}->mapping;
my $controller = 'MyApp::' . $mapping->{controller};
my $action = $controller->can(lc($r->method) . '_' . $mapping->{action})
or return $r->new_response(405);
$controller->$action($r, $mapping);
}
my $router = Path::Router->new;
$router->add_route('/' => (
defaults => {
controller => 'Hello',
action => 'index',
},
target => \&dispatch
));
$router->add_route('/blog/:year/:month' => (
defaults => {
controller => 'Blog',
action => 'monthly',
},
validations => {
year => subtype( as 'Int' => where { $_ > 0 } ),
month => subtype( as 'Int' => where { $_ >= 1 && $_ <= 12 } ),
},
target => \&dispatch
));
$router->add_route('/comment', => (
defaults => {
controller => 'Blog',
action => 'comment',
},
target => \&dispatch
));
Plack::App::Path::Router->new(router => $router)->to_app;