Last active
December 16, 2015 15:29
-
-
Save powerman/5456484 to your computer and use it in GitHub Desktop.
Support for non-blocking Mojolicious app in CGI mode
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 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