call.pl 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. 'psgi.errors' => *STDERR,
  18. );
  19. sub emit_error {
  20. my $env = shift;
  21. return $env->{'psgi.errors'}->print(shift) if $env->{'psgi.errors'};
  22. print $@;
  23. }
  24. our $app = sub {
  25. my %cfg = TPSGI::get_config();
  26. # We are debugging here
  27. $cfg{verbose} = 1;
  28. my $self = TPSGI->new(%cfg);
  29. local $@;
  30. chdir($cfg{'basedir'}) || warn "Can't chdir to $cfg{'basedir'}: $_";
  31. return eval { $self->app(@_) } || do {
  32. my $env = shift;
  33. emit_error($env, $@);
  34. # Redact the stack trace past line 1, it usually has things which should not be shown
  35. $self->{cur_query}->{message} = $@;
  36. $self->{cur_query}->{message} =~ s/\n.*//g if $self->{cur_query}->{message};
  37. return $self->error($self->{cur_query});
  38. };
  39. };
  40. sub stream_to_stdout {
  41. my ($input) = @_;
  42. my ( $code, $headers ) = @$input;
  43. print "RC: $code\n";
  44. print "Headers:\n";
  45. print Dumper($headers);
  46. return \*STDOUT;
  47. }
  48. my $limit = $ARGV[3] || 1;
  49. for ( 1 .. $limit ) {
  50. my $out = $app->( \%env );
  51. if (ref $out eq 'CODE') {
  52. $out->(\&stream_to_stdout);
  53. next;
  54. }
  55. if (ref $out eq 'ARRAY') {
  56. print $out->[2][0];
  57. next;
  58. }
  59. die "Weird return:\n".Dumper($out);
  60. }