This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use Mojolicious::Lite; | |
# Global logic shared by all routes | |
under sub { | |
my $self = shift; | |
return 1 if $self->req->headers->header('X-Bender'); | |
$self->render(text => "You're not Bender!"); | |
return; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Mojo::Base -strict; | |
use Mojo::UserAgent; | |
my $ua = Mojo::UserAgent->new; | |
# Random UserAgent header | |
$ua->on(start => sub { | |
my ($ua, $tx) = @_; | |
$tx->req->headers->user_agent(join '', | |
map { ('a' .. 'z')[rand 26] } 1 .. 10); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Mojolicious::Lite; | |
use Time::HiRes 'time'; | |
# How long did receiving the request take? | |
hook after_build_tx => sub { | |
my $tx = shift; | |
my $before = time; | |
$tx->req->on(finish => sub { | |
my $req = shift; | |
$req->headers->header('X-Time' => time - $before); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use Mojo::Base -strict; | |
use Mojo::UserAgent; | |
# Lets start by fetching the current top Perl stories from Reddit | |
my $reddit = | |
Mojo::UserAgent->new->get('www.reddit.com/r/perl')->res->dom; | |
# And extract all titles from the HTML | |
my $titles = |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Mojolicious::Lite; | |
use Coro; | |
use Coro::EV; | |
use Coro::Timer; | |
my $old = app->on_process; | |
app->on_process( | |
sub { | |
my ($self, $c) = @_; | |
async { $self->$old($c) }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Mojolicious::Lite; | |
plugin 'PODRenderer'; | |
get '/' => sub { | |
my $self = shift; | |
$self->render('welcome', name => 'Somebody'); | |
}; | |
app->start; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use Mojolicious::Lite; | |
get '/' => 'index'; | |
get '/events' => sub { | |
my $self = shift; | |
# Subscribe to "message" event and emit "log" event | |
$self->res->headers->content_type('text/event-stream'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use Mojolicious::Lite; | |
# Documentation under "/perldoc" | |
plugin 'PODRenderer'; | |
# Template with browser-side code | |
get '/' => 'index'; | |
# WebSocket log service |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Mojo::Base -strict; | |
use Mojo::UserAgent; | |
use Mojo::Util 'sha1_sum'; | |
my $ua = Mojo::UserAgent->new; | |
# Decorate every transaction in "start" event | |
$ua->on(start => sub { | |
my ($ua, $tx) = @_; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Mojolicious::Lite; | |
get '/delay' => sub { | |
my $self = shift; | |
Mojo::IOLoop->timer(3 => sub { | |
$self->render_text('Delayed for 3 seconds.'); | |
}); | |
}; | |
get '/nodelay' => sub { |