Skip to contentSkip to conversation

gitweb: Proof of concept of PSGI interface

PATCH In review Jakub Narebski 1 participant 7 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

cover gitweb: Proof of concept of PSGI interface

The major goal of this series is to make it possible for gitweb to run as PSGI application, even if non-native (emulated).

PSGI[1][2] is an interface between Perl web applications and web servers. It is accompanied by Plack, a Perl module and toolkit that contains PSGI middleware (e.g. for serving static files), helpers (e.g. 'plackup' command line launcher) and adapters to web servers (e.g. to CGI, FastCGI, or mod_perl). PSGI and Plack are inspired by Python's WSGI and Ruby's Rack.

[1]: http://plackperl.org
[2]: http://search.cpan.org/perldoc?PSGI
[3]: http://advent.plackperl.org

First two patches are independent improvements, and make sense even outside this patch series. Last patch is making use of gitweb as PSGI in "git instaweb --httpd=plackup".

Jakub Narebski (5):
gitweb: Jump do DONE_REQUEST not DONE_GITWEB on error
gitweb: Prepare for splitting gitweb
gitweb: Enable running gitweb as PSGI app, via CGI::Emulate::PSGI gitweb.psgi: Allow passing arguments to Plack::Runner
git-instaweb: Use new PSGI interface mode of gitweb

 Documentation/gitweb.txt |   16 +++++++
 git-instaweb.sh          |   33 +++------------
 gitweb/INSTALL           |    3 +
 gitweb/Makefile          |   14 ++++++-
 gitweb/README            |   13 +++++-
 gitweb/gitweb.perl       |   98 +++++++++++++++++++++++++++++++++++++++++++---
 6 files changed, 141 insertions(+), 36 deletions(-)
Signature · 1 line
1.7.9
#1 lore raw

PATCH 1/5 gitweb: Jump do DONE_REQUEST not DONE_GITWEB on error

On error (die_error()), instead of ending gitweb process by non-local jump to DONE_GITWEB label at the end of requests loop in run() subroutine, just end current request by jumping to DONE_REQUESt at the end of run_request() subroutine.

This change should improve truly persistent deployment methods, currently only FastCGI but in the future also PSGI, as an error page would no longer require reloading whole persistent environment, just end request.

This would allow to create new deployment mechanisms (for example PSGI) based on run_request() and not on run().

Note that DONE_REQUEST label is removed because it is no longer used, and it wouldn't work correctly for PSGI anyway.

---
Without this we could not write to_psgi_app() in patch 3/5.

This patch has its value independently on PSGI support, though if it is to be accepted standalone, the commit message would have to be slighlty edited, so that the last two paragraphs of it read:

"This would allow to create new deployment mechanisms based on
run_request() and not on run().

DONE_REQUEST label is removed because it is no longer used."

 gitweb/gitweb.perl |    9 +++++----
 1 files changed, 5 insertions(+), 4 deletions(-)
gitweb/gitweb.perl +5 -4
index e2e6a73..2cca2cd 100755
@@ -1195,6 +1195,10 @@ sub run_request {
 	configure_gitweb_features();
 
 	dispatch();
+
+
+ DONE_REQUEST:
+	1;
 }
 
 our $is_last_request = sub { 1 };
@@ -1252,9 +1256,6 @@ sub run {
 
 		last REQUEST if ($is_last_request->());
 	}
-
- DONE_GITWEB:
-	1;
 }
 
 run();
@@ -4195,7 +4196,7 @@ EOF
 	print "</div>\n";
 
 	git_footer_html();
-	goto DONE_GITWEB
+	goto DONE_REQUEST
 		unless ($opts{'-error_handler'});
 }
 
Signature · 1 line
1.7.9
#2 lore raw

PATCH 2/5 gitweb: Prepare for splitting gitweb

In reply to Jakub Narebski

Prepare gitweb for having been split into modules that are to be installed alongside gitweb in 'lib/' subdirectory, by adding

use lib __DIR__.'/lib';

to gitweb.perl (to main gitweb script), and preparing for putting modules (relative path) in $(GITWEB_MODULES) in gitweb/Makefile.

---
This patch is the beginning of splitting gitweb (with its 8000+ lines, and more than 240kB) into modules, and is useful independently of being in this patch series.

Sidenote: it is needed for __DIR__ only here.

 gitweb/Makefile    |   14 +++++++++++++-
 gitweb/gitweb.perl |    8 ++++++++
 2 files changed, 21 insertions(+), 1 deletions(-)
gitweb/Makefile +13 -1
index cd194d0..549e7dc 100644
@@ -59,6 +59,7 @@ PERL_PATH  ?= /usr/bin/perl
 bindir_SQ = $(subst ','\'',$(bindir))#'
 gitwebdir_SQ = $(subst ','\'',$(gitwebdir))#'
 gitwebstaticdir_SQ = $(subst ','\'',$(gitwebdir)/static)#'
+gitweblibdir_SQ = $(subst ','\'',$(gitwebdir)/lib)#'
 SHELL_PATH_SQ = $(subst ','\'',$(SHELL_PATH))#'
 PERL_PATH_SQ  = $(subst ','\'',$(PERL_PATH))#'
 DESTDIR_SQ    = $(subst ','\'',$(DESTDIR))#'
@@ -178,12 +179,23 @@ test-installed:
 
 ### Installation rules
 
-install: all
+install: all install-modules
 	$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(gitwebdir_SQ)'
 	$(INSTALL) -m 755 $(GITWEB_PROGRAMS) '$(DESTDIR_SQ)$(gitwebdir_SQ)'
 	$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(gitwebstaticdir_SQ)'
 	$(INSTALL) -m 644 $(GITWEB_FILES) '$(DESTDIR_SQ)$(gitwebstaticdir_SQ)'
 
+install-modules:
+	install_dirs="$(sort $(dir $(GITWEB_MODULES)))" && \
+	for dir in $$install_dirs; do \
+		test -d '$(DESTDIR_SQ)$(gitwebdir_SQ)/$$dir' || \
+		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(gitwebdir_SQ)/$$dir'; \
+	done
+	gitweb_modules="$(GITWEB_MODULES)" && \
+	for mod in $$gitweb_modules; do \
+		$(INSTALL) -m 644 $$mod '$(DESTDIR_SQ)$(gitwebdir_SQ)/$$(dirname $$mod)'; \
+	done
+
 ### Cleaning rules
 
 clean:
gitweb/gitweb.perl +8 -0
index 2cca2cd..22efec2 100755
@@ -10,6 +10,14 @@
 use 5.008;
 use strict;
 use warnings;
+
+use File::Spec;
+# __DIR__ is taken from Dir::Self __DIR__ fragment
+sub __DIR__ () {
+	File::Spec->rel2abs(join '', (File::Spec->splitpath(__FILE__))[0, 1]);
+}
+use lib __DIR__ . '/lib';
+
 use CGI qw(:standard :escapeHTML -nosticky);
 use CGI::Util qw(unescape);
 use CGI::Carp qw(fatalsToBrowser set_message);
Signature · 1 line
1.7.9
#3 lore raw

PATCH 3/5 gitweb: Enable running gitweb as PSGI app, via CGI::Emulate::PSGI

In reply to Jakub Narebski

This commit makes it possible to run gitweb as a PSGI application (see http://plackperl.org), using any of PSGI web servers. This includes ability to run gitweb from command line via Plack::Runner module; just run "./gitweb.cgi --psgi" and point web browser to http://0:5000

Gitweb uses CGI mode by default; to trigger PSGI mode one must either pass --psgi or --plackup option to gitweb script, or the script must be run with *.psgi filename (either copy/rename script, or make symlink ending with *.psgi e.g. gitweb.psgi).

Besides running gitweb itself, converting its run_request() from CGI to PSGI via CGI::Emulate::PSGI, gitweb as PSGI app also serves static
files: scripts, stylesheets and images via Plack::Middleware::Static. Currently it assumes that positions and URLs of those assets are left at their default values, namely that URLs end with "/static/<file>", and that files can be found in "static/" directory relative to the gitweb script itself. This assumption should be relaxed in future.

Currently "git instaweb --httpd=plackup" doesn't (yet!) use this newly introduced feature: it is left for future commit.

---
The major part of this series.

"./gitweb.cgi --psgi" runs web server on http://0:5000/

 Documentation/gitweb.txt |   16 +++++++++
 gitweb/INSTALL           |    3 ++
 gitweb/README            |   13 ++++++--
 gitweb/gitweb.perl       |   80 ++++++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 107 insertions(+), 5 deletions(-)
Documentation/gitweb.txt +16 -0
index 605a085..157903b 100644
@@ -473,6 +473,22 @@ With that configuration the full path to browse repositories would be:
 
   http://server/gitweb
 
+As PSGI using plackup
+~~~~~~~~~~~~~~~~~~~~~
+Gitweb can run as PSGI app (via emulation with *CGI::Emulate::PSGI*(3pm)).
+First you need to rename, copy or symlink gitweb.cgi to gitweb.psgi.
+You can run gitweb as a PSGI application from 'plackup' command line
+utility, using any *PSGI*(3) web server (see http://plackperl.org for
+a list), for example:
+
+    $ plackup --server HTTP::Server::Simple --port 8080 \
+      --host 127.0.0.1 gitweb.psgi
+
+With that configuration the full path to browse repositories would be:
+
+  http://127.0.0.1:8080/
+
+See *plackup*(1) manpage for more details.
 
 ADVANCED WEB SERVER SETUP
 -------------------------
gitweb/INSTALL +3 -0
index 6d45406..9f55ab4 100644
@@ -36,6 +36,9 @@ Requirements
 The following optional Perl modules are required for extra features
  - Digest::MD5 - for gravatar support
  - CGI::Fast and FCGI - for running gitweb as FastCGI script
+ - CGI::Emulate::PSGI, Plack::Builder, Plack::Middleware::Static
+   for running gitweb as PSGI application, and Plack::Runner to
+   make it runnable as a standalone script
  - HTML::TagCloud - for fancy tag cloud in project list view
  - HTTP::Date or Time::ParseDate - to support If-Modified-Since for feeds
 
gitweb/README +10 -3
index 6da4778..a435f9b 100644
@@ -56,9 +56,16 @@ See also gitweb.conf(5) manpage.
 Web server configuration
 ------------------------
 Gitweb can be run as CGI script, as legacy mod_perl application (using
-ModPerl::Registry), and as FastCGI script.  You can find some simple examples
-in "Example web server configuration" section in INSTALL file for gitweb (in
-gitweb/INSTALL).
+ModPerl::Registry), as FastCGI script, and as PSGI application.  You
+can find some simple examples in "Example web server configuration"
+section in INSTALL file for gitweb (in gitweb/INSTALL).
+
+Note that to run as FastCGI script gitweb must be run with *.fcgi
+extension or with `--fastcgi` / `--fcgi` parameter on command line.
+To run as PSGI application gitweb must be run with *.psgi extension,
+or with `--psgi` / `--plackup` parameter on command line, or from
+PSGI web server which sets 'PLACK_ENV' or 'PLACK_SERVER' environment
+variable.
 
 See "Webserver configuration" and "Advanced web server setup" sections in
 gitweb(1) manpage.
gitweb/gitweb.perl +78 -2

114 lines not on this page · raw message

Signature · 1 line
1.7.9
#4 lore raw

PATCH 4/5 gitweb.psgi: Allow passing arguments to Plack::Runner

In reply to Jakub Narebski

Command options after first '--' are passed literally to Plack::Runner as arguments, in addition to default ones used. This way you can select PSGI server, host, port, etc.

---
Could be squashed with 4/5, now that I realized that it is that simple.

 gitweb/gitweb.perl |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
gitweb/gitweb.perl +2 -1
index 0dbdd15..6bd7b08 100755
@@ -1312,7 +1312,8 @@ sub run_psgi_app {
 
 		my $runner = Plack::Runner->new();
 		$runner->parse_options(qw(--env deployment),
-		                       qw(--host 127.0.0.1));
+		                       qw(--host 127.0.0.1),
+		                       @ARGV);
 		$runner->run($app);
 	}
 }
Signature · 1 line
1.7.9
#5 lore raw

PATCH 5/5 git-instaweb: Use new PSGI interface mode of gitweb

In reply to Jakub Narebski

Now that gitweb supports PSGI interface itself, use it instead of Plack::App::WrapCGI.

Note that gitweb as PSGI serves static files and ensures correct handling of SIGCHLD, but the wrapper is still needed for logging, mimetype handling (probably unnecessary), and selecting host and port. It is now named gitweb-wrapper.psgi and not gitweb.psgi, though.

The advantage of previous version is that it uses persistent mode (similar to FastCGI); important if $per_request_config is false or coderef.

---
Should make git-instaweb plackup mode better.

 git-instaweb.sh |   33 +++++++--------------------------
 1 files changed, 7 insertions(+), 26 deletions(-)
git-instaweb.sh +7 -26
index 01a1b05..58e142a 100755
@@ -53,8 +53,8 @@ resolve_full_httpd () {
 		fi
 		;;
 	*plackup*)
-		# server is started by running via generated gitweb.psgi in $fqgitdir/gitweb
-		full_httpd="$fqgitdir/gitweb/gitweb.psgi"
+		# server is started by running via generated gitweb-wrapper.psgi in $fqgitdir/gitweb
+		full_httpd="$fqgitdir/gitweb/gitweb-wrapper.psgi"
 		httpd_only="${httpd%% *}" # cut on first space
 		return
 		;;
@@ -434,7 +434,7 @@ EOF
 plackup_conf () {
 	# generate a standalone 'plackup' server script in $fqgitdir/gitweb
 	# with embedded configuration; it does not use "$conf" file
-	cat > "$fqgitdir/gitweb/gitweb.psgi" <<EOF
+	cat > "$fqgitdir/gitweb/gitweb-wrapper.psgi" <<EOF
 #!$PERL
 
 # gitweb - simple web interface to track changes in git repositories
@@ -445,8 +445,7 @@ use strict;
 use IO::Handle;
 use Plack::MIME;
 use Plack::Builder;
-use Plack::App::WrapCGI;
-use CGI::Emulate::PSGI 0.07; # minimum version required to work with gitweb
+use Plack::Util;
 
 # mimetype mapping (from lighttpd_conf)
 Plack::MIME->add_type(
@@ -538,25 +537,7 @@ my \$app = builder {
 			\$app->(\$env);
 		}
 	};
-	# 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).
-	enable_if { \$SIG{'CHLD'} eq 'IGNORE' } sub {
-		my \$app = shift;
-		sub {
-			my \$env = shift;
-			local \$SIG{'CHLD'} = 'DEFAULT';
-			local \$SIG{'CLD'}  = 'DEFAULT';
-			\$app->(\$env);
-		}
-	};
-	# serve static files, i.e. stylesheet, images, script
-	enable 'Static',
-		path => sub { m!\.(js|css|png)\$! && s!^/gitweb/!! },
-		root => "$root/",
-		encoding => 'utf-8'; # encoding for 'text/plain' files
-	# convert CGI application to PSGI app
-	Plack::App::WrapCGI->new(script => "$root/gitweb.cgi")->to_app;
+	Plack::Util::load_psgi("$root/gitweb.cgi");
 };
 
 # make it runnable as standalone app,
@@ -574,8 +555,8 @@ if (caller) {
 __END__
 EOF
 
-	chmod a+x "$fqgitdir/gitweb/gitweb.psgi"
-	# configuration is embedded in server script file, gitweb.psgi
+	chmod a+x "$fqgitdir/gitweb/gitweb-wrapper.psgi"
+	# configuration is embedded in server script file, gitweb-wrapper.psgi
 	rm -f "$conf"
 }
 
Signature · 1 line
1.7.9
#6 lore raw

[RFC/PATCH 6/5] gitweb.psgi: Use installed static files, if they are available

In reply to Jakub Narebski

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.

---
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(-)
gitweb/Makefile +5 -2
index 549e7dc..6f673ff 100644
@@ -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' \
gitweb/gitweb.perl +30 -8
index 6bd7b08..f871090 100755
@@ -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;
 }
 
Signature · 1 line
1.7.9