call.pl 1.6 KB

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