JSON.pm 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package Trog::Routes::JSON;
  2. use strict;
  3. use warnings;
  4. no warnings 'experimental';
  5. use feature qw{signatures state};
  6. use Clone qw{clone};
  7. use JSON::MaybeXS();
  8. use Trog::Config();
  9. my $conf = Trog::Config::get();
  10. # TODO de-duplicate this, it's shared in html
  11. my $theme_dir = '';
  12. $theme_dir = "themes/".$conf->param('general.theme') if $conf->param('general.theme') && -d "www/themes/".$conf->param('general.theme');
  13. our %routes = (
  14. '/api/catalog' => {
  15. method => 'GET',
  16. callback => \&catalog,
  17. parameters => [],
  18. },
  19. '/api/webmanifest' => {
  20. method => 'GET',
  21. callback => \&webmanifest,
  22. parameters => [],
  23. },
  24. '/api/version' => {
  25. method => 'GET',
  26. callback => \&version,
  27. parameters => [],
  28. },
  29. );
  30. # Clone / redact for catalog
  31. my $cloned = clone(\%routes);
  32. foreach my $r (keys(%$cloned)) {
  33. delete $cloned->{$r}{callback}
  34. }
  35. my $enc = JSON::MaybeXS->new( utf8 => 1 );
  36. # Note to authors, don't forget to update this
  37. sub _version () {
  38. return '1.0';
  39. }
  40. sub version ($query) {
  41. state $ret = [200, ['Content-type' => "application/json", ETag => 'version-'._version()],[_version()]];
  42. return $ret;
  43. }
  44. sub catalog ($query) {
  45. state $ret = [200, ['Content-type' => "application/json", ETag => 'catalog-'._version()], [$enc->encode($cloned)]];
  46. return $ret;
  47. }
  48. sub webmanifest ($query) {
  49. state $headers = ['Content-type' => "application/json", ETag => 'manifest-'._version()];
  50. state %manifest = (
  51. "icons" => [
  52. { "src" => "$theme_dir/img/icon/favicon-192.png", "type" => "image/png", "sizes" => "192x192" },
  53. { "src" => "$theme_dir/img/icon/favicon-512.png", "type" => "image/png", "sizes" => "512x512" },
  54. ],
  55. );
  56. state $content = $enc->encode(\%manifest);
  57. return [ 200, $headers, [$content] ];
  58. }
  59. 1;