Browse files

move Middleware in Application. D'oh, you should call ->psgi_app from…

… .psgi to avoid initializing Application multiple times
  • Loading branch information...
1 parent eafb4ab commit d240448f2dbe9942183646fe826499f4c90cd565 @miyagawa committed Nov 4, 2009
Showing with 15 additions and 10 deletions.
  1. +3 −9 eg/demo.pl
  2. +12 −1 lib/Tatsumaki/Application.pm
View
12 eg/demo.pl
@@ -152,13 +152,7 @@ package main;
]);
$app->template_path(dirname(__FILE__) . "/templates");
-
-# TODO this should be part of core
-use Plack::Middleware::Static;
-$app = Plack::Middleware::Static->wrap($app, path => qr/^\/static/, root => dirname(__FILE__));
-
-use Tatsumaki::Middleware::BlockingFallback;
-$app = Tatsumaki::Middleware::BlockingFallback->wrap($app);
+$app->static_path(dirname(__FILE__) . "/static");
# TODO these should be an external services module
use Try::Tiny;
@@ -309,7 +303,7 @@ package main;
if (__FILE__ eq $0) {
require Tatsumaki::Server;
- Tatsumaki::Server->new(port => 9999)->run($app);
+ Tatsumaki::Server->new(port => 9999)->run($app->psgi_app);
} else {
- return $app;
+ return $app->psgi_app;
}
View
13 lib/Tatsumaki/Application.pm
@@ -6,11 +6,16 @@ use Tatsumaki::Request;
use Text::MicroTemplate::File;
use Try::Tiny;
+use Plack::Middleware::Static;
+use Tatsumaki::Middleware::BlockingFallback;
+
use overload q(&{}) => sub { shift->psgi_app }, fallback => 1;
has _rules => (is => 'rw', isa => 'ArrayRef');
has template => (is => 'rw', isa => 'Text::MicroTemplate::File', lazy_build => 1, handles => [ 'render_file' ]);
+has static_path => (is => 'rw', isa => 'Str', default => 'static');
+
around BUILDARGS => sub {
my $orig = shift;
my $class = shift;
@@ -48,7 +53,8 @@ sub dispatch {
sub psgi_app {
my $self = shift;
- return sub {
+
+ my $app = sub {
my $env = shift;
my $req = Tatsumaki::Request->new($env);
@@ -72,6 +78,11 @@ sub psgi_app {
return $res;
};
+
+ $app = Plack::Middleware::Static->wrap($app, path => sub { s/^\/static\/// }, root => $self->static_path);
+ $app = Tatsumaki::Middleware::BlockingFallback->wrap($app);
+
+ $app;
}
sub _build_template {

0 comments on commit d240448

Please sign in to comment.