tpsgi 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!bin/tarbaby
  2. #ABSTRACT: PSGI application which simplifies migrating legacy/cgi applications to PSGI, but also has advanced features for normal psgi apps
  3. use strict;
  4. use warnings;
  5. use Cwd;
  6. use FindBin::libs;
  7. use File::Basename qw{dirname};
  8. use TPSGI;
  9. my %options = main(@ARGV);
  10. =head1 USAGE
  11. tpsgi ... starman options
  12. tpsgi obeys a config file, ~/.tpsgi.ini which is in Config::Simple (key=value) format, with the keys being:
  13. verbose # Whether to print all log messages or not
  14. custom_log # Custom location for default (rotated) log
  15. routers # Router module(s), separate with comma. Relative to TPSGI install dir.
  16. indices # Dirindex files. In addition to index.html, index.htm, index.cgi
  17. loggers # Logger module(s), separate with comma
  18. auth # Authentication module
  19. domain # Domain name of the application, used by things like service files
  20. basedir # Directory you want to chdir into on startup. Relative to TPSGI install dir. Default '.'
  21. user # Who to run as
  22. http_user # Who's proxying this application (used for statics)
  23. autoreload # Whether or not to automatically reload the application when library files change. Default 0.
  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. # Profiler is opt-in: set NYTPROF env var to enable.
  41. # The default path is used when NYTPROF is set without a file= clause.
  42. if ($ENV{NYTPROF}) {
  43. $ENV{NYTPROF} = "sigexit=1:savesrc=0:start=no:file=$options{tpsgi_dir}/prof/nytprof.out"
  44. unless $ENV{NYTPROF} =~ /file=/;
  45. require Devel::NYTProf;
  46. mkdir "$options{tpsgi_dir}/prof";
  47. }
  48. my $self = TPSGI->new(%options);
  49. # TPSGI::app already catches all _app exceptions internally.
  50. # This outer eval is a last-resort safety net only.
  51. my $result = eval { $self->app(@_) };
  52. return $result if defined $result;
  53. my $env = $_[0];
  54. $env->{'psgi.errors'}->print($@) if $env && ref $env eq 'HASH' && $env->{'psgi.errors'};
  55. return [500, ['Content-Type', 'text/plain', 'Content-Length', 21], ['Internal Server Error']];
  56. };