From fd9e36933e419a5427eb521dfb753d4d11dbf2ec Mon Sep 17 00:00:00 2001 From: Jakub Narebski Date: Tue, 8 May 2012 21:35:08 +0200 Subject: [PATCH] gitweb.psgi: Use installed static files, if they are available When gitweb is running as PSGI app, it must itself take care of serving static files: stylesheets, script, images that are required to render gitweb output. This commit makes gitweb (in PSGI mode) use installed static files from $(gitwebstaticdir) if such directory exists. Before this commit gitweb served static files from 'static/' directory relative to position of gitweb script itself (to __DIR__). Note that mechanism of serving is slightly different: the one with __DIR__ uses Plack::Middleware::Static, while the installdir one uses URLMap-ped set of Plack::App::File. Signed-off-by: Jakub Narebski --- gitweb/Makefile | 7 +++++-- gitweb/gitweb.perl | 38 ++++++++++++++++++++++++++++++-------- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/gitweb/Makefile b/gitweb/Makefile index 549e7dcd4b..6f673ff9a7 100644 --- a/gitweb/Makefile +++ b/gitweb/Makefile @@ -13,6 +13,8 @@ all:: prefix ?= $(HOME) bindir ?= $(prefix)/bin gitwebdir ?= /var/www/cgi-bin +gitwebstaticdir ?= $(gitwebdir)/static +gitweblibdir ?= $(gitwebdir)/lib RM ?= rm -f INSTALL ?= install @@ -58,8 +60,8 @@ PERL_PATH ?= /usr/bin/perl # Shell quote; bindir_SQ = $(subst ','\'',$(bindir))#' gitwebdir_SQ = $(subst ','\'',$(gitwebdir))#' -gitwebstaticdir_SQ = $(subst ','\'',$(gitwebdir)/static)#' -gitweblibdir_SQ = $(subst ','\'',$(gitwebdir)/lib)#' +gitwebstaticdir_SQ = $(subst ','\'',$(gitwebstaticdir))#' +gitweblibdir_SQ = $(subst ','\'',$(gitweblibdir))#' SHELL_PATH_SQ = $(subst ','\'',$(SHELL_PATH))#' PERL_PATH_SQ = $(subst ','\'',$(PERL_PATH))#' DESTDIR_SQ = $(subst ','\'',$(DESTDIR))#' @@ -130,6 +132,7 @@ GITWEB_JSLIB_FILES += static/js/blame_incremental.js GITWEB_REPLACE = \ -e 's|++GIT_VERSION++|$(GIT_VERSION)|g' \ -e 's|++GIT_BINDIR++|$(bindir)|g' \ + -e 's|++GITWEBSTATICDIR++|$(gitwebstaticdir)|g' \ -e 's|++GITWEB_CONFIG++|$(GITWEB_CONFIG)|g' \ -e 's|++GITWEB_CONFIG_SYSTEM++|$(GITWEB_CONFIG_SYSTEM)|g' \ -e 's|++GITWEB_CONFIG_COMMON++|$(GITWEB_CONFIG_COMMON)|g' \ diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl index b82c3665af..28db0e23aa 100755 --- a/gitweb/gitweb.perl +++ b/gitweb/gitweb.perl @@ -1277,9 +1277,10 @@ sub to_psgi_app { } sub build_psgi_app { - require Plack::Builder; - require Plack::Middleware::Static; - + # gitweb currently doesn't work with $SIG{CHLD} set to 'IGNORE', + # because it uses 'close $fd or die...' on piped filehandle $fh + # (which causes the parent process to wait for child to finish). + # this middleware is enabled only if $SIG{CHLD} is 'IGNORE'. my $sigchld_mw = sub { my $app = shift; sub { @@ -1294,14 +1295,35 @@ sub build_psgi_app { # note: Plack::Builder DSL (builder, enable_if, enable) won't work # with "require Plack::Builder" outside BEGIN section. my $app = to_psgi_app(); - $app = Plack::Middleware::Static->wrap($app, - path => qr{(?:^|/)static/.*\.(?:js|css|png)$}, - root => __DIR__, - encoding => 'utf-8', # encoding for 'text/plain' files - ); $app = $sigchld_mw->($app) if (defined $SIG{'CHLD'} && $SIG{'CHLD'} eq 'IGNORE'); + 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 + ); + + } + return $app; } -- 2.11.4.GIT