| 1 | 1 | ▶ | use v5.40; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 1 | ▶ | use experimental 'signatures'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use Future::AsyncAwait; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use FindBin; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use PAGI::Nano; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | # Ports PAGI-Tools' 13-contact-form to PAGI::Nano. | ||||||||||
| 8 | # Form parsing, whitelisting, and a custom 400 on missing fields via | ||||||||||
| 9 | # $c->params->required; an optional multipart file upload read through | ||||||||||
| 10 | # $c->req->upload; and the form page served statically. | ||||||||||
| 11 | # | ||||||||||
| 12 | # pagi-server app.pl | ||||||||||
| 13 | # curl -X POST -F email=a@b.com -F message=hi -F attachment=@README \ | ||||||||||
| 14 | # http://127.0.0.1:5000/submit | ||||||||||
| 15 | |||||||||||
| 16 | 1 | ▶ | my $app = app { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 1 | static '/form' => "$FindBin::Bin/public/"; | |||||||||
| 18 | |||||||||||
| 19 | 3 | ▶ | post '/submit' => async sub ($c) { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 20 | # Strong parameters: whitelist + a chosen 400 if a required field is absent. | ||||||||||
| 21 | my $attrs = await $c->params->required( | ||||||||||
| 22 | 'email', 'message', | ||||||||||
| 23 | 1 | ▶ | sub ($c, $missing) { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 1 | $c->json({ error => 'missing fields', fields => $missing }, status => 400); | |||||||||
| 25 | }, | ||||||||||
| 26 | 3 | ); | |||||||||
| 27 | |||||||||||
| 28 | # Optional file upload (multipart). $req->upload is async; undef if absent. | ||||||||||
| 29 | 2 | my $upload = await $c->req->upload('attachment'); | |||||||||
| 30 | 2 | ▶ | my $file = $upload ? { filename => $upload->filename, size => $upload->size } : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | |||||||||||
| 32 | 2 | $c->json({ received => $attrs, attachment => $file }, status => 201); | |||||||||
| 33 | 1 | }; | |||||||||
| 34 | 1 | }; | |||||||||
| 35 | |||||||||||
| 36 | 1 | $app; | |||||||||