call.pl 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env perl
  2. # Use to call specific routes with the same configuration your website has
  3. use strict;
  4. use warnings;
  5. use Data::Dumper;
  6. use FindBin::libs;
  7. use Carp::Always;
  8. #Grab our custom routes
  9. use TPSGI;
  10. my $host = $ENV{DOMAIN} // 'localhost';
  11. my %env = (
  12. REQUEST_METHOD => $ARGV[0],
  13. PATH_INFO => $ARGV[1],
  14. QUERY_STRING => $ARGV[2],
  15. REQUEST_URI => "http://$host/".$ARGV[1],
  16. HTTP_HOST => $host
  17. );
  18. sub emit_error {
  19. my $env = shift;
  20. return $env->{'psgi.errors'}->print(shift) if $env->{'psgi.errors'};
  21. print $@;
  22. }
  23. our $app = sub {
  24. my %cfg = TPSGI::get_config();
  25. my $self = TPSGI->new(%cfg);
  26. local $@;
  27. chdir($cfg{'basedir'}) || warn "Can't chdir to $cfg{'basedir'}: $_";
  28. return eval { $self->app(@_) } || do {
  29. my $env = shift;
  30. emit_error($env, $@);
  31. # Redact the stack trace past line 1, it usually has things which should not be shown
  32. $self->{cur_query}->{message} = $@;
  33. $self->{cur_query}->{message} =~ s/\n.*//g if $self->{cur_query}->{message};
  34. return $self->error($self->{cur_query});
  35. };
  36. };
  37. sub stream_to_stdout {
  38. my ($input) = @_;
  39. my ( $code, $headers ) = @$input;
  40. print "RC: $code\n";
  41. print "Headers:\n";
  42. print Dumper($headers);
  43. return \*STDOUT;
  44. }
  45. my $limit = $ARGV[3] || 1;
  46. for ( 1 .. $limit ) {
  47. my $out = $app->( \%env );
  48. if (ref $out eq 'CODE') {
  49. $out->(\&stream_to_stdout);
  50. next;
  51. }
  52. if (ref $out eq 'ARRAY') {
  53. print $out->[2][0];
  54. next;
  55. }
  56. die "Weird return:\n".Dumper($out);
  57. }