Created
May 30, 2013 14:34
-
-
Save shibayu36/5678303 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app.psgi | |
use strict; | |
use warnings; | |
use WAF; | |
any '/' => sub { | |
my $c = shift; | |
$c->render('index.tt', { name => 'shiba_yu36' }); | |
}; | |
get '/hoge' => sub { | |
my $c = shift; | |
$c->render('hoge.tt', { name => 'shiba_yu36' }); | |
}; | |
waf; | |
__DATA__ | |
@@ index.tt | |
<html> | |
<body> | |
Hello, [% name %] | |
</body> | |
</html> | |
@@ hoge.tt | |
<html> | |
<body> | |
Hoge, [% name %] | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package WAF; | |
use strict; | |
use warnings; | |
use Exporter::Lite; | |
our @EXPORT = qw(get post any waf); | |
use Router::Simple; | |
our $router = Router::Simple->new(); | |
use Data::Section::Simple; | |
our $data_section = Data::Section::Simple->new(caller(0)); | |
# ----- Controller ----- | |
sub get { | |
my ($url, $action) = @_; | |
any($url, $action, ['GET']); | |
} | |
sub post { | |
my ($url, $action) = @_; | |
any($url, $action, ['POST']); | |
} | |
sub any { | |
my ($url, $action, $methods) = @_; | |
my $opts = {}; | |
$opts->{method} = $methods if $methods; | |
$router->connect($url, { action => $action }, $opts); | |
} | |
sub waf { | |
return my $app = sub { | |
my $env = shift; | |
my $context = WAF::Context->new( | |
env => $env, | |
data_section => $data_section, | |
); | |
if (my $p = $router->match($env)) { | |
$p->{action}->($context); | |
return $context->res->finalize; | |
} | |
else { | |
[404, [], ['not found']]; | |
} | |
}; | |
} | |
# ------ Controllerで利用するAPI ------ | |
package WAF::Context; | |
use Data::Section::Simple qw(get_data_section); | |
sub new { | |
my ($class, %args) = @_; | |
return bless { | |
env => $args{env}, | |
data_section => $args{data_section}, | |
}, $class; | |
} | |
sub env { | |
my $self = shift; | |
return $self->{env}; | |
} | |
sub data_section { | |
my $self = shift; | |
return $self->{data_section}; | |
} | |
sub req { | |
my $self = shift; | |
return $self->{_req} ||= WAF::Request->new($self->env); | |
} | |
sub res { | |
my $self = shift; | |
return $self->{_res} ||= $self->req->new_response(200); | |
} | |
sub render { | |
my ($self, $tmpl_name, $args) = @_; | |
my $str = $self->data_section->get_data_section($tmpl_name); | |
my $body = WAF::View->render_string($str, $args); | |
return $self->res->body($body); | |
} | |
package WAF::Request; | |
use parent qw(Plack::Request); | |
package WAF::Response; | |
use parent qw(Plack::Response); | |
# -------- View --------- | |
package WAF::View; | |
use Text::Xslate; | |
our $tx = Text::Xslate->new( | |
syntax => 'TTerse', | |
module => [ qw(Text::Xslate::Bridge::TT2Like) ], | |
); | |
sub render_string { | |
my ($class, $str, $args) = @_; | |
return $tx->render_string($str, $args); | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment