git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jakub Narebski <jnareb@gmail.com>
To: git@vger.kernel.org
Cc: Jakub Narebski <jnareb@gmail.com>
Subject: [RFC/PATCH 6/5] gitweb.psgi: Use installed static files, if they are available
Date: Thu,  8 Mar 2012 15:06:06 +0100	[thread overview]
Message-ID: <1331215566-11637-1-git-send-email-jnareb@gmail.com> (raw)
In-Reply-To: <1330797423-22926-1-git-send-email-jnareb@gmail.com>

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__).

This allows to use gitweb in PSGI mode even if static files are
installed to non-standard place.

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 <jnareb@gmail.com>
---
This probably would have to be reworked, so that using Plack::Middleware::Static
is preferred over set of Plack::App::File (which I think is slightly slower).

 gitweb/Makefile    |    7 +++++--
 gitweb/gitweb.perl |   38 ++++++++++++++++++++++++++++++--------
 2 files changed, 35 insertions(+), 10 deletions(-)

diff --git a/gitweb/Makefile b/gitweb/Makefile
index 549e7dc..6f673ff 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 6bd7b08..f871090 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -1271,9 +1271,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 {
@@ -1288,14 +1289,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;
 }
 
-- 
1.7.9

      parent reply	other threads:[~2012-03-08 14:06 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-03 17:56 [RFC/PATCH 0/5] gitweb: Proof of concept of PSGI interface Jakub Narebski
2012-03-03 17:56 ` [RFC/PATCH 1/5] gitweb: Jump do DONE_REQUEST not DONE_GITWEB on error Jakub Narebski
2012-03-03 17:57 ` [RFC/PATCH 2/5] gitweb: Prepare for splitting gitweb Jakub Narebski
2012-03-03 17:57 ` [RFC/PATCH 3/5] gitweb: Enable running gitweb as PSGI app, via CGI::Emulate::PSGI Jakub Narebski
2012-03-03 17:57 ` [RFC/PATCH 4/5] gitweb.psgi: Allow passing arguments to Plack::Runner Jakub Narebski
2012-03-03 17:57 ` [RFC/PATCH 5/5] git-instaweb: Use new PSGI interface mode of gitweb Jakub Narebski
2012-03-08 14:06 ` Jakub Narebski [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1331215566-11637-1-git-send-email-jnareb@gmail.com \
    --to=jnareb@gmail.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).