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;
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;
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;
$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!