Skip to content

Instantly share code, notes, and snippets.

@brianmed
Created May 24, 2016 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianmed/6a905072d2d1354c9f1ff0bfb1d382ea to your computer and use it in GitHub Desktop.
Save brianmed/6a905072d2d1354c9f1ff0bfb1d382ea to your computer and use it in GitHub Desktop.
after_dispatch example hook that uses to_string in Mojolicious
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