call.pl 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. # Change your dir into whatever basedir you need to be in
  30. chdir($cfg{'basedir'}) || warn "Can't chdir to $cfg{'basedir'}: $_";
  31. # If we have manually set the NYTPROF var, use it and don't try to control
  32. # When to stop or start it.
  33. $ENV{NYTPROF} ||= "sigexit=1:savesrc=0:start=no:file=$cfg{tpsgi_dir}/prof/nytprof.out";
  34. require Devel::NYTProf;
  35. mkdir "$cfg{tpsgi_dir}/prof";
  36. my $self = TPSGI->new(%cfg);
  37. local $@;
  38. return eval { $self->app(@_) } || do {
  39. my $env = shift;
  40. emit_error($env, $@);
  41. # Redact the stack trace past line 1, it usually has things which should not be shown
  42. $self->{cur_query}->{message} = $@;
  43. $self->{cur_query}->{message} =~ s/\n.*//g if $self->{cur_query}->{message};
  44. return $self->error($self->{cur_query});
  45. };
  46. };
  47. sub stream_to_stdout {
  48. my ($input) = @_;
  49. my ( $code, $headers ) = @$input;
  50. print "RC: $code\n";
  51. print "Headers:\n";
  52. print Dumper($headers);
  53. return \*STDOUT;
  54. }
  55. my $limit = $ARGV[3] || 1;
  56. for ( 1 .. $limit ) {
  57. my $out = $app->( \%env );
  58. if (ref $out eq 'CODE') {
  59. $out->(\&stream_to_stdout);
  60. next;
  61. }
  62. if (ref $out eq 'ARRAY') {
  63. print $out->[2][0];
  64. next;
  65. }
  66. die "Weird return:\n".Dumper($out);
  67. }