api.cgi 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/local/cpanel/3rdparty/bin/perl
  2. #ACLS:all
  3. package Troglodyne::CGI::API;
  4. use strict;
  5. use warnings;
  6. use Cpanel::LoadModule::Custom ();
  7. use JSON::XS ();
  8. run() unless caller();
  9. sub run {
  10. # Load up CGI processing modules
  11. Cpanel::LoadModule::Custom::load_perl_module("Troglodyne::CGI");
  12. # Process the args
  13. my $args = Troglodyne::CGI::get_args();
  14. # XXX Validation plz
  15. # Load route code
  16. my $namespace = "Troglodyne::API::$args->{'module'}";
  17. my ( $loaded, $err, $coderef );
  18. {
  19. local $@;
  20. $loaded = eval {
  21. Cpanel::LoadModule::Custom::load_perl_module($namespace);
  22. };
  23. $err = $@;
  24. $coderef = $namespace->can($args->{'function'});
  25. }
  26. # Get back the datastruct from the called module.
  27. # Yeah, yeah, I know. String eval. XXX
  28. my $ret = {
  29. 'metadata' => {
  30. 'input_args' => $args,
  31. },
  32. };
  33. if( $loaded && $coderef ) {
  34. local $@;
  35. my $data = eval { $coderef->() };
  36. my $error = $@;
  37. if($data) {
  38. $ret->{'data'} = $data;
  39. $ret->{'result'} = 1;
  40. } else {
  41. $ret->{'result'} = 0;
  42. $ret->{'error'} = $error;
  43. }
  44. } elsif( !$coderef ) {
  45. $ret->{'error'} = "No such function '$args->{'function'}' in $namespace";
  46. $ret->{'result'} = 0;
  47. } else {
  48. $ret->{'error'} = $err;
  49. $ret->{'result'} = 0;
  50. }
  51. # Emit the JSON
  52. print "Content-type: application/json\r\n\r\n";
  53. print JSON::XS::encode_json($ret);
  54. exit;
  55. }
  56. 1;