tpsgi 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. autoreload # Whether or not to automatically reload the application when library files change. Default 0.
  25. The idea is that this application is going to be run in a chroot with the user's homedir being the application dir.
  26. This is why the various paths asked for are relative to the tPSGI install dir.
  27. =cut
  28. sub main {
  29. my (@args) = @_;
  30. my %options = TPSGI::get_config();
  31. foreach my $route (@{$options{routers}}) {
  32. my $libdir = dirname($route);
  33. push(@INC, $libdir);
  34. }
  35. return %options;
  36. }
  37. our $app = sub {
  38. umask 0022;
  39. # Change your dir into whatever basedir you need to be in
  40. chdir("$options{tpsgi_dir}/$options{basedir}") or die "Can't chdir into $options{tpsgi_dir}/$options{'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=$options{basedir}/prof/nytprof.out";
  44. require Devel::NYTProf;
  45. mkdir "$options{basedir}/prof";
  46. my $self = TPSGI->new(%options);
  47. return eval { $self->app(@_) } || do {
  48. my $env = shift;
  49. $env->{'psgi.errors'}->print($@);
  50. # Redact the stack trace past line 1, it usually has things which should not be shown
  51. $self->{cur_query}->{message} = $@;
  52. $self->{cur_query}->{message} =~ s/\n.*//g if $self->{cur_query}->{message};
  53. return $self->error($self->{cur_query});
  54. };
  55. };