]> Sergey Matveev's repositories - public-inbox.git/commitdiff
www: make interface more OO
authorEric Wong <e@80x24.org>
Thu, 25 Feb 2016 03:10:51 +0000 (03:10 +0000)
committerEric Wong <e@80x24.org>
Thu, 25 Feb 2016 04:03:04 +0000 (04:03 +0000)
This allows multiple instances the WWW app from
running within the same process space

examples/public-inbox.psgi
lib/PublicInbox/WWW.pm
public-inbox-httpd
public-inbox.cgi

index c4a8903e1639e2a101b2ea919058d0c9b2b7897e..e3e421526125a25f670205ab54a024953ad14d4b 100644 (file)
@@ -10,7 +10,7 @@ PublicInbox::WWW->preload;
 use Plack::Request;
 use Plack::Builder;
 my $have_deflater = eval { require Plack::Middleware::Deflater; 1 };
-
+my $www = PublicInbox::WWW->new;
 builder {
        enable 'Chunked';
        if ($have_deflater) {
@@ -25,8 +25,5 @@ builder {
        # See Plack::Middleware::ReverseProxy documentation for details
        # enable 'ReverseProxy';
        enable 'Head';
-       sub {
-               my $req = Plack::Request->new(@_);
-               PublicInbox::WWW::run($req, $req->method);
-       }
+       sub { $www->call(@_) };
 }
index 651c19e05bbacde34e0db79de2980db0f41efde4..e87e559428ffc55400b9942d4c1561c7197dd44e 100644 (file)
@@ -22,14 +22,26 @@ use PublicInbox::GitHTTPBackend;
 our $LISTNAME_RE = qr!\A/([\w\.\-]+)!;
 our $MID_RE = qr!([^/]+)!;
 our $END_RE = qr!(f/|T/|t/|R/|t\.mbox(?:\.gz)?|t\.atom|raw|)!;
-our $pi_config;
 
-sub run {
-       my ($cgi, $method) = @_;
+sub new {
+       my ($class, $pi_config) = @_;
        $pi_config ||= PublicInbox::Config->new;
-       my $ctx = { cgi => $cgi, pi_config => $pi_config };
+       bless { pi_config => $pi_config }, $class;
+}
+
+# backwards compatibility, do not use
+sub run {
+       my ($req, $method) = @_;
+       PublicInbox::WWW->new->call($req->env);
+}
+
+sub call {
+       my ($self, $env) = @_;
+       my $cgi = Plack::Request->new($env);
+       my $ctx = { cgi => $cgi, pi_config => $self->{pi_config} };
        my $path_info = $cgi->path_info;
 
+       my $method = $cgi->method;
        if ($method eq 'POST' &&
                 $path_info =~ m!$LISTNAME_RE/(git-upload-pack)\z!) {
                my $path = $2;
@@ -107,7 +119,7 @@ sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
 # returns undef if valid, array ref response if invalid
 sub invalid_list {
        my ($ctx, $listname) = @_;
-       my $git_dir = $pi_config->get($listname, "mainrepo");
+       my $git_dir = $ctx->{pi_config}->get($listname, "mainrepo");
        if (defined $git_dir) {
                $ctx->{git_dir} = $git_dir;
                $ctx->{git} = PublicInbox::Git->new($git_dir);
@@ -264,7 +276,7 @@ sub footer {
                        join("\n", map { "\t$_" } @urls);
        }
 
-       my $addr = $pi_config->get($listname, 'address');
+       my $addr = $ctx->{pi_config}->get($listname, 'address');
        if (ref($addr) eq 'ARRAY') {
                $addr = $addr->[0]; # first address is primary
        }
index 6436bd7df1011cbfbff3e26bb6197d59d54852d7..3635c9a75e804af9b190eda13fed7cd7dab6aeb7 100644 (file)
@@ -24,6 +24,7 @@ my $refresh = sub {
 "$0 runs in /, command-line paths must be absolute\n";
                }
        } else {
+               my $www = PublicInbox::WWW->new;
                $app = eval {
                        my $deflate_types = eval {
                                require Plack::Middleware::Deflater;
@@ -37,11 +38,7 @@ my $refresh = sub {
                                                content_type => $deflate_types
                                }
                                enable 'Head';
-                               sub {
-                                       my $req = Plack::Request->new(@_);
-                                       PublicInbox::WWW::run($req,
-                                                       $req->method);
-                               };
+                               sub { $www->call(@_) };
                        };
                };
        }
index e73e23ca7663662c907021a695a857fbdd6af4cb..ee9510c1f012757163076cf8b991af64f78ac6d8 100755 (executable)
@@ -11,7 +11,7 @@ use Plack::Request;
 use Plack::Handler::CGI;
 use PublicInbox::WWW;
 BEGIN { PublicInbox::WWW->preload if $ENV{MOD_PERL} }
-
+my $www = PublicInbox::WWW->new;
 my $have_deflater = eval { require Plack::Middleware::Deflater; 1 };
 my $app = builder {
        if ($have_deflater) {
@@ -27,9 +27,6 @@ my $app = builder {
        # enable 'ReverseProxy';
 
        enable 'Head';
-       sub {
-               my $req = Plack::Request->new(@_);
-               PublicInbox::WWW::run($req, $req->method);
-       }
+       sub { $www->call(@_) };
 };
 Plack::Handler::CGI->new->run($app);