-
Notifications
You must be signed in to change notification settings - Fork 1
/
cpan-meta-browser.pl
executable file
·176 lines (152 loc) · 6.75 KB
/
cpan-meta-browser.pl
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env perl
# This software is Copyright (c) 2017 by Dan Book <[email protected]>.
# This is free software, licensed under:
# The Artistic License 2.0 (GPL Compatible)
use 5.020;
use Mojo::JSON::MaybeXS;
use Mojolicious::Lite -signatures;
use Mojo::URL;
use Mojo::Util 'trim';
use CPAN::DistnameInfo;
use HTTP::Tiny;
use lib::relative 'lib';
push @{app->commands->namespaces}, 'CPANMetaBrowser::Command';
push @{app->plugins->namespaces}, 'CPANMetaBrowser::Plugin';
plugin 'Config' => {file => 'cpan-meta-browser.conf', default => {}};
if (defined(my $logfile = app->config->{logfile})) {
app->log->with_roles('+Clearable')->path($logfile);
}
my $httptiny;
helper 'httptiny' => sub { $httptiny //= HTTP::Tiny->new };
my $cache_dir = app->home->child('cache')->make_path;
helper 'cache_dir' => sub { $cache_dir };
my $backend = $ENV{CPAN_META_BROWSER_BACKEND} // app->config->{backend} // 'sqlite';
if ($backend eq 'sqlite') {
plugin 'Backend::SQLite';
} elsif ($backend eq 'pg') {
plugin 'Backend::Pg';
} elsif ($backend eq 'redis') {
plugin 'Backend::Redis';
} else {
die "Unknown backend '$backend' (should be 'sqlite', 'pg', or 'redis')\n";
}
helper backend => sub { $backend };
helper _keys_equal => sub ($c, $first, $second, $keys) {
return 0 unless defined $first and defined $second;
foreach my $key (@$keys) {
next if !defined $first->{$key} and !defined $second->{$key};
return 0 unless defined $first->{$key} and defined $second->{$key} and $first->{$key} eq $second->{$key};
}
return 1;
};
helper _sql_pattern_escape => sub ($c, $value) { $value =~ s/([%_\\])/\\$1/gr };
my $access_log = app->config->{access_log} // 'log/access.log';
my $old_level = app->log->level;
app->log->level('error'); # hack around AccessLog's dumb warning
plugin 'AccessLog' => {log => $access_log} if $access_log;
app->log->level($old_level);
helper results_module_url => sub ($c, $module) { Mojo::URL->new('https://metacpan.org/')->path("pod/$module")->to_string };
helper results_author_url => sub ($c, $author) { Mojo::URL->new('https://metacpan.org/')->path("author/$author")->to_string };
helper results_author_homepage => sub ($c, $url) { $url =~ m!^(?:[a-z]+:)?//!i ? $url : "http://$url" };
helper results_cpandir_url => sub ($c, $path) { Mojo::URL->new('https://www.cpan.org/')->path("authors/id/$path")->to_string };
helper results_release_name => sub ($c, $path) { CPAN::DistnameInfo->new($path)->distvname };
helper results_release_url => sub ($c, $uploader, $path) {
my $name = $c->results_release_name($path);
my $url = Mojo::URL->new('https://metacpan.org/');
if (defined $name) {
$url->path("release/$uploader/$name");
} else {
$url->path("author/$uploader/releases");
}
return $url->to_string;
};
my %perms_map = (
m => 'modulelist',
f => 'first-come',
a => 'admin',
c => 'co-maint',
p => 'permission manager',
);
helper results_permission_string => sub ($c, $perm) { $perms_map{$perm // ''} // '' };
helper results_date_string => sub ($c, $epoch) { gmtime($epoch) . ' UTC' };
helper results_author_cpandir => sub ($c, $author) { $author =~ m/((.).)/; "$2/$1/$author" };
get '/' => sub ($c) { $c->res->code(301); $c->redirect_to('packages') } => 'index';
get '/packages' => sub ($c) {
my $module = trim($c->param('module') // '');
return $c->render unless length $module;
my $match_mode = $c->param('match_mode') || 'prefix';
my $packages = $c->get_packages($module, $match_mode eq 'prefix', $match_mode eq 'infix');
my $last_updated = $c->get_refreshed('packages');
$c->render(packages => $packages, last_updated => $last_updated);
};
get '/perms' => sub ($c) {
my $author = trim($c->param('author') // '');
my $module = trim($c->param('module') // '');
return $c->render unless length $author or length $module;
my $match_mode = $c->param('match_mode') || 'prefix';
my $other_authors = $c->param('other_authors');
my $perms = $c->get_perms($author, $module, $match_mode eq 'prefix', $match_mode eq 'infix', $other_authors);
my $last_updated = $c->get_refreshed('perms');
$c->render(perms => $perms, last_updated => $last_updated);
};
get '/module-perms' => sub ($c) { $c->res->code(301); $c->redirect_to('perms') };
get '/author-perms' => sub ($c) { $c->res->code(301); $c->redirect_to('perms') };
get '/authors' => sub ($c) {
my $author = trim($c->param('author') // '');
return $c->render unless length $author;
my $match_mode = $c->param('match_mode') || 'prefix';
my $authors = $c->get_authors($author, $match_mode eq 'prefix', $match_mode eq 'infix');
my $last_updated = $c->get_refreshed('authors');
$c->render(authors => $authors, last_updated => $last_updated);
};
get '/api/v1/packages/:module' => sub ($c) {
my $module = trim($c->param('module') // '');
my $as_prefix = $c->param('as_prefix');
my $packages = $c->get_packages($module, $as_prefix);
$c->render(json => $packages);
};
get '/api/v1/perms/by-module/:module' => sub ($c) {
my $module = trim($c->param('module') // '');
my $as_prefix = $c->param('as_prefix');
my $perms = $c->get_perms('', $module, $as_prefix);
$c->render(json => $perms);
};
get '/api/v1/perms/by-author/:author' => sub ($c) {
my $author = trim($c->param('author') // '');
my $perms = $c->get_perms($author);
$c->render(json => $perms);
};
get '/api/v1/authors/:author' => sub ($c) {
my $author = trim($c->param('author') // '');
my $as_prefix = $c->param('as_prefix');
my $authors = $c->get_authors($author, $as_prefix);
$c->render(json => $authors);
};
get '/api/v2/packages/:module' => {module => ''} => sub ($c) {
my @modules = grep { length } map { trim $_ } $c->param('module'), @{$c->req->every_param('module')};
my $module = @modules == 1 ? $modules[0] : \@modules;
my $as_prefix = $c->param('as_prefix');
my $as_infix = $c->param('as_infix');
my $packages = $c->get_packages($module, $as_prefix, $as_infix);
my $last_updated = $c->get_refreshed('packages');
$c->render(json => {data => $packages, last_updated => $last_updated});
};
get '/api/v2/perms' => sub ($c) {
my $author = trim($c->param('author') // '');
my $module = trim($c->param('module') // '');
my $as_prefix = $c->param('as_prefix');
my $as_infix = $c->param('as_infix');
my $other_authors = $c->param('other_authors');
my $perms = $c->get_perms($author, $module, $as_prefix, $as_infix, $other_authors);
my $last_updated = $c->get_refreshed('perms');
$c->render(json => {data => $perms, last_updated => $last_updated});
};
get '/api/v2/authors/:author' => sub ($c) {
my $author = trim($c->param('author') // '');
my $as_prefix = $c->param('as_prefix');
my $as_infix = $c->param('as_infix');
my $authors = $c->get_authors($author, $as_prefix, $as_infix);
my $last_updated = $c->get_refreshed('authors');
$c->render(json => {data => $authors, last_updated => $last_updated});
};
app->start;