aboutsummaryrefslogtreecommitdiff
path: root/gorillanest.pl.cgi
blob: a807fc49e2c5664d6abeb1f3ecd0dcd268ef1229 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env perl

# XXX
# why are we passing around root like a cheap whore?
# looking into it, i think we should have a global config object using
# https://metacpan.org/pod/Readonly
#
# i modified the routing heavily, this is how people do it;
# pretty clean
# you must also realize that not all routes are necessarily templates,
# it could be a redirect for example, so the original solution would
# complicate beyond comprehension

use strict;
use warnings;

use CGI;
use Template;
use URI::Escape;
use Cwd;
use Data::Dumper;
use Git::Repository;

use lib '.';
BEGIN { require 'config.pl'; }

sub info {
    warn join(' ', @_);
}

our $template = Template->new({INCLUDE_PATH => 'template'});

sub serve_template {
    my ($template_name, $data) = @_;

    $template->process($template_name, $data)
        or info("Template: " . $template->error());
}

# 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) = @_;
    my %data;

    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; }

    serve_template("index.tt", \%data);
}

sub GN::user { # /$username/
    my ($root, $username) = @_;

    my %data;
    my @directories = @{GN::directories(join('/', $root, $data{username}))};
    $data{directories} = \@directories;
    if ($data{directories}) { $data{found} = 1; }

    serve_template("index_user.tt", \%data);
}

sub GN::repository { # /$username/$repository
    my ($root, $username, $repository) = @_;

    die 'not implemented';
}

my $public = 'repos/';
my $dbfile = 'gorillanest.sqlite3';
my %data = (
    found => 0,
);

my %routes = (
    '/'                   => sub { GN::index($public); },
    '/~([\w.]+)'          => sub { GN::user($public, @_) },
    '/~([\w.]+)/([\w.]+)' => sub { GN::repository($public, @_) },
);
my %route_regex_cache = map { $_ => qr{^$_$} } keys %routes;

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 =~ $route_regex_cache{$pattern}) {
			my $handler = $routes{$pattern};
			$handler->($uri, $1, $2, $3);
            return;
		}
	}

	serve_template("404.tt", {}); # XXX missing code
}

master() if !caller; 1;