Subject: Re: gitweb, FastCGI and PSGI/Plack

From: Jakub Narebski <[email-masked]>
Date: Mon, 10 May 2010 01:05:48 +0200

On Sun, 9 May 2010, Tatsuhiko Miyagawa wrote:

> I'm not subscribed to the git mailing list but:
> 
> http://www.spinics.net/lists/git/msg131014.html

You don't need to be subscribed to post to git mailing list.  I personally
read it with a news reader using GMane NNTP interface

  nntp://news.gmane.org/gmane.comp.version-control.git

but sending messages via email, to [email-masked].  Usually people
reply both to original poster and to git mailing list, but just in case
you can mention that you are not subscribed, and to not forget to send
you a CC copy.

All CC re-added.

> 
> > Actually both CGI and CGI::Fast are in Perl core distribution since
> > perl 5.004 (Perl 5.4.0).  I assume that CGI::Fast simply degrades to CGI
> > if FCGI module is not present.
> 
> No, it just dies if FCGI is not installed

Ah, so that is why CGI distribution has FCGI as requirement!

  http://deps.cpantesters.org/?module=CGI;perl=latest

> (so CGI::Fast in core doesn't make any sense).

Yes, it doesn't.

Nevertheless gitweb would try to require CGI::Fast only when it is run
with '--fastcgi' / '--fcgi' / '-f' option.  It should probably check if
it can be require'd without errors, like it is done for FCGI::ProcManager
in
  [RFC/PATCHv2 2/2] gitweb: Add support for FastCGI, using CGI::Fast
  Message-Id: <[email-masked]>
  http://article.gmane.org/gmane.comp.version-control.git/146647

> 
> > JN> Yes, it can.  CGI::Compile is used for example by CGI::Emulate::PSGI,
> > JN> and you can run PSGI app on standalone Perl web server (pure Perl
> > JN> HTTP::Server::PSGI, or HTTP::Server::Simple::PSGI which in turn uses
> > JN> HTTP::Server::Simple, or Starman, or Twiggy, or Perlbal)
> 
> And FastCGI.
> 
> I don't understand why you implemented FastCGI interface *in addition
> to* PSGI/Plack interface.

I didn't implement support for PSGI/Plack, at least not in patch send in
"[PATCH 0/2] gitweb: Add support for running gitweb as FastCGI script"
series.

Adding support for FastCGI to CGI application is as simple as using
CGI::Fast object in place of CGI, and wrapping running main subroutine
in a loop that processes requests, like described in CGI::Fast manpage.
Well, for gitweb it required a few more small changes, anyway...

Moving to PSGI, or adding possibility to run gitweb as PSGI script (like
the series adds *ability* to run gitweb as FastCGI script) by modifying
gitweb would not be that easy, even with help of CGI::PSGI.

CGI::Compile was referring to an alternate approach, where instead of
modifying gitweb to be able to run it as FastCGI script (you can run it
as CGI script and as ModPerl::Registry script from mod_perl) there was
added gitweb.fcgi wrapper:

JN> The alternate solution would be to add gitweb.fcgi wrapper, like e.g.:
JN> in the following patch by Eric Wong
JN> 
JN>  "[PATCH 1/2] gitweb: add a simple wrapper for FCGI support"
JN>  http://thread.gmane.org/gmane.comp.version-control.git/35920/focus=35921
JN>
JN> which was part of the "[0/2 PATCH] FastCGI and nginx support for gitweb"
JN> series.  (Note that the patch does 'do $gitweb_cgi;' without checking for
JN> errors, see the bottom of `perldoc -f do` documentation on how it should
JN> be done).

One can of course use this approach wrapping gitweb to be run on PSGI,
using CGI::Emulate::PSGI (via Plack::App::WrapCGI), which in turn uses
CGI::Compile.  The gitweb.fcgi wrapper could use CGI::Emulate::FCGI...
if it existed.

NB I use the following gitweb.psgi wrapper to run gitweb from "plackup"
for tests (after running "make gitweb", of course, and with appropriate
gitweb_config.perl, unversioned, in gitweb/ alongside gitweb.perl, the
gitweb.psgi wrapper, and generated gitweb.cgi):

-- 8< --
#!/usr/bin/env plackup

# gitweb - simple web interface to track changes in git repositories
#          PSGI wrapper (see http://plackperl.org)

use strict;
use warnings;

use Plack::Builder;
use Plack::App::WrapCGI;
use CGI::Emulate::PSGI 0.07; # minimum version required to work

use File::Spec;
# __DIR__ is taken from Dir::Self __DIR__ fragment
sub __DIR__ () {
	File::Spec->rel2abs(join '', (File::Spec->splitpath(__FILE__))[0, 1]);
}

builder {
	enable 'Static',
		path => sub { m!\.(js|css|png)$! && s!^/gitweb/!! }, root => __DIR__."/";
	Plack::App::WrapCGI->new(script => __DIR__."/gitweb.cgi")->to_app;
}

__END__
-- >8 --

> 
> `plackup -s FCGI` makes your PSGI app a fastcgi handler using FCGI.pm,
> or `plackup -s Net::FastCGI` does the same but using Net::FastCGI,
> pure perl alternative.

It is a pity that Plack::App::WrapFCGI / FCGI::Emulate::PSGI does not
exist, so that gitweb.psgi wrapper would not require indirectly 
CGI::Compile.

> 
> Come over to #plack (irc://irc.perl.org/#plack) for more discussion.

Email not good?

-- 
Jakub Narebski
ShadeHawk on #git, jnareb on #plack
Poland