tpsgi 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!bin/tarbaby
  2. # PODNAME: tpsgi
  3. # ABSTRACT: PSGI application which simplifies migrating legacy/cgi applications to PSGI, but also has advanced features for normal psgi apps
  4. use 5.040;
  5. use warnings FATAL => 'all';
  6. use re '/aa';
  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. Loaded by name, from beside a router or @INC
  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 %config = TPSGI::get_config();
  31. foreach my $route ( @{ $config{routers} } ) {
  32. my $libdir = dirname($route);
  33. push( @INC, $libdir );
  34. }
  35. return %config;
  36. }
  37. our $app = sub {
  38. umask 0o022;
  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{tpsgi_dir}/prof/nytprof.out";
  44. require Devel::NYTProf;
  45. mkdir "$options{tpsgi_dir}/prof";
  46. # new() loads the routers, so a router that will not compile dies here,
  47. # before app() can catch anything.
  48. my $self = eval { TPSGI->new(%options) } or return TPSGI::cannot_start( $@, $_[0], %options );
  49. return $self->app(@_);
  50. };