Skip to content

Commit

Permalink
Plenty of changes in Bailador.pm
Browse files Browse the repository at this point in the history
Use Bailador::Response objects.
Don't create new Bailador::Request on every request.
Add status() and content_type()
  • Loading branch information
Tadeusz Sośnierz committed Dec 27, 2011
1 parent a64fbc3 commit de118c2
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions lib/Bailador.pm
@@ -1,12 +1,14 @@
module Bailador;
use Bailador::Request;
use Bailador::Response;
use HTTP::Easy::PSGI;

my %routes;
%routes<GET> = [];
%routes<POST> = [];

my $current-request;
my $current-request = Bailador::Request.new;
my $current-response = Bailador::Response.new;

sub get(Pair $x) is export {
%routes<GET>.push: $x;
Expand All @@ -20,24 +22,34 @@ sub post(Pair $x) is export {

sub request is export { $current-request }

sub content_type(Str $type) is export {
$current-response.headers<Content-Type> = $type;
}

sub status(Int $code) is export {
$current-response.code = $code;
}

sub dispatch($env) {
my $res = '';
$current-request = Bailador::Request.new(:$env);

$current-request.env = $env;
$current-response.code = 404;
$current-response.content = 'Not found';
$current-response.headers<Content-Type> = 'text/html';

for %routes{$env<REQUEST_METHOD>}.list -> $r {
next unless $r;
if $env<REQUEST_URI> ~~ $r.key {
$current-response.code = 200;
if $/ {
$res = $r.value.(|$/.list);
$current-response.content = $r.value.(|$/.list);
} else {
$res = $r.value.();
$current-response.content = $r.value.();
}
}
}
if $res {
return [200, [ 'Content-Type' => 'text/html' ], [$res]];
} else {
return [404, [ 'Content-Type' => 'text/plain' ], ['Not found']];
}
return $current-response.psgi;
}

sub baile is export {
Expand Down

0 comments on commit de118c2

Please sign in to comment.