Created
May 24, 2016 00:49
-
-
Save brianmed/6a905072d2d1354c9f1ff0bfb1d382ea to your computer and use it in GitHub Desktop.
after_dispatch example hook that uses to_string in Mojolicious
This file contains 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; | |
use POSIX; | |
use Mojo::Util qw(spurt); | |
app->hook(after_dispatch => sub { | |
my ($c) = @_; | |
my ($sec, $micro) = Time::HiRes::gettimeofday(); | |
my $iso8601 = POSIX::strftime("%FT%T", localtime($sec)); | |
$micro = sprintf("%06d", $micro); | |
my $time = join(".", ($sec, $micro)); | |
if ($c->tx->res->to_string =~ m/^(.*\r\n\r\n)/s) { | |
spurt($1, "/tmp/joy-$time-$iso8601"); | |
} | |
return 1; | |
}); | |
get '/' => sub { | |
my $c = shift; | |
$c->render(template => 'index'); | |
}; | |
app->start; | |
__DATA__ | |
@@ index.html.ep | |
% layout 'default'; | |
% title 'Welcome'; | |
<h1>Welcome to the Mojolicious real-time web framework!</h1> | |
To learn more, you can browse through the documentation | |
<%= link_to 'here' => '/perldoc' %>. | |
@@ layouts/default.html.ep | |
<!DOCTYPE html> | |
<html> | |
<head><title><%= title %></title></head> | |
<body><%= content %></body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment