Skip to contentSkip to conversation

git-instaweb: Add support for running gitweb via 'plackup'

RFC/PATCH Superseded Jakub Narebski 3 participants 12 messages Jun 15, 2016 Documentation

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PSGI is an interface between Perl web applications and web servers, and Plack is a Perl module and toolkit that contains PSGI middleware, helpers and adapters to web servers; see http://plackperl.org

PSGI and Plack are inspired by Python's WSGI and Ruby's Rack.

Plack core distribution includes HTTP::Server::PSGI, a reference PSGI standalone web server implementation. plackup is a command line launcher to run PSGI applications from command line, connecting web app to a web server; it uses Plack::Runner module. It uses HTTP::Server::PSGI as a web server by default.

git-instaweb generates gitweb.psgi wrapper, which uses Plack::App::WrapCGI to compile a CGI script into a PSGI application using CGI::Compile and CGI::Emulate::PSGI, and runs it with any PSGI server as a PSGI application.

To run git-instaweb with '--httpd=plackup', you need to have instaled Plack core, CGI::Emulate::PSGI, CGI::Compile, and available for a Perl script (e.g. by setting PERL5LIB environment variable).

---
Compared to earlier version (only mentioned, but not sent to git mailing list, if I remember correctly), instead of configuring plackup from command line (by modifying $httpd variable), the configuration is embedded in generated gitweb.psgi file. Also instead of using plackup, the generated gitweb.psgi is made into standalone app, running server (so $full_httpd is full path to gitweb.psgi).

RFC is because when when running

  $ ./git-instaweb --httpd=plackup --browser=lynx

I get the following error

Looking up 127.0.0.1:1234
Making HTTP connection to 127.0.0.1:1234
Alert!: Unable to connect to remote host.

lynx: Can't access startfile http://127.0.0.1:1234/
http://127.0.0.1:1234

But running 'lynx http://127.0.0.1:1234/' after this works correctly without problems. Running './.git/gitweb/gitweb.psgi' doesn't cause any problems either. WTF?

HELP!!!

 Documentation/git-instaweb.txt |    2 +-
 git-instaweb.sh                |   52 +++++++++++++++++++++++++++++++++++++--
 2 files changed, 50 insertions(+), 4 deletions(-)
Documentation/git-instaweb.txt +1 -1
index a1f17df..2c3c4d2 100644
@@ -29,7 +29,7 @@ OPTIONS
 	The HTTP daemon command-line that will be executed.
 	Command-line options may be specified here, and the
 	configuration file will be added at the end of the command-line.
-	Currently apache2, lighttpd, mongoose and webrick are supported.
+	Currently apache2, lighttpd, mongoose, plackup and webrick are supported.
 	(Default: lighttpd)
 
 -m::
git-instaweb.sh +49 -3

76 lines not on this page · raw message

Signature · 1 line
1.7.0.1
#1 lore raw
Quoted 1 line · Jakub Narebski

---

[...]

Quoted 10 lines

RFC is because when when running

  $ ./git-instaweb --httpd=plackup --browser=lynx

I get the following error

Looking up 127.0.0.1:1234
Making HTTP connection to 127.0.0.1:1234
Alert!: Unable to connect to remote host.

Quoted 6 lines

lynx: Can't access startfile http://127.0.0.1:1234/
http://127.0.0.1:1234

But running 'lynx http://127.0.0.1:1234/' after this works correctly without problems. Running './.git/gitweb/gitweb.psgi' doesn't cause any problems either. WTF?

I think I know what might be happening here. The plackup / Plack::Runner needs some time to setup and start running HTTP::Server::PSGI based web server. When it is ready to serve requests (web pages), it prints to stderr

<server>: Accepting connections at http://<host>:<port>/

In the case of .git/gitweb/gitweb.psgi script generated by git-instaweb it is:

HTTP::Server::PSGI: Accepting connections at http://0:1234/

But plackup / Plack::Runner is run in background (HTTP::Server::PSGI does not support '--daemonize'), and most probably the web browser is started before web server is not ready.

Do you have any idea on how to synchronize those two processes, the web server and the web client (web browser), so that web browser is run only after web server prints that it is ready to accept connection?

TIA.

Quoted 13 lines
git-instaweb.sh +3 -3
@@ -83,9 +90,9 @@ start_httpd () {
 
 	# don't quote $full_httpd, there can be arguments to it (-f)
 	case "$httpd" in
-	*mongoose*)
-		#The mongoose server doesn't have a daemon mode so we'll have to fork it
-		$full_httpd "$fqgitdir/gitweb/httpd.conf" &
+	*mongoose*|*plackup*)
+		#The mongoose server and plackup don't have a daemon mode so we'll have to fork it
+		$full_httpd "$fqgitdir/gitweb/httpd.conf" 2>"$fqgitdir/error_log" &
 		#Save the pid before doing anything else (we'll print it later)
 		pid=$!
 
Signature · 2 lines
Jakub Narebski
Poland
#2 lore raw

PATCH v2 1/1 git-instaweb: Add support for running gitweb via 'plackup'

PSGI is an interface between Perl web applications and web servers, and Plack is a Perl module and toolkit that contains PSGI middleware, helpers and adapters to web servers; see http://plackperl.org

PSGI and Plack are inspired by Python's WSGI and Ruby's Rack.

Plack core distribution includes HTTP::Server::PSGI, a reference PSGI standalone web server implementation. plackup is a command line launcher to run PSGI applications from command line, connecting web app to a web server via Plack::Runner module. By default it uses HTTP::Server::PSGI as a web server.

git-instaweb generates gitweb.psgi wrapper (in $GIT_DIR/gitweb). This wrapper uses Plack::App::WrapCGI to compile gitweb.cgi (which is a CGI script) into a PSGI application using CGI::Compile and CGI::Emulate::PSGI. git-instaweb then runs this wrapper, using by default HTTP::Server::PSGI standalone Perl server.

The configuration for 'plackup' is currently embedded in generated gitweb.psgi wrapper, instead of using httpd.conf ($conf).

To run git-instaweb with '--httpd=plackup', you need to have instaled Plack core, CGI::Emulate::PSGI, CGI::Compile. Those modules have to be available for Perl scripts (which can be done for example by setting PERL5LIB environment variable). This is currently not documented.

---
It lives^W works! I have added 'httpd_is_ready' function, which ensures that server is fully started before running web browser. Currently it is non-trivial only for '--httpd=plackup'.

Currently gitweb.psgi wrapper uses Perl she-bang line, and Plack::Runner module (if gitweb.psgi is run as a standalone script) to configure used PSGI server in a server-agnostic way.

Attempts which didn't work or wouldn't work:

* Configuring plackup via command line options, i.e. setting $httpd to e.g.

     httpd="plackup --port=$port --host=127.0.0.1"

But this wouldn't preserve options across server restart.

* Using

    #!/usr/bin/env plackup --port=$port --host=127.0.0.1

as a she-bang line for gitweb.psgi wrapper. It doesn't work because (in most cases) system passes everything after interpreter name as a _single_ option; in this case it would try to run

    /usr/bin/env 'plackup --port=$port --host=127.0.0.1' ...

It also requires for plackup to be in $PATH.

* Using

    #!$PLACKUP --port=$port --host=127.0.0.1

as a she-bang line for gitweb.psgi wrapper, after finding full path to 'plackup' and putting it in $PLACKUP variable (similar to $PERL). It doesn't work because (in most cases) system passes everything after interpreter name as a _single_ option; in this case it would try to run

    /path/to/plackup '--port=$port --host=127.0.0.1' ...

Well, it would work if git-instaweb is run without '--local'...

 Documentation/git-instaweb.txt |    2 +-
 git-instaweb.sh                |   71 +++++++++++++++++++++++++++++++++++++---
 2 files changed, 67 insertions(+), 6 deletions(-)
Documentation/git-instaweb.txt +1 -1
index a1f17df..2c3c4d2 100644
@@ -29,7 +29,7 @@ OPTIONS
 	The HTTP daemon command-line that will be executed.
 	Command-line options may be specified here, and the
 	configuration file will be added at the end of the command-line.
-	Currently apache2, lighttpd, mongoose and webrick are supported.
+	Currently apache2, lighttpd, mongoose, plackup and webrick are supported.
 	(Default: lighttpd)
 
 -m::
git-instaweb.sh +66 -5

106 lines not on this page · raw message

Signature · 1 line
1.7.0.1
#3 lore raw

PSGI is an interface between Perl web applications and web servers, and Plack is a Perl module and toolkit that contains PSGI middleware, helpers and adapters to web servers; see http://plackperl.org

PSGI and Plack are inspired by Python's WSGI and Ruby's Rack.

Plack core distribution includes HTTP::Server::PSGI, a reference PSGI standalone web server implementation. plackup is a command line launcher to run PSGI applications from command line, connecting web app to a web server via Plack::Runner module. By default it uses HTTP::Server::PSGI as a web server.

git-instaweb generates gitweb.psgi wrapper (in $GIT_DIR/gitweb). This wrapper uses Plack::App::WrapCGI to compile gitweb.cgi (which is a CGI script) into a PSGI application using CGI::Compile and CGI::Emulate::PSGI. git-instaweb then runs this wrapper, using by default HTTP::Server::PSGI standalone Perl server.

The configuration for 'plackup' is currently embedded in generated gitweb.psgi wrapper, instead of using httpd.conf ($conf).

To run git-instaweb with '--httpd=plackup', you need to have instaled Plack core, CGI::Emulate::PSGI, CGI::Compile. Those modules have to be available for Perl scripts (which can be done for example by setting PERL5LIB environment variable). This is currently not documented.

---
This is an RFC because it should be split into three patches:
* a patch which makes git-instaweb remove pid-file after stopping server,
* a patch adding and using httpd_is_ready function,
* finally, a patch adding support for 'plackup' Perl web server.

Also, it somwehat lacks documentation.

Differences from previous version (v2):
* Make 'plackup' / Plack::Runner run in 'deployment' mode, and not in default 'development' mode.

* Use generic httpd_is_ready function, which checks if web server is available by checking if it connect to INET port $port on localhost.

* Instead of running 'plackup' / Plack::Runner in the default development mode, and redirecting its stderr to "$fqgitdir/error_log", use the same files for access log and for error log as for other web servers.

* Make if possible to run 'plackup' on HTTP::Server::Simple::PSGI, by setting PLACK_SERVER environment variable to HTTP::Server::Simple, even though it sets $SIG{CHLD} to 'IGNORE', which would be incompatibile with gitweb (see comments in code).

* A few cleanups.

 Documentation/git-instaweb.txt |    2 +-
 git-instaweb.sh                |  102 ++++++++++++++++++++++++++++++++++++++--
 2 files changed, 99 insertions(+), 5 deletions(-)
Documentation/git-instaweb.txt +1 -1
index a1f17df..2c3c4d2 100644
@@ -29,7 +29,7 @@ OPTIONS
 	The HTTP daemon command-line that will be executed.
 	Command-line options may be specified here, and the
 	configuration file will be added at the end of the command-line.
-	Currently apache2, lighttpd, mongoose and webrick are supported.
+	Currently apache2, lighttpd, mongoose, plackup and webrick are supported.
 	(Default: lighttpd)
 
 -m::
git-instaweb.sh +98 -4

137 lines not on this page · raw message

Signature · 1 line
1.7.0.1
#4 lore raw
Quoted 2 lines · Jakub Narebski

The configuration for 'plackup' is currently embedded in generated gitweb.psgi wrapper, instead of using httpd.conf ($conf).

A question: is this solution acceptable? Or should the configuration (port, address if git-instaweb was run with '--local', root) be in a separate file.

Webrick, which is in similar situation, uses httpd.conf config file in YAML. Unfortunately there is no config / serialization reader in Perl core, so if we have to go this route either the config file would have to be in Perl, or gitweb.psgi wrapper would have to include some simple config file parsing.

Signature · 2 lines
Jakub Narebski
Poland
#5 lore raw
Quoted 8 lines · Jakub Narebski
Quoted 2 lines · Jakub Narebski

The configuration for 'plackup' is currently embedded in generated gitweb.psgi wrapper, instead of using httpd.conf ($conf).

A question: is this solution acceptable? Or should the configuration (port, address if git-instaweb was run with '--local', root) be in a separate file.

Hi Jakub,

Yes, the psgi wrapper is acceptable and probably ideal.

My understanding is that the .psgi file is analogous to a .ru file in the (Ruby) Rack world[1], and having only a .ru file is probably how I would add Rack support to git-instaweb if WEBrick weren't in Ruby core[2].

Quoted 5 lines

Webrick, which is in similar situation, uses httpd.conf config file in YAML. Unfortunately there is no config / serialization reader in Perl core, so if we have to go this route either the config file would have to be in Perl, or gitweb.psgi wrapper would have to include some simple config file parsing.

I don't think we need parsing in the gitweb.psgi wrapper, we'll just consider gitweb.psgi the config file. This is also the case for Rack .ru files, which can be made executable and have a shebang pointing to "rackup".

[1] - I'm not familiar with Plack/PSGI at all, but I've been dabbling quite a bit in Rack over the past year or two.

[2] - of course since WEBrick is in Ruby core and WEBrick supports CGI scripts out-of-the-box (unlike Rack), adding Rack support to instaweb would be a waste of time.

Signature · 1 line
Eric Wong
#6 lore raw
Quoted 10 lines · Eric Wong
Quoted 8 lines · Jakub Narebski
Quoted 2 lines · Jakub Narebski

The configuration for 'plackup' is currently embedded in generated gitweb.psgi wrapper, instead of using httpd.conf ($conf).

A question: is this solution acceptable? Or should the configuration (port, address if git-instaweb was run with '--local', root) be in a separate file.

Quoted 8 lines

Hi Jakub,

Yes, the psgi wrapper is acceptable and probably ideal.

My understanding is that the .psgi file is analogous to a .ru file in the (Ruby) Rack world[1], and having only a .ru file is probably how I would add Rack support to git-instaweb if WEBrick weren't in Ruby core[2].

Yes, .psgi file in the (Perl) PSGI/Plack world is, as I understand it, analogous to .ru file in the (Ruby) Rack world. PSGI/Plack was inspired by WSGI from Python world and Rack from Ruby world.

There is no web server in Perl core, although probably HTTP::Server::Simple and HTTP::Engine comes close. Well, there is HTTP::Server::Brick inspired by WEBrick...

Using PSGI/Plack (via wrapper around CGI) allows easily to serve gitweb's static files (stylesheet, images, etc.), gives persistent environment (although this probably doesn't matter much for git-instaweb), and allows for easy switching between different PSGI-capable (e.g. via PSGI handlers) web servers, including the default standalone HTTP::Server::PSGI (aka Standalone), HTTP::Server::Simple, preforking Starlet or Starman, currently via PLACK_SERVER environment variable.

Quoted 8 lines
Quoted 5 lines

Webrick, which is in similar situation, uses httpd.conf config file in YAML. Unfortunately there is no config / serialization reader in Perl core, so if we have to go this route either the config file would have to be in Perl, or gitweb.psgi wrapper would have to include some simple config file parsing.

I don't think we need parsing in the gitweb.psgi wrapper, we'll just consider gitweb.psgi the config file.

Well, if it is all right, I would keep it as it is now (rebased on top of Pavan's work related to GSoC 2010). If we would want to keep config separate from wrapper (e.g. by installing wrappers in $(gitwebdir) together with gitweb.cgi, but keeping config files in ~/.gitweb or .git/gitweb), we can split it later.

Quoted 2 lines

This is also the case for Rack .ru files, which can be made executable and have a shebang pointing to "rackup".

Well, generated gitweb.psgi wrapper could have e.g.

#!/usr/bin/env plackup

or

#!$PLACKUP

(after putting full path to 'plackup' in $PLACKUP) as a she-bang line ('plackup' is PSGI/Plack equivalent inspired by 'rackup')... if not for the fact that (as I wrote earlier) different web servers supported by 'plackup' utility have different ways of configuring them. The common API is 'plackup' command line options.

However in most operating systems everything after the interpreter in the she-bang line is passed as *single* option, so for example generating first line of gitweb.psgi wrapper as

  #!/usr/bin/evn plackup --port=1234 --host=127.0.0.1

wouldn't work. That is why I used Plack::Runner instead, the module behind 'plackup'.

As a nice side effect now you don't need to have 'plackup' in $PATH; you only need to have Plack::Runner in $PERL5LIB (or equivalent), but you need to have other required modules there anyway.

Quoted 6 lines

[1] - I'm not familiar with Plack/PSGI at all, but I've been dabbling quite a bit in Rack over the past year or two.

[2] - of course since WEBrick is in Ruby core and WEBrick supports CGI scripts out-of-the-box (unlike Rack), adding Rack support to instaweb would be a waste of time.

Signature · 2 lines
Jakub Narebski
Poland
#7 lore raw
In reply to Jakub Narebski

2010/5/22 Jakub Narebski <[email protected]>:

Quoted 5 lines

Webrick, which is in similar situation, uses httpd.conf config file in YAML.  Unfortunately there is no config / serialization reader in Perl core, so if we have to go this route either the config file would have to be in Perl, or gitweb.psgi wrapper would have to include some simple config file parsing.

You could easily do (pseudocode):

    unless (eval { require Config::Any; 1 }) {
        do $conf_file;
    } else {
        my $parser = Config::Any->new( ... );
         $parser->docnf( $conf_file );
     }

Then you'd read (eval) a perl config file by default, but remain compatible with saner config formats for users prepared to install things from the CPAN.

#10 lore raw
In reply to Jakub Narebski
Quoted 3 lines · Jakub Narebski

Do you have any idea on how to synchronize those two processes, the web server and the web client (web browser), so that web browser is run only after web server prints that it is ready to accept connection?

Your polling http_is_ready() function is fine, and really the only portable solution across all servers (and OSes).

But if your webserver lets you run arbitrary code hooks after it binds the listen socket, what I do when writing integration tests is have it open a FIFO for writing.

Adapted and abridged from the Rainbows! web server test suite, the following config snippet is from the config file for the Rainbows! web server:
---------------- rainbows.conf.rb ----------------- after_fork do |server, worker|

  # test script will block while reading from the FIFO,
  # so notify the script on the first worker we spawn
  # by opening the FIFO
  if worker.nr == 0
    File.open("/path/to/fifo", "wb") { |fp| fp.syswrite "START" }
  end
end
---------------------------------------------------

And then in the test script:
----------- my-integration-test.sh ---------------- set -e
mkfifo /path/to/fifo

# start the server
rainbows --daemonize --config-file rainbows.conf.rb

# "cat /path/to/fifo" will block until the after_fork hook is called

# in rainbows.conf.rb
test xSTART = x"$(cat /path/to/fifo)"
# server is up, run the rest of the tests...
---------------------------------------------------
Signature · 1 line
Eric Wong
#11 lore raw
Quoted 8 lines · Eric Wong
Quoted 4 lines · Jakub Narebski

Do you have any idea on how to synchronize those two processes, the web server and the web client (web browser), so that web browser is run only after web server prints that it is ready to accept connection?

Your polling http_is_ready() function is fine, and really the only portable solution across all servers (and OSes).

Well, even if we use the solution below it would be good fallback for those web servers, for which the solution below cannot be used.

Quoted 4 lines

But if your webserver lets you run arbitrary code hooks after it binds the listen socket, what I do when writing integration tests is have it open a FIFO for writing.

I was thinking about 'flock' function in Perl (and similar in other languages), and '/usr/bin/flock' utility from util-linux. But using FIFO as a semaphore might be a better idea: it is slighly more portable, I guess.

Anyway, Plack handlers have (undocumented!) 'server_ready' option to web servers, containing callback (code reference). It can be set via parse_options method of Plack::Runner, for example. It is used by 'plackup' to print

$server: Accepting connections at $proto://$host:$port

in the (default) "development" mode.

Quoted 22 lines

Adapted and abridged from the Rainbows! web server test suite, the following config snippet is from the config file for the Rainbows! web server:
---------------- rainbows.conf.rb ----------------- after_fork do |server, worker|

  # test script will block while reading from the FIFO,
  # so notify the script on the first worker we spawn
  # by opening the FIFO
  if worker.nr == 0
    File.open("/path/to/fifo", "wb") { |fp| fp.syswrite "START" }
  end
end
---------------------------------------------------

And then in the test script:
----------- my-integration-test.sh ---------------- set -e
mkfifo /path/to/fifo

# start the server
rainbows --daemonize --config-file rainbows.conf.rb
Quoted 6 lines

# "cat /path/to/fifo" will block until the after_fork hook is called

# in rainbows.conf.rb
test xSTART = x"$(cat /path/to/fifo)"
# server is up, run the rest of the tests...
---------------------------------------------------

I wonder how portable is that technique...

It certainly could be done in gitweb.psgi; the question is if it is it worth it...

Signature · 2 lines
Jakub Narebski
Poland
#8 lore raw
In reply to Ævar Arnfjörð Bjarmason
Quoted 17 lines · Ævar Arnfjörð Bjarmason

2010/5/22 Jakub Narebski <[email protected]>:

Quoted 6 lines

Webrick, which is in similar situation, uses httpd.conf config file in YAML. Unfortunately there is no config / serialization reader in Perl core, so if we have to go this route either the config file would have to be in Perl, or gitweb.psgi wrapper would have to include some simple config file parsing.

You could easily do (pseudocode):

    unless (eval { require Config::Any; 1 }) {
    	do $conf_file;
    } else {
    	my $parser = Config::Any->new( ... );
    	$parser->docnf( $conf_file );
    }
Quoted 3 lines

Then you'd read (eval) a perl config file by default, but remain compatible with saner config formats for users prepared to install things from the CPAN.

I was thinking more about something like that:

  my @opts = qw(--env deployment);
  while (<>) {
  	chomp;
  	next unless $_;  # skip empty lines
  	next if /^\s*#/; # skip comments
  	if (/^\s*(.*?)\s*=\s*(.*)\s*$/) {
  		push @opts, '--'.lc($1), $2;
  	}
  }

where httpd.conf would look like this:

port = 1234
host = 127.0.0.1

Anyway, if such code is to be be added, it would be added in a separate commit.

Signature · 2 lines
Jakub Narebski
Poland
#9 lore raw
In reply to Jakub Narebski
Quoted 5 lines · Jakub Narebski

---
This is an RFC because it should be split into three patches:
* a patch which makes git-instaweb remove pid-file after stopping server,
* a patch adding and using httpd_is_ready function,
* finally, a patch adding support for 'plackup' Perl web server.

I'll wait with resending it as a patch series for Pavan work on git-instaweb to be in, to not force him to do additional work on the new 'plackup'-related code in git-instaweb.

Quoted 2 lines

Also, it somwehat lacks documentation.

Hmmm... where should the documentation about requirements etc. of specific supported by git-instaweb web servers be added?

Signature · 2 lines
Jakub Narebski
Poland