diff options
| -rw-r--r-- | Makefile | 4 | ||||
| -rwxr-xr-x | gorillanest.pl.cgi | 100 | ||||
| -rwxr-xr-x | gorillanest.pl.fcgi | 22 | ||||
| -rw-r--r-- | lighttpd-cgi.conf | 21 | ||||
| -rw-r--r-- | lighttpd-fcgi.conf (renamed from lighttpd.conf) | 2 |
5 files changed, 88 insertions, 61 deletions
@@ -1,2 +1,2 @@ -serve: - lighttpd -D -f ./lighttpd.conf +serve-dev: + lighttpd -D -f ./lighttpd-cgi.conf diff --git a/gorillanest.pl.cgi b/gorillanest.pl.cgi index 49a6e4e..8f688aa 100755 --- a/gorillanest.pl.cgi +++ b/gorillanest.pl.cgi @@ -3,9 +3,7 @@ use strict; use warnings; use CGI; -use FCGI; use Switch::Back; -use Syntax::Keyword::Try; use Template; use URI::Escape; use Cwd; @@ -19,6 +17,8 @@ sub info { warn join(' ', @_); } +our $template = Template->new({INCLUDE_PATH => 'template'}); + # significant dirs only sub GN::directories { my $root = $_[0]; @@ -61,61 +61,45 @@ sub GN::repository { # /$username/$repository return \%data; } -sub GN::main { - open STDERR, '>', LOG_FILE or die LOG_FILE . ": $!"; - my $sock = FCGI::OpenSocket('/tmp/gorillanest.socket', 100); - try { - my $root = GIT_ROOT; - my $dbfile = DB_FILE; - my %data = ( - found => 0, - access => 0, - ); - - my $request = FCGI::Request(\*STDIN, \*STDOUT, \*STDERR, \%ENV, $sock); - my $template = Template->new({INCLUDE_PATH => TEMPLATE_ROOT}); - my $head = 0; - my $a_template; - while($request->Accept() >= 0) { - $data{access} += 1; - my $cgi = CGI->new; - my %header = ( - -Content_Type => 'text/html', - -charset => 'UTF-8', - ); - my $method = $ENV{'REQUEST_METHOD'} || ''; - my $uri = $ENV{'REQUEST_URI'} || '/'; - if ($method eq 'HEAD') { - $head = 1; - } elsif ($method eq 'GET') { - ($data{username}, $data{repository}) = $uri =~ USER_REPOSITORY; - info("name:", $data{username} || '', "repo:", $data{repository} || ''); - if ($uri eq '/') { - %data = %{GN::index($root, \%data)}; - $a_template = "index.tt"; - } elsif ($data{repository}) { - %data = %{GN::repository($root, \%data)}; - $a_template = "repository.tt"; - } elsif ($data{username}) { - %data = %{GN::user($root, \%data)}; - $a_template = "index_user.tt"; - } - if (!$data{found}) { - $header{-status} = '404 Not Found'; - $a_template = "404.tt"; - } - print $cgi->header(%header); - if ($head) { $head = 0; continue; } - $template->process($a_template, \%data) or info("Template: " . $template->error()); - } else { - $header{-status} = '405 Method Not Allowed'; - print $cgi->header(%header); - } - } - } catch ($error) { - info("Crashed: $error"); - } - FCGI::CloseSocket($sock); +sub serve_template { + my ($file, @rest) = @_; + my %vars = @rest ? @rest : (); + + $template->process($file, \%vars) + or info("Template: " . $template->error()); } -GN::main(); +my %routes = ( + '/' => sub { serve_template("index.tt", @_) }, + '/~([a-zA-Z0-9_.]+)' => sub { serve_template("index_user.tt", @_) }, + '/~([a-zA-Z0-9_.]+)/([a-zA-Z0-9_.]+)' => sub { serve_template("repository.tt", @_) }, +); + +my $root = GIT_ROOT; +my $dbfile = DB_FILE; +my %data = ( + found => 0, +); + +sub master { + my $cgi = CGI->new; + my %header = ( + -Content_Type => 'text/html', + -charset => 'UTF-8', + ); + my $method = $ENV{'REQUEST_METHOD'} || ''; + my $uri = $ENV{'REQUEST_URI'} || '/'; + + for my $pattern (keys %routes) { + if ($uri =~ m{^$pattern$}) { + my $handler = $routes{$pattern}; + $handler->($uri, $1, $2, $3); + } + } + + serve_template("404.tt"); # XXX missing code +} + +master() if !caller; + +1; diff --git a/gorillanest.pl.fcgi b/gorillanest.pl.fcgi new file mode 100755 index 0000000..2692877 --- /dev/null +++ b/gorillanest.pl.fcgi @@ -0,0 +1,22 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +use Syntax::Keyword::Try; +use FCGI; + +use lib '.'; +# BEGIN { require 'config.pl'; } +BEGIN { require 'gorillanest.pl.cgi'; } + +try { + open STDERR, '>', LOG_FILE or die LOG_FILE . ": $!"; + my $sock = FCGI::OpenSocket(SOCKET_FILE, 100); + my $request = FCGI::Request(\*STDIN, \*STDOUT, \*STDERR, \%ENV, $sock); + while($request->Accept() >= 0) { + master(); + } +} catch ($error) { + info("Crashed: $error"); +} diff --git a/lighttpd-cgi.conf b/lighttpd-cgi.conf new file mode 100644 index 0000000..07b55d7 --- /dev/null +++ b/lighttpd-cgi.conf @@ -0,0 +1,21 @@ +server.modules = ( + "mod_cgi", + "mod_rewrite", + "mod_setenv" +) + +server.document-root = var.CWD +server.port = 5050 + +url.rewrite-once = ( + "/" => "/gorillanest.pl.cgi" +) + +setenv.add-environment = ( + "PERL5LIB" => env.PERL5LIB +) + + +cgi.assign = ( + ".pl.cgi" => "/usr/bin/perl" +) diff --git a/lighttpd.conf b/lighttpd-fcgi.conf index 3f21f8f..a9cbb25 100644 --- a/lighttpd.conf +++ b/lighttpd-fcgi.conf @@ -17,7 +17,7 @@ setenv.add-environment = ( fastcgi.server = ( "/gorillanest" => (( - "bin-path" => "gorillanest", + "bin-path" => "gorillanest.pl.fcgi", "socket" => "gorillanest.sock", )) ) |
