diff options
| author | Emil Williams | 2025-08-20 23:46:09 +0000 |
|---|---|---|
| committer | Emil Williams | 2025-08-20 23:46:09 +0000 |
| commit | df2e0e9da234428306fb933f84e21078dc75020b (patch) | |
| tree | 0500a46d9ddc1ea080919b7bcbc8279c11aa440f /gorillanest.pl.cgi | |
| parent | e49369309a6893e968f01c7cf38e27ea9219a4d9 (diff) | |
| parent | efb00d45c75762bac1b89f07128d00909caebd8d (diff) | |
| download | gorillanest-df2e0e9da234428306fb933f84e21078dc75020b.tar.xz gorillanest-df2e0e9da234428306fb933f84e21078dc75020b.tar.zst | |
I Doubt It Works The Same
Diffstat (limited to 'gorillanest.pl.cgi')
| -rwxr-xr-x | gorillanest.pl.cgi | 100 |
1 files changed, 42 insertions, 58 deletions
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; |
