https://metacpan.org/release/MOTEMEN/AnyEvent-Plackup-0.01
(Japanese entry at: http://subtech.g.hatena.ne.jp/motemen/20120920/1348138250)
Simple obvious usage
use AnyEvent::Plackup; my $server = plackup(app => \&psgi_app, port => $port); say $server; # stringifies to server origin, e.g. "http://0.0.0.0:$port"
You may omit port number and random open one is chosen.
In this example, this is only a wrapper of Twiggy. But what makes this module useful is that you can omit even the app
parameter.
Non-app usage
my $server = plackup();
Invoking plackup
without app
specified, you can serve responses without a PSGI-coderef. To handle requests:
my $req = $server->recv; $req->respond([ 200, [], [ 'OK' ] ]);
Calling $server->recv
makes the program wait for a request coming (AnyEvent loop starts). The returned request object is a Plack::Request
equipped with one extra method, respond
. Giving a PSGI response (or a coderef) to this method, you can serve response to the client.
Highly useful, for example, if you are writing a simple script that requires OAuth and you want to establish an HTTP server just only to receive an auth token.
For an example, see eg/google-oauth.pl.
use strict; use warnings; use autodie ':all'; use Net::OAuth2::Profile::WebServer; use JSON::XS qw(decode_json); use Browser::Open qw(open_browser); use AnyEvent::Plackup; # Obtain one at Google Cloud Console <https://cloud.google.com/console>. my $client_secret = do { open my $fh, '<', 'client_secret.json'; local $/; decode_json scalar <$fh>; }; my $server = plackup(port => 4000); my $auth = Net::OAuth2::Profile::WebServer->new( client_id => $client_secret->{web}->{client_id}, client_secret => $client_secret->{web}->{client_secret}, authorize_url => $client_secret->{web}->{auth_uri}, access_token_url => $client_secret->{web}->{token_uri}, scope => 'openid profile', redirect_uri => "http://localhost:$server->{port}/", ); open_browser $auth->authorize; my $req = $server->recv; # Program stops here until user comes to http://localhost:4000/ $req->respond([ 200, [], [ 'Thank you! Now go back to the console.' ] ]); my $access_token = $auth->get_access_token($req->parameters->{code}); my $res = $access_token->get('https://www.googleapis.com/oauth2/v1/userinfo'); my $user_info = decode_json $res->decoded_content; print "Hello, $user_info->{name}.\n";
Once you send the user to the browser, then he/she is redirected to your temporary server, and you will get the auth. The user is not required to enter PIN by his/her hand.
Thank you very much to @tokuhirom and @schmorp, for giving me advice for this module!