[Jifty-commit] jifty branch, master, updated. 99425d042432180fa77cf383531c6f4fdf3e648d

Jifty commits jifty-commit at lists.jifty.org
Wed Apr 7 12:42:46 EDT 2010


The branch, master has been updated
       via  99425d042432180fa77cf383531c6f4fdf3e648d (commit)
      from  a96d64ac387814ae563705b58c8710e6634f9571 (commit)

Summary of changes:
 lib/Jifty/Config.pm  |    1 +
 lib/Jifty/Handler.pm |   49 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+), 0 deletions(-)

- Log -----------------------------------------------------------------
commit 99425d042432180fa77cf383531c6f4fdf3e648d
Author: Chia-liang Kao <clkao at clkao.org>
Date:   Thu Apr 8 00:41:49 2010 +0800

    Use PSGI app to serve static content by default.

diff --git a/lib/Jifty/Config.pm b/lib/Jifty/Config.pm
index 7a981d8..0613c49 100644
--- a/lib/Jifty/Config.pm
+++ b/lib/Jifty/Config.pm
@@ -515,6 +515,7 @@ sub guess {
                     default_escape_flags => 'h',
                 },
                 Globals => [],
+                PSGIStatic => 1,
             },
         },
     };
diff --git a/lib/Jifty/Handler.pm b/lib/Jifty/Handler.pm
index 70d8144..6b9e7b8 100644
--- a/lib/Jifty/Handler.pm
+++ b/lib/Jifty/Handler.pm
@@ -27,6 +27,7 @@ use base qw/Class::Accessor::Fast Jifty::Object/;
 use Jifty::View::Declare::Handler ();
 use Class::Trigger;
 use String::BufferStack;
+use Plack::Builder;
 use Plack::Request;
 
 __PACKAGE__->mk_accessors(qw(dispatcher _view_handlers stash buffer));
@@ -112,6 +113,47 @@ sub view {
     return $self->_view_handlers->{$class};
 }
 
+=head2 psgi_app_static
+
+Returns a closure for L<PSGI> application that handles all static
+content, including plugins.
+
+=cut
+
+sub psgi_app_static {
+    my $self = shift;
+
+    my $view_handler = $self->view('Jifty::View::Static::Handler')
+        or return;;
+
+    require Plack::App::Cascade;
+    require Plack::App::File;
+    my $static = Plack::App::Cascade->new;
+    $static->add( Plack::App::File->new(root => $_)->to_app)
+        for $view_handler->roots;
+
+    # the buffering and unsetting of psgi.streaming is to vivify the
+    # responded res from the $static cascade app.
+    builder {
+        enable 'Plack::Middleware::ConditionalGET';
+        enable
+            sub { my $app = shift;
+                  sub { my $env = shift;
+                        $env->{'psgi.streaming'} = 0;
+                        my $res = $app->($env);
+                        # skip streamy response
+                        return $res unless ref($res) eq 'ARRAY' && $res->[2];
+                        my $h = Plack::Util::headers($res->[1]);;
+                        $h->set( 'Cache-Control' => 'max-age=31536000, public' );
+                        $h->set( 'Expires' => HTTP::Date::time2str( time() + 31536000 ) );
+                        $res;
+                    };
+              };
+        enable 'Plack::Middleware::BufferedStreaming';
+        $static;
+    };
+}
+
 =head2 psgi_app
 
 Returns a closure for L<PSGI> application.
@@ -122,6 +164,13 @@ sub psgi_app {
     my $self = shift;
 
     my $app = sub { $self->handle_request(@_) };
+    my $static = $self->psgi_app_static;
+
+    $app = builder {
+        mount '/static' => $static;
+        mount '/'       => $app
+    }
+        if Jifty->config->framework("Web")->{PSGIStatic} && $static;
 
     # allow plugin to wrap $app
     for ( Jifty->plugins ) {

-----------------------------------------------------------------------


More information about the Jifty-commit mailing list