Skip to content
Snippets Groups Projects
Verified Commit 1efa8685 authored by Andrej Ramašeuski's avatar Andrej Ramašeuski
Browse files

Verze 2.0. Simplifikace, jen chobotnice

parent cc6b66a8
No related branches found
No related tags found
No related merge requests found
Pipeline #11255 passed
......@@ -2,7 +2,7 @@ image: docker:20.10.9
variables:
DOCKER_TLS_CERTDIR: "/certs"
IMAGE_VER: 1.1.0
IMAGE_VER: 2.0.0
services:
- docker:20.10.9-dind
......
FROM alpine:latest
RUN apk update && apk add perl-mojolicious perl-io-socket-ssl
RUN apk update && apk add perl-mojolicious perl-io-socket-ssl perl-app-cpanminus make
RUN cpanm GraphQL::Client
ADD . /opt/piratar
WORKDIR /opt/piratar
USER nobody
EXPOSE 3000
CMD hypnotoad -f /opt/piratar/piratar
CMD /opt/piratar/piratar daemon -c 3000
......@@ -2,12 +2,7 @@
use Mojolicious::Lite -signatures;
use Mojo::UserAgent;
use Mojolicious::Static;
my $config = plugin 'Config';
my $ua = Mojo::UserAgent->new;
my $cache = $config->{cache};
mkdir $cache if ! -d $cache;
use GraphQL::Client;
get '/*url' => sub ($c) {
......@@ -15,57 +10,22 @@ get '/*url' => sub ($c) {
$username =~ s/^.*\///;
$username =~ s/\.(png|jpg|gif)$//;
if (my $url = check($c, $username)) {
$c->redirect_to($url);
}
else {
$c->reply->file('default.jpg');
}
};
app->start;
sub urls ( $c, $photos, $order ) {
my @default = split /\W+/, $c->config->{sources};
my @custom = ();
foreach my $p ( split '', $order // '' ) {
push @custom, $default[$p] if $p < scalar @default;
}
my %photos = map { $_->{source} => $_->{url} } @{ $photos };
return grep {defined} map { $photos{$_} } ( @custom, @default );
}
sub check ($c, $username ) {
my ( $order, $photos, $photo);
$order = $1 if $username =~ s/~(\d+)$//;
eval {
$photos = $ua->get(
sprintf($config->{iapi_get_photos}, $username)
)->result->json;
};
if ( ! $photos || ref $photos ne 'ARRAY' ) {
$c->render( status => 404, text => 'not found' );
my $gq = GraphQL::Client->new(url => $ENV{OCTOPUS});
my $rc = $gq->execute(qq[ query MyQuery {
allPeople(filters: {username: {iExact: "$username"}}) {
edges { node { profilePhoto } }
}}],
);
if ( ! $rc->{errors} && $rc->{data} ) {
my $user = $rc->{data}{allPeople}{edges}[0]{node};
if ( my $photo = $user->{profilePhoto}) {
$c->redirect_to($photo);
return;
}
foreach my $url ( urls($c, $photos, $order ) ) {
my $res;
eval { $res = $ua->head( $url )->result; };
$c->app->log->info("CHECK $url");
$c->app->log->error($@), next if $@;
$c->app->log->error($res->message), next if ! $res->is_success;
$photo = $url;
last;
}
return $photo;
$c->reply->file('default.jpg');
};
}
app->start;
{
iapi_get_photos => 'https://iapi.pirati.cz/v1/user/photos?username=%s',
sources => 'lide_0 lide_1 lide_2 web_main web_local gravatar_0 gravatar_1 gravatar_2',
cache => '/tmp/cache',
hypnotoad => {
listen => ['http://0.0.0.0:3000'],
workers => 10,
proxy => 1,
pid_file => '/tmp/piratar.pid',
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment