diff options
author | Yorhel <git@yorhel.nl> | 2019-12-30 09:49:13 +0100 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2019-12-30 09:49:13 +0100 |
commit | d79c982ee876502db2e1a12752667c6a198c2ddc (patch) | |
tree | ccd4a4e72e5b0d3dd412bed6aee58044e93893f0 /lib/VN3/Prelude.pm | |
parent | e146ed7bda12d369532e485ca0c2e3d823811854 (diff) |
Actually, let's get rid of v3 now that it doesn't work anymore anyway
Diffstat (limited to 'lib/VN3/Prelude.pm')
-rw-r--r-- | lib/VN3/Prelude.pm | 104 |
1 files changed, 0 insertions, 104 deletions
diff --git a/lib/VN3/Prelude.pm b/lib/VN3/Prelude.pm deleted file mode 100644 index a10a66ac..00000000 --- a/lib/VN3/Prelude.pm +++ /dev/null @@ -1,104 +0,0 @@ -# Importing this module is equivalent to: -# -# use strict; -# use warnings; -# use v5.10; -# use utf8; -# -# use TUWF ':Html5', 'mkclass'; -# use Exporter 'import'; -# use Time::HiRes 'time'; -# -# use VNDBUtil; -# use VNDB::Types; -# use VNWeb::Auth; -# use VN3::HTML; -# use VN3::DB; -# use VN3::Types; -# use VN3::Validation; -# use VN3::BBCode; -# use VN3::ElmGen; -# -# WARNING: This should not be used from the above modules. -# -# This module also exports a few utility functions for writing URI handlers. -package VN3::Prelude; - -use strict; -use warnings; -use utf8; -use feature ':5.10'; -use TUWF; -use VNWeb::Auth; -use VN3::ElmGen; - -sub import { - my $c = caller; - - strict->import; - warnings->import; - feature->import(':5.10'); - utf8->import; - - die $@ if !eval <<" EOM;"; - package $c; - - use TUWF ':Html5', 'mkclass'; - use Exporter 'import'; - use Time::HiRes 'time'; - - use VNDBUtil; - use VNDB::Types; - use VNWeb::Auth; - use VN3::HTML; - use VN3::DB; - use VN3::Types; - use VN3::Validation; - use VN3::BBCode; - use VN3::ElmGen; - 1; - EOM; - - no strict 'refs'; - *{$c.'::json_api'} = \&json_api; -} - - - -# Easy wrapper to create a simple API that accepts JSON data on POST requests. -# The CSRF token and the input data are validated before the subroutine is -# called. -# -# Usage: -# -# json_api '/some/url', { -# username => { maxlength => 10 }, -# }, sub { -# my $validated_data = shift; -# }; -my $elm_Invalid = elm_api 'Invalid', {}; -sub json_api { - my($path, $keys, $sub) = @_; - - my $schema = ref $keys eq 'HASH' ? tuwf->compile({ type => 'hash', keys => $keys }) : $keys; - - TUWF::post $path => sub { - if(!auth->csrfcheck(tuwf->reqHeader('X-CSRF-Token')||'')) { - warn "Invalid CSRF token in request\n"; - $elm_CSRF->(); - return; - } - - my $data = tuwf->validate(json => $schema); - if(!$data) { - warn "JSON validation failed\ninput: " . JSON::XS->new->allow_nonref->pretty->canonical->encode(tuwf->reqJSON) . "\nerror: " . JSON::XS->new->encode($data->err) . "\n"; - $elm_Invalid->($data->err); - return; - } - - $sub->($data->data); - warn "Non-JSON response to a json_api request, is this intended?\n" if tuwf->resHeader('Content-Type') !~ /^application\/json/; - }; -} - -1; |