Skip to content

Instantly share code, notes, and snippets.

@jnareb
Created May 28, 2010 10:35
Show Gist options
  • Save jnareb/417020 to your computer and use it in GitHub Desktop.
Save jnareb/417020 to your computer and use it in GitHub Desktop.
gitweb.psgi generated by git-instaweb
#!/usr/bin/perl
# gitweb - simple web interface to track changes in git repositories
# PSGI wrapper and server starter (see http://plackperl.org)
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
# mimetype mapping (from lighttpd_conf)
Plack::MIME->add_type(
".pdf" => "application/pdf",
".sig" => "application/pgp-signature",
".spl" => "application/futuresplash",
".class" => "application/octet-stream",
".ps" => "application/postscript",
".torrent" => "application/x-bittorrent",
".dvi" => "application/x-dvi",
".gz" => "application/x-gzip",
".pac" => "application/x-ns-proxy-autoconfig",
".swf" => "application/x-shockwave-flash",
".tar.gz" => "application/x-tgz",
".tgz" => "application/x-tgz",
".tar" => "application/x-tar",
".zip" => "application/zip",
".mp3" => "audio/mpeg",
".m3u" => "audio/x-mpegurl",
".wma" => "audio/x-ms-wma",
".wax" => "audio/x-ms-wax",
".ogg" => "application/ogg",
".wav" => "audio/x-wav",
".gif" => "image/gif",
".jpg" => "image/jpeg",
".jpeg" => "image/jpeg",
".png" => "image/png",
".xbm" => "image/x-xbitmap",
".xpm" => "image/x-xpixmap",
".xwd" => "image/x-xwindowdump",
".css" => "text/css",
".html" => "text/html",
".htm" => "text/html",
".js" => "text/javascript",
".asc" => "text/plain",
".c" => "text/plain",
".cpp" => "text/plain",
".log" => "text/plain",
".conf" => "text/plain",
".text" => "text/plain",
".txt" => "text/plain",
".dtd" => "text/xml",
".xml" => "text/xml",
".mpeg" => "video/mpeg",
".mpg" => "video/mpeg",
".mov" => "video/quicktime",
".qt" => "video/quicktime",
".avi" => "video/x-msvideo",
".asf" => "video/x-ms-asf",
".asx" => "video/x-ms-asf",
".wmv" => "video/x-ms-wmv",
".bz2" => "application/x-bzip",
".tbz" => "application/x-bzip-compressed-tar",
".tar.bz2" => "application/x-bzip-compressed-tar",
"" => "text/plain"
);
my $app = builder {
# to be able to override $SIG{__WARN__} to log build time warnings
use CGI::Carp; # it sets $SIG{__WARN__} itself
my $logdir = "/home/jnareb/git/.git/gitweb/plackup";
open my $access_log_fh, '>', "$logdir/access.log"
or die "Couldn't open access log '$logdir/access.log': $!";
open my $error_log_fh, '>', "$logdir/error.log"
or die "Couldn't open error log '$logdir/error.log': $!";
$access_log_fh->autoflush(1);
$error_log_fh->autoflush(1);
# redirect build time warnings to error.log
$SIG{'__WARN__'} = sub {
my $msg = shift;
# timestamp warning like in CGI::Carp::warn
my $stamp = CGI::Carp::stamp();
$msg =~ s/^/$stamp/gm;
print $error_log_fh $msg;
};
# write errors to error.log, access to access.log
enable 'AccessLog',
format => "combined",
logger => sub { print $access_log_fh @_; };
enable sub {
my $app = shift;
sub {
my $env = shift;
$env->{'psgi.errors'} = $error_log_fh;
$app->($env);
}
};
# gitweb currently doesn't work with {CHLD} set to 'IGNORE',
# because it uses 'close or die...' on piped filehandle
# (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/static/!! },
root => "gitweb/static/",
encoding => 'utf-8'; # encoding for 'text/plain' files
# convert CGI application to PSGI app
Plack::App::WrapCGI->new(script => "gitweb/gitweb.cgi")->to_app;
};
# make it runnable as standalone app,
# like it would be run via 'plackup' utility
if (__FILE__ eq $0) {
require Plack::Runner;
my $runner = Plack::Runner->new();
$runner->parse_options(qw(--env deployment --port 1234),
"" ? qw(--host 127.0.0.1) : ());
$runner->run($app);
}
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment