JSON.pm 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package Trog::Routes::JSON;
  2. use strict;
  3. use warnings;
  4. no warnings 'experimental';
  5. use feature qw{signatures};
  6. use JSON::MaybeXS();
  7. our %routes = (
  8. '/api/catalog' => {
  9. method => 'GET',
  10. callback => \&catalog,
  11. parameters => [],
  12. },
  13. '/api/webmanifest' => {
  14. method => 'GET',
  15. callback => \&webmanifest,
  16. parameters => [],
  17. },
  18. );
  19. my $contenttype = "Content-type:application/json;";
  20. sub catalog ($query, $input, $=) {
  21. my $enc = JSON::MaybeXS->new( utf8 => 1 );
  22. my %rcopy = %{\%routes};
  23. foreach my $r (keys(%rcopy)) {
  24. delete $rcopy{$r}{callback}
  25. }
  26. return [200,[$contenttype],[$enc->encode(\%rcopy)]];
  27. }
  28. sub webmanifest ($query, $input, $=) {
  29. my $enc = JSON::MaybeXS->new( utf8 => 1 );
  30. my %manifest = (
  31. "icons" => [
  32. { "src" => "$query->{theme_dir}/img/icon/favicon-192.png", "type" => "image/png", "sizes" => "192x192" },
  33. { "src" => "$query->{theme_dir}/img/icon/favicon-512.png", "type" => "image/png", "sizes" => "512x512" },
  34. ],
  35. );
  36. return [ 200, [$contenttype], [$enc->encode(\%manifest)] ];
  37. }
  38. 1;