diff options
| -rw-r--r-- | Makefile | 4 | ||||
| -rw-r--r-- | gorillanest.pl.cgi | 105 | ||||
| -rwxr-xr-x | gorillanest.pl.fcgi | 19 | ||||
| -rw-r--r-- | lighttpd-cgi.conf | 21 | ||||
| -rw-r--r-- | lighttpd-fcgi.conf (renamed from lighttpd.conf) | 2 |
5 files changed, 148 insertions, 3 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 new file mode 100644 index 0000000..565fd6f --- /dev/null +++ b/gorillanest.pl.cgi @@ -0,0 +1,105 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +use CGI; +use Switch::Back; +use Template; +use URI::Escape; +use Cwd; + +use Data::Dumper; +sub info { + print STDERR join(' ', @_); +} + +our $template = Template->new({INCLUDE_PATH => 'template'}); + +# significant dirs only +sub GN::directories { + my $root = $_[0]; + opendir my $dir, $root or die "Cannot open directory '$root': $!"; + my @directories; + my %drop = ( + '.' => 0, + '..' => 0, + ); + foreach (readdir $dir) { + push(@directories, $_) if (-d join('/', $_[0], $_) && ($drop{$_} // 1)); + } + closedir $dir; + return \@directories; +} + +# probably should output all repos recursively, currently just outputs list of users +sub GN::index { # / + my ($root, $dataref) = @_; + my %data = %$dataref; + my @directories = map { my $i = $_; map { join('/', $i, $_) } @{GN::directories(join('/', $root, $i))} } @{GN::directories($root)}; + $data{directories} = \@directories; + if ($data{directories}) { $data{found} = 1; } + return \%data; +} + +sub GN::user { # /$username/ + my ($root, $dataref) = @_; + my %data = %$dataref; + my @directories = @{GN::directories(join('/', $root, $data{username}))}; + $data{directories} = \@directories; + if ($data{directories}) { $data{found} = 1; } + return \%data; +} + +sub GN::repository { # /$username/$repository + my ($root, $dataref) = @_; + my %data = %$dataref; + my $d = getcwd(); + chdir(join('/', $root, $data{username}, $data{repository})); + warn 'sneed ' . getcwd() . "\n"; + $data{log} = Dumper(split(/\n/, qx(git log --pretty=format:\'%H | %an | %ad | %s%x0a\'))); # It technically works # | tac | tr -s \'\n\' + chdir($d); + $data{found} = 1; + return \%data; +} + +sub serve_template { + my ($file, @rest) = @_; + my %vars = @rest ? @rest : (); + + $template->process($file, \%vars) + or info("Template: " . $template->error()); +} + +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 $public = 'git/public'; +my $dbfile = 'gorillanest.sqlite3'; +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..f4c0687 --- /dev/null +++ b/gorillanest.pl.fcgi @@ -0,0 +1,19 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +use Syntax::Keyword::Try; +use FCGI; + +require "gorillanest.pl.cgi"; + +our $request = FCGI::Request(); + +try { + 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", )) ) |
