JSON.pm 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. use constant routes => {
  11. '/api/catalog' => {
  12. method => 'GET',
  13. callback => \&catalog,
  14. parameters => [],
  15. },
  16. '/api/webmanifest' => {
  17. method => 'GET',
  18. callback => \&webmanifest,
  19. parameters => [],
  20. },
  21. '/api/version' => {
  22. method => 'GET',
  23. callback => \&version,
  24. parameters => [],
  25. },
  26. };
  27. # Clone / redact for catalog
  28. my $cloned = clone(\%routes);
  29. foreach my $r (keys(%$cloned)) {
  30. delete $cloned->{$r}{callback}
  31. }
  32. my $enc = JSON::MaybeXS->new( utf8 => 1 );
  33. # Note to authors, don't forget to update this
  34. sub _version () {
  35. return '1.0';
  36. }
  37. sub version ($query) {
  38. state $ret = [200, ['Content-type' => "application/json", ETag => 'version-'._version()],[_version()]];
  39. return $ret;
  40. }
  41. sub catalog ($query) {
  42. state $ret = [200, ['Content-type' => "application/json", ETag => 'catalog-'._version()], [$enc->encode($cloned)]];
  43. return $ret;
  44. }
  45. sub webmanifest ($query) {
  46. my $theme_dir = Trog::Config::theme_dir();
  47. state $headers = ['Content-type' => "application/json", ETag => 'manifest-'._version()];
  48. state %manifest = (
  49. "icons" => [
  50. { "src" => "$theme_dir/img/icon/favicon-192.png", "type" => "image/png", "sizes" => "192x192" },
  51. { "src" => "$theme_dir/img/icon/favicon-512.png", "type" => "image/png", "sizes" => "512x512" },
  52. ],
  53. );
  54. state $content = $enc->encode(\%manifest);
  55. return [ 200, $headers, [$content] ];
  56. }
  57. 1;