api.cgi 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. my $data = $coderef->();
  35. $ret->{'data'} = $data;
  36. $ret->{'result'} = 1;
  37. } elsif( !$coderef ) {
  38. $ret->{'error'} = "No such function '$args->{'function'}' in $namespace";
  39. $ret->{'result'} = 0;
  40. } else {
  41. $ret->{'error'} = $err;
  42. $ret->{'result'} = 0;
  43. }
  44. # Emit the JSON
  45. print "Content-type: application/json\r\n\r\n";
  46. print JSON::XS::encode_json($ret);
  47. exit;
  48. }
  49. 1;