1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
use strict;
use warnings;
use Carp;
use Plack::App::Path::Router::PSGI;
use Path::Router;
use Plack::Builder;
use Plack::Request;
use Plack::Response;
use lib '../lib';
use Captcha::reCAPTCHA::V3;
my $rc = Captcha::reCAPTCHA::V3->new(
secret => '__YOUR_SECRET__',
sitekey => '__YOUR_SITEKEY__',
);
use Text::Xslate::PP;
my $tx = Text::Xslate->new(
syntax => 'Kolon',
cache => 0,
verbose => 1,
);
my $router = Path::Router->new;
$router->add_route( '/' => target => \&root );
$router->add_route( '/verify' => target => \&verify );
# now create the Plack app
my $app = Plack::App::Path::Router::PSGI->new( router => $router );
return builder {
if ( !$app && ( my $error = $@ || $! ) ) { die $error; }
$app->to_app();
};
#===============================================================================
sub root {
my $env = shift;
my $body = $tx->render( './template.tx', {
script4head => $rc->script4head(),
input4form => $rc->input4form(),
} );
return _response( $env, $body );
}
sub verify {
my $env = shift;
my $req = Plack::Request->new($env);
my %param = %{ $req->body_parameters };
my $content = $rc->verify($param{'reCAPTCHA_Token'});
my $body = $tx->render( './template.tx', {
script4head => $rc->script4head(),
input4form => $rc->input4form(),
# param => \%param,
response => $content,
} );
return _response( $env, $body );
}
sub _response {
my $env = shift;
my $body = shift;
my $req = Plack::Request->new($env);
my $res = $req->new_response(200);
$res->content_length( length $body );
$res->content_type('text/html; charset=utf-8');
$res->body($body) unless $req->method eq 'HEAD';
$res->finalize;
}
|