-
Notifications
You must be signed in to change notification settings - Fork 4
/
SCO.pm
82 lines (65 loc) · 1.54 KB
/
SCO.pm
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
package MetaCPAN::SCO;
use strict;
use warnings;
use Carp ();
use Cwd qw(abs_path);
use File::Basename qw(dirname);
use JSON qw(from_json);
use Path::Tiny qw(path);
use Plack::Builder;
use Plack::Request;
use Template;
our $VERSION = '0.01';
=head1 NAME
SCO - search.cpan.org clone
=cut
sub run {
my $root = root();
my $app = sub {
my $env = shift;
my $request = Plack::Request->new($env);
if ($request->path_info eq '/') {
return template('index', {front => 1});
}
if ($request->path_info eq '/feedback') {
return template('feedback');
}
if ($request->path_info =~ m{^/author/?$}) {
return template('author');
}
my $reply = template('404');
return [ '404', [ 'Content-Type' => 'text/html' ], $reply->[2], ];
};
builder {
enable 'Plack::Middleware::Static',
path => qr{^/(favicon.ico|robots.txt)},
root => "$root/static/";
$app;
};
}
sub template {
my ( $file, $vars ) = @_;
$vars //= {};
Carp::confess 'Need to pass HASH-ref to template()'
if ref $vars ne 'HASH';
my $root = root();
$vars->{totals} = from_json path("$root/totals.json")->slurp_utf8;
my $tt = Template->new(
INCLUDE_PATH => "$root/tt",
INTERPOLATE => 0,
POST_CHOMP => 1,
EVAL_PERL => 1,
START_TAG => '<%',
END_TAG => '%>',
PRE_PROCESS => 'incl/header.tt',
POST_PROCESS => 'incl/footer.tt',
);
my $out;
$tt->process( "$file.tt", $vars, \$out )
|| die $tt->error();
return [ '200', [ 'Content-Type' => 'text/html' ], [$out], ];
}
sub root {
return dirname(dirname(dirname( abs_path(__FILE__) )));
}
1;