Skip to content

Instantly share code, notes, and snippets.

@n7st
Last active April 20, 2018 14:05
Show Gist options
  • Save n7st/3c99cfe2dd4153a58fb725f6f7832cab to your computer and use it in GitHub Desktop.
Save n7st/3c99cfe2dd4153a58fb725f6f7832cab to your computer and use it in GitHub Desktop.
Asynchronous Mojolicious web application which sleeps for n seconds (GET / ?delay=5 sleeps for 5 seconds)
#!/usr/bin/env perl
use Mojolicious::Lite;
use Mojo::IOLoop;
get '/' => sub {
my $c = shift;
my $seconds = $c->param('delay');
if (!($seconds >= 0 && $seconds =~ /^\d+$/)) {
return $c->render_json(json => {
message => "${seconds} is not a positive integer",
});
}
# Non-blocking timer which waits for n seconds before responding
Mojo::IOLoop->timer($seconds => sub {
return $c->render(json => {
message => "Slept for ${seconds} seconds",
});
});
};
app->start;
__END__
=head1 NAME
SlowAPI
=head1 DESCRIPTION
Simulates a slow API response you might have to wait a while for.
=head1 USAGE
morbo SlowAPI
Navigate to the base address printed by Morbo and add a delay parameter, e.g.
http://locahost:3000?delay=5 (which will sleep for 5 seconds).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment