summaryrefslogtreecommitdiffstats
path: root/lib/Plack/Client/Backend/psgi_local.pm
blob: 65a1c31b09388be57ef3bcc6d25945b6944e25ef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package Plack::Client::Backend::psgi_local;
use strict;
use warnings;

use Plack::Middleware::ContentLength;

sub new {
    my $class = shift;
    my %params = @_;

    die 'apps must be a hashref'
        if ref($params{apps}) ne 'HASH';

    bless {
        apps => $params{apps},
    }, $class;
}

sub apps { shift->{apps} }

sub app_for {
    my $self = shift;
    my ($for) = @_;
    return $self->apps->{$for};
}

sub app_from_request {
    my $self = shift;
    my ($req) = @_;

    my $app_name = $req->env->{'plack.client.authority'};
    if (!defined $app_name) {
        $app_name = $req->uri->authority;
        $app_name =~ s/(.*):.*/$1/; # in case a port was added at some point
    }
    my $app = $self->app_for($app_name);
    die "Unknown app: $app_name" unless $app;
    return Plack::Middleware::ContentLength->wrap($app);
}

1;