tpsgi 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env starman
  2. #pacakge bin::tpsgi;
  3. #ABSTRACT: PSGI application which simplifies migrating legacy/cgi applications to PSGI
  4. use strict;
  5. use warnings;
  6. use FindBin::libs;
  7. use TPSGI;
  8. use Getopt::Long qw{GetOptionsFromArray};
  9. use Config::Simple;
  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. The idea is that this application is going to be run in a chroot with the user's homedir being the application dir.
  21. =cut
  22. sub main {
  23. my (@args) = @_;
  24. my %options = _get_from_config();
  25. return %options;
  26. }
  27. sub _get_from_config {
  28. my %options = (
  29. verbose => 0,
  30. custom_log => undef,
  31. routers => [],
  32. loggers => [],
  33. auth => undef,
  34. );
  35. my $config_file = "$ENV{HOME}/.tpsgi.ini";
  36. if (-f $config_file) {
  37. my $conf = Config::Simple->new($config_file);
  38. my %config;
  39. %config = %{$conf->param(-block => 'default')} if $conf;
  40. # Merge the configuration with the options
  41. foreach my $opt (keys(%options)) {
  42. if ( ref $options{$opt} eq 'ARRAY' ) {
  43. next unless exists $config{$opt};
  44. my @arrayed = ref $config{$opt} eq 'ARRAY' ? @{$config{$opt}} : ($config{$opt});
  45. push(@{$options{$opt}}, @arrayed);
  46. next;
  47. }
  48. $options{$opt} = $config{$opt} if exists $config{$opt};
  49. }
  50. }
  51. return %options;
  52. }
  53. our $app = sub {
  54. $options{verbose} = 1;
  55. my $self = TPSGI->new(%options);
  56. return eval { $self->app(@_) } || do {
  57. my $env = shift;
  58. $env->{'psgi.errors'}->print($@);
  59. # Redact the stack trace past line 1, it usually has things which should not be shown
  60. $self->{cur_query}->{message} = $@;
  61. $self->{cur_query}->{message} =~ s/\n.*//g if $self->{cur_query}->{message};
  62. return $self->error($self->{cur_query});
  63. };
  64. };