call.pl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. =head1 USAGE
  11. call.pl METHOD ROUTE QUERY_STRING COOKIE REPEAT_X_TIMES MAX_REDIRECTS
  12. Max redirects default is 6.
  13. =cut
  14. my $host = $ENV{DOMAIN} // 'localhost';
  15. my %env = (
  16. REQUEST_METHOD => $ARGV[0],
  17. PATH_INFO => $ARGV[1],
  18. QUERY_STRING => $ARGV[2],
  19. REQUEST_URI => "http://$host/".$ARGV[1],
  20. HTTP_HOST => $host,
  21. HTTP_COOKIE => $ARGV[3],
  22. 'psgi.errors' => *STDERR,
  23. 'psgi.input' => *STDIN,
  24. );
  25. sub emit_error {
  26. my $env = shift;
  27. return $env->{'psgi.errors'}->print(shift) if $env->{'psgi.errors'};
  28. print $@;
  29. }
  30. my %cfg;
  31. our $app = sub {
  32. # Allow re-run
  33. if (%cfg) {
  34. chdir($cfg{tpsgi_dir}) || warn "Can't chdir to $cfg{tpsgi_dir}: $_";
  35. }
  36. %cfg = TPSGI::get_config();
  37. # We are debugging here
  38. $cfg{verbose} = 1;
  39. # Change your dir into whatever basedir you need to be in
  40. chdir($cfg{'basedir'}) || warn "Can't chdir to $cfg{'basedir'}: $_";
  41. # If we have manually set the NYTPROF var, use it and don't try to control
  42. # When to stop or start it.
  43. $ENV{NYTPROF} ||= "sigexit=1:savesrc=0:start=no:file=$cfg{tpsgi_dir}/prof/nytprof.out";
  44. require Devel::NYTProf;
  45. mkdir "$cfg{tpsgi_dir}/prof";
  46. my $self = TPSGI->new(%cfg);
  47. local $@;
  48. return eval { $self->app(@_) } || do {
  49. my $env = shift;
  50. emit_error($env, $@);
  51. # Redact the stack trace past line 1, it usually has things which should not be shown
  52. $self->{cur_query}->{message} = $@;
  53. $self->{cur_query}->{message} =~ s/\n.*//g if $self->{cur_query}->{message};
  54. return $self->error($self->{cur_query});
  55. };
  56. };
  57. sub stream_to_stdout {
  58. my ($input) = @_;
  59. my ( $code, $headers ) = @$input;
  60. print "RC: $code\n";
  61. print "Headers:\n";
  62. print Dumper($headers);
  63. return \*STDOUT;
  64. }
  65. my $limit = $ARGV[4] || 1;
  66. my $max_redirects = $ARGV[5] || 6;
  67. my $redirects=0;
  68. for ( 1 .. $limit ) {
  69. LOOP:
  70. my $out = $app->( \%env );
  71. if (ref $out eq 'CODE') {
  72. $out->(\&stream_to_stdout);
  73. next;
  74. }
  75. if (ref $out eq 'ARRAY') {
  76. # Might be a redirect, so check
  77. my $rc = $out->[0];
  78. my %hdr = @{$out->[1]};
  79. my $to = $hdr{Location};
  80. if ($to) {
  81. $env{REQUEST_URI} = "http://$host/$to";
  82. $env{PATH_INFO} = $to;
  83. $redirects++;
  84. die "Max redirects reached!" unless $redirects < $max_redirects;
  85. goto LOOP;
  86. }
  87. print $out->[2][0];
  88. next;
  89. }
  90. die "Weird return:\n".Dumper($out);
  91. }