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
|
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use CGI::Tiny;
use Routes::Tiny;
my %dispatch = (
foos => sub {
my ($cgi) = @_;
my $method = $cgi->method;
$cgi->render(text => "$method foos");
},
get_foo => sub {
my ($cgi, $captures) = @_;
my $id = $captures->{id};
$cgi->render(text => "Retrieved foo $id");
},
put_foo => sub {
my ($cgi, $captures) = @_;
my $id = $captures->{id};
$cgi->render(text => "Stored foo $id");
},
);
cgi {
my $cgi = $_;
my $routes = Routes::Tiny->new;
# /script.cgi/foo
$routes->add_route('/foo', name => 'foos');
# /script.cgi/foo/42
$routes->add_route('/foo/:id', method => 'GET', name => 'get_foo');
$routes->add_route('/foo/:id', method => 'PUT', name => 'put_foo');
if (defined(my $match = $routes->match($cgi->path, method => $cgi->method))) {
$dispatch{$match->name}->($cgi, $match->captures);
} else {
$cgi->set_response_status(404)->render(text => 'Not Found');
}
};
|