tpsgi 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. Relative to TPSGI install dir.
  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. Relative to TPSGI install dir. Default '.'
  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. This is why the various paths asked for are relative to the tPSGI install dir.
  26. =cut
  27. sub main {
  28. my (@args) = @_;
  29. my %options = TPSGI::get_config();
  30. foreach my $route (@{$options{routers}}) {
  31. my $libdir = dirname($route);
  32. push(@INC, $libdir);
  33. }
  34. return %options;
  35. }
  36. our $app = sub {
  37. umask 0022;
  38. # Change your dir into whatever basedir you need to be in
  39. chdir("$options{tpsgi_dir}/$options{basedir}") or die "Can't chdir into $options{tpsgi_dir}/$options{'basedir'}: $!";
  40. # If we have manually set the NYTPROF var, use it and don't try to control
  41. # When to stop or start it.
  42. $ENV{NYTPROF} ||= "sigexit=1:savesrc=0:start=no:file=$options{basedir}/prof/nytprof.out";
  43. require Devel::NYTProf;
  44. mkdir "$options{basedir}/prof";
  45. my $self = TPSGI->new(%options);
  46. return eval { $self->app(@_) } || do {
  47. my $env = shift;
  48. $env->{'psgi.errors'}->print($@);
  49. # Redact the stack trace past line 1, it usually has things which should not be shown
  50. $self->{cur_query}->{message} = $@;
  51. $self->{cur_query}->{message} =~ s/\n.*//g if $self->{cur_query}->{message};
  52. return $self->error($self->{cur_query});
  53. };
  54. };