Skip to content

Instantly share code, notes, and snippets.

@powerman
Last active December 16, 2015 15:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save powerman/5456484 to your computer and use it in GitHub Desktop.
Save powerman/5456484 to your computer and use it in GitHub Desktop.
Support for non-blocking Mojolicious app in CGI mode
use AnyEvent;
sub allow_non_blocking_cgi {
my ($module) = @_;
eval "require $module;" or die $@; ## no critic (ProhibitStringyEval)
my $startup = \&{$module.'::startup'};
my $wrapper = sub {
my ($app) = @_;
&{$startup};
my $done = AnyEvent->condvar;
$app->hook(after_dispatch => sub {
$done->send;
});
$app->hook(around_dispatch => sub {
shift->();
$done->recv;
});
};
no strict 'refs';
no warnings 'redefine'; ## no critic (ProhibitNoWarnings)
*{$module.'::startup'} = $wrapper;
return;
}
require Mojolicious::Commands;
if (Mojolicious::Commands->detect(q{}) eq 'cgi') {
allow_non_blocking_cgi('WebSite');
}
Mojolicious::Commands->start_app('WebSite');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment