get '/titles' => sub ($c) {
$c->render_later;
my $tx = $c->tx;
my @promises = map { $ua->get_p($_) } @urls;
Mojo::Promise->all(@promises)->then(sub {
my @results = @_;
my $titles = [];
foreach my $result (@results) {
my $tx = $result->[0];
my $title = trim($tx->res->dom->at('title')->text);
my $status = $tx->res->is_success ? 'Connected' : $tx->error->{message};
push @$titles, {host => $tx->req->url->host, status => $status, title => $title};
}
$c->render(titles => $titles);
})->catch(sub { $c->reply->exception(pop); undef $tx });
} => 'titles';
####
#!/usr/bin/perl
use Mojolicious::Lite;
my @urls = (
'https://mojolicious.org',
'https://metacpan.org',
'https://perlmonks.org',
'http://www.google.com',
);
my $ua = Mojo::UserAgent->new;
my $async = Mojo::IOLoop::Delay->new;
$async->steps(
sub {
my $self = shift;
$ua->get($_, $self->begin) for @urls;
},
sub {
my ($self, @tx) = @_;
foreach (@tx) {
my $title = '';
my $status = $_->res->is_success ? 'Connected' : $_->error->{message};
if ( $_->res->is_success ) {
$title = $_->res->dom->at('title')->text;
$title =~ s/^\s+|\s+$//g; # couldn't get Mojo::Util trim function to work
}
printf("Host: %-20s Status: %-15s Title: %s\n", $_->req->url->host, $status, $title);
}
}
);
$async->wait unless $async->ioloop->is_running;
####
$ ./titles_cli.pl
Host: mojolicious.org Status: Connected Title: Mojolicious - Perl real-time web framework
Host: metacpan.org Status: Connected Title: Search the CPAN - metacpan.org
Host: perlmonks.org Status: Connected Title: PerlMonks - The Monastery Gates
Host: www.google.com Status: Connected Title: Google
####
#!/usr/bin/perl
use Mojolicious::Lite;
my @urls = (
'https://mojolicious.org',
'https://metacpan.org',
'https://perlmonks.org',
'http://www.google.com',
);
get '/titles' => sub {
my $ua = Mojo::UserAgent->new;
my $async = Mojo::IOLoop::Delay->new;
$async->steps(
sub {
my $self = shift;
$ua->get($_, $self->begin) for @urls;
},
sub {
my ($self, @tx) = @_;
my $titles = [];
foreach (@tx) {
my $title = '';
my $status = $_->res->is_success ? 'Connected' : $_->error->{message};
if ( $_->res->is_success ) {
$title = $_->res->dom->at('title')->text;
$title =~ s/^\s+|\s+$//g; # couldn't get Mojo::Util trim function to work
}
push @$titles, {host => $_->req->url->host, status => $status, title => $title};
}
$self->stash(titles => $titles);
}
);
} => 'titles';
app->start;
__DATA__
@@ titles.html.ep
% layout 'titles';
%= dumper stash('titles')
| Host |
Status |
Title |
% for my $server ( stash('titles') ) {
| <%= $server->{host} %> |
<%= $server->{status} %> |
<%= $server->{title} =%> |
% }
@@ layouts/titles.html.ep
Titles
Titles
<%= content =%>
####
$ ./titles_web.pl get /titles
[Thu Feb 7 08:58:06 2019] [debug] GET "/titles" (842f1ec4)
[Thu Feb 7 08:58:06 2019] [debug] Routing to a callback
[Thu Feb 7 08:58:06 2019] [debug] Rendering template "titles.html.ep" from DATA section
[Thu Feb 7 08:58:06 2019] [debug] Rendering template "layouts/titles.html.ep" from DATA section
[Thu Feb 7 08:58:06 2019] [debug] 200 OK (0.002744s, 364.431/s)
Titles
Titles
undef