tpsgi 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env starman
  2. #pacakge bin::tpsgi;
  3. #ABSTRACT: PSGI application which simplifies migrating legacy/cgi applications to PSGI, but also has advanced features for normal psgi apps
  4. use strict;
  5. use warnings;
  6. use Cwd;
  7. use FindBin::libs;
  8. use File::Basename qw{dirname};
  9. use TPSGI;
  10. my %options = main(@ARGV);
  11. =head1 USAGE
  12. tpsgi ... starman options
  13. tpsgi obeys a config file, ~/.tpsgi.ini which is in Config::Simple (key=value) format, with the keys being:
  14. verbose # Whether to print all log messages or not
  15. custom_log # Custom location for default (rotated) log
  16. routers # Router module(s), separate with comma
  17. indices # Dirindex files. In addition to index.html, index.htm, index.cgi
  18. loggers # Logger module(s), separate with comma
  19. auth # Authentication module
  20. domain # Domain name of the application, used by things like service files
  21. basedir # Directory you want to chdir into on startup (defaults to HOME).
  22. user # Who to run as
  23. http_user # Who's proxying this application (used for statics)
  24. The idea is that this application is going to be run in a chroot with the user's homedir being the application dir.
  25. =cut
  26. sub main {
  27. my (@args) = @_;
  28. my %options = TPSGI::get_config();
  29. foreach my $route (@{$options{routers}}) {
  30. my $libdir = dirname($route);
  31. push(@INC, $libdir);
  32. }
  33. return %options;
  34. }
  35. our $app = sub {
  36. umask 0022;
  37. # Change your dir into whatever basedir you need to be in
  38. chdir($options{'basedir'}) or die "Can't chdir into $options{'basedir'}: $!";
  39. # If we have manually set the NYTPROF var, use it and don't try to control
  40. # When to stop or start it.
  41. $ENV{NYTPROF} ||= "sigexit=1:savesrc=0:start=no:file=$options{basedir}/prof/nytprof.out";
  42. require Devel::NYTProf;
  43. mkdir "$options{basedir}/prof";
  44. my $self = TPSGI->new(%options);
  45. return eval { $self->app(@_) } || do {
  46. my $env = shift;
  47. $env->{'psgi.errors'}->print($@);
  48. # Redact the stack trace past line 1, it usually has things which should not be shown
  49. $self->{cur_query}->{message} = $@;
  50. $self->{cur_query}->{message} =~ s/\n.*//g if $self->{cur_query}->{message};
  51. return $self->error($self->{cur_query});
  52. };
  53. };