/
API.pm
189 lines (151 loc) · 4.92 KB
/
API.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package MetaCPAN::API;
=head1 DESCRIPTION
This is not a web app. It's purely here to manage the API's release indexing
queue.
# On vagrant VM
./bin/run morbo bin/queue.pl
# Display information on jobs in queue
./bin/run bin/queue.pl minion job
To run the minion admin web interface, run the following on one of the servers:
# Run the daemon on a local port (tunnel to display on your browser)
./bin/run bin/queue.pl daemon
=cut
use Mojo::Base 'Mojolicious';
use Config::ZOMG ();
use File::Temp ();
use List::Util qw( any );
use MetaCPAN::Model::Search ();
use MetaCPAN::Model::User ();
use MetaCPAN::Script::Runner ();
use Search::Elasticsearch ();
use Try::Tiny qw( catch try );
has es => sub {
return Search::Elasticsearch->new(
client => '2_0::Direct',
nodes => [':9200'], #TODO config
);
};
has model_search => sub {
my $self = shift;
return MetaCPAN::Model::Search->new(
es => $self->es,
index => 'cpan',
);
};
has model_user => sub {
my $self = shift;
return MetaCPAN::Model::User->new( es => $self->es, );
};
sub startup {
my $self = shift;
unless ( $self->config->{config_override} ) {
$self->config(
Config::ZOMG->new(
name => 'metacpan_server',
path => $self->home->to_string,
)->load
);
}
die 'need secret' unless $self->config->{secret};
$self->secrets( [ $self->config->{secret} ] );
$self->static->paths( [ $self->home->child('root') ] );
if ( $ENV{HARNESS_ACTIVE} ) {
my $file = File::Temp->new( UNLINK => 1, SUFFIX => '.db' );
$self->plugin( Minion => { SQLite => 'sqlite:' . $file } );
}
else {
$self->plugin( Minion => { Pg => $self->config->{minion_dsn} } );
}
$self->minion->add_task(
index_release => $self->_gen_index_task_sub('release') );
$self->minion->add_task(
index_latest => $self->_gen_index_task_sub('latest') );
$self->minion->add_task(
index_favorite => $self->_gen_index_task_sub('favorite') );
$self->_set_up_routes;
}
sub _gen_index_task_sub {
my ( $self, $type ) = @_;
return sub {
my ( $job, @args ) = @_;
my @warnings;
local $SIG{__WARN__} = sub {
push @warnings, $_[0];
warn $_[0];
};
# @args could be ( '--latest', '/path/to/release' );
unshift @args, $type;
# Runner expects to have been called via CLI
local @ARGV = @args;
try {
MetaCPAN::Script::Runner->run(@args);
$job->finish( @warnings ? { warnings => \@warnings } : () );
}
catch {
warn $_;
$job->fail(
{
message => $_,
@warnings ? ( warnings => \@warnings ) : (),
}
);
};
}
}
sub _set_up_routes {
my $self = shift;
my $r = $self->routes;
$self->plugin(
'Web::Auth',
module => 'Github',
key => $self->config->{github_key},
secret => $self->config->{github_secret},
on_finished => sub {
my ( $c, $access_token, $account_info ) = @_;
my $login = $account_info->{login};
if ( $self->_is_admin($login) ) {
$c->session( username => $login );
$c->redirect_to('/admin');
return;
}
return $c->render(
text => "$login is not authorized to access this application",
status => 403
);
},
);
my $admin = $r->under(
'/admin' => sub {
my $c = shift;
return 1 if $self->_is_admin( $c->session('username') );
$c->redirect_to('/auth/github/authenticate');
return 0;
}
);
$admin->get('home')->to('admin#home')->name('admin-home');
$admin->post('enqueue')->to('queue#enqueue')->name('enqueue');
$admin->post('search-identities')->to('admin#search_identities')
->name('search-identities');
$admin->get('index-release')->to('queue#index_release')
->name('index-release');
$admin->get('identity-search-form')->to('admin#identity_search_form')
->name('identity_search_form');
$self->plugin( 'Minion::Admin' => { route => $admin->any('/minion') } );
$self->plugin(
'OpenAPI' => { url => $self->home->rel_file('root/static/v1.yml') } );
$self->plugin(
MountPSGI => { '/' => $self->home->child('app.psgi')->to_string } );
}
sub _is_admin {
my $self = shift;
my $username
= shift || ( $ENV{HARNESS_ACTIVE} ? $ENV{FORCE_ADMIN_AUTH} : () );
return 0 unless $username;
my @admins = (
'haarg', 'jberger', 'mickeyn', 'oalders',
'ranguard', 'reyjrar', 'ssoriche',
$ENV{HARNESS_ACTIVE} ? 'tester' : (),
);
return any { $username eq $_ } @admins;
}
1;