JSON.pm 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. use Trog::Config();
  8. my $conf = Trog::Config::get();
  9. # TODO de-duplicate this, it's shared in html
  10. my $theme_dir = '';
  11. $theme_dir = "themes/".$conf->param('general.theme') if $conf->param('general.theme') && -d "www/themes/".$conf->param('general.theme');
  12. our %routes = (
  13. '/api/catalog' => {
  14. method => 'GET',
  15. callback => \&catalog,
  16. parameters => [],
  17. },
  18. '/api/webmanifest' => {
  19. method => 'GET',
  20. callback => \&webmanifest,
  21. parameters => [],
  22. },
  23. );
  24. my $contenttype = "Content-type:application/json;";
  25. sub catalog ($query, $input, $=) {
  26. my $enc = JSON::MaybeXS->new( utf8 => 1 );
  27. my %rcopy = %{\%routes};
  28. foreach my $r (keys(%rcopy)) {
  29. delete $rcopy{$r}{callback}
  30. }
  31. return [200,[$contenttype],[$enc->encode(\%rcopy)]];
  32. }
  33. sub webmanifest ($query, $input, $=) {
  34. my $enc = JSON::MaybeXS->new( utf8 => 1 );
  35. my %manifest = (
  36. "icons" => [
  37. { "src" => "$theme_dir/img/icon/favicon-192.png", "type" => "image/png", "sizes" => "192x192" },
  38. { "src" => "$theme_dir/img/icon/favicon-512.png", "type" => "image/png", "sizes" => "512x512" },
  39. ],
  40. );
  41. return [ 200, [$contenttype], [$enc->encode(\%manifest)] ];
  42. }
  43. 1;