call.pl 1.7 KB

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