Skip to content

Instantly share code, notes, and snippets.

@jnareb
Created March 31, 2012 12:19
Show Gist options
  • Select an option

  • Save jnareb/2263255 to your computer and use it in GitHub Desktop.

Select an option

Save jnareb/2263255 to your computer and use it in GitHub Desktop.
fragment of gitweb code responsible for static assets in PSGI
if (-d "++GITWEBSTATICDIR++") {
require Plack::App::URLMap;
require Plack::App::File;
my $urlmap = Plack::App::URLMap->new();
$urlmap->map("/" => $app);
foreach my $static_url (@stylesheets, $stylesheet, $logo, $favicon, $javascript) {
next if (!defined $static_url || $static_url eq "");
(my $static_file = $static_url) =~ s!^.*/!!; # basename
$static_file = "++GITWEBSTATICDIR++/$static_file";
$urlmap->map($static_url => Plack::App::File->new(file => $static_file));
}
$app = $urlmap->to_app();
} else {
require Plack::Middleware::Static;
$app = Plack::Middleware::Static->wrap($app,
path => qr{(?:^|/)static/.*\.(?:js|css|png)$},
root => __DIR__,
encoding => 'utf-8', # encoding for 'text/plain' files
);
}
# it is very similar to run() subroutine, but it would be hard to
# extract common code; note that $*_hook variables can be set only
# for FastCGI, so they are absent here
sub to_psgi_app {
require CGI::Emulate::PSGI;
our $CGI = 'CGI';
our $first_request = 1;
my $app = CGI::Emulate::PSGI->handler(sub {
CGI::initialize_globals();
our $cgi = CGI->new();
run_request();
$first_request = 0;
});
return $app;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment