MultiTenant.pm

filter: Statementstmt 70.8% 56 / 79 Branchbran 25.0% 3 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 83.3% 15 / 18 Podpod 100% 2 / 2 totaltotal 68.4% 76 / 111 CC 9 SCAR 16.5
CC
9
Cov
65%
about
20.0
app
6.6
__ANON__
2.5
CRAP 5.2
« MultiTenant/Config.pm ↑ Summary
1 package Dezi::MultiTenant;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Dezi::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Dezi::MultiTenant::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Plack::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Plack::App::URLMap;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 our $VERSION = '0.003';
16  
17 =head1 NAME
18  
19 Dezi::MultiTenant - multiple Dezi::Server applications in a single instance
20  
21 =head1 SYNOPSIS
22  
23 % dezi --server-class Dezi::MultiTenant --dezi-config myconfig.pl
24
25 # or in your own Plack app
26
27 use Plack::Runner;
28 use Dezi::MultiTenant;
29
30 my $multitenant_config = {
31 'foo' => Dezi::Config->new(\%foo_opts),
32 'bar' => Dezi::Config->new(\%bar_opts),
33 };
34
35 my $runner = Plack::Runner->new();
36 my $app = Dezi::MultiTenant->app( $multitenant_config );
37 $runner->run($app);
38
39 # /foo mounts a Dezi::Server
40 # /bar mounts a Dezi::Server
41
42 =head1 DESCRIPTION
43  
44 Dezi::MultiTenant provides a simple way to mount multiple
45 Dezi::Server applications in a single Plack app using
46 a single configuration structure.
47  
48 Dezi::Server allows you to serve multiple indexes, but all
49 the indexes must have identical schemas.
50  
51 Dezi::MultiTenant allows you to server multiple indexes per
52 server, and each server can have a different schema, as well
53 as individual administration, logging, unique configuration, etc.
54  
55 =head1 METHODS
56  
57 =cut
58  
59 =head2 app( I<config> )
60  
61 Returns Plack $app instance via Plack::Builder.
62  
63 I<config> should either be a hashref with keys representing each
64 Dezi::Server's mount point, or a Dezi::MultiTenant::Config object.
65 By default the root '/' mount point is reserved for the
66 Dezi::MultiTenant->about() method response. Hash keys should have
67 the '/' prefix (same syntax as L<Plack::App::URLMap>).
68  
69 =cut
70  
71 1 ▶sub app {
Subroutine app: called CC=5 SCAR=18.9
Pod documented
72 1 my $class = shift;
73 1 ▶ my $config = shift or croak "config required";
Branch: 1 branch
BranchTrueFalseTotal
my $config = shift() or croak("config required") 1 0 1
74 1 my $mt_config;
75 1 ▶ if ( blessed $config) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($config)) { } 0 1 1
76 0 ▶ if ( $config->isa('Dezi::MultiTenant::Config') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->isa('Dezi::MultiTenant::Config')) { } 0 0 0
77 0 $mt_config = $config;
78 }
79 else {
80 0 croak "config is not a Dezi::MultiTenant::Config-derived object";
81 }
82 }
83 else {
84 1 $mt_config = Dezi::MultiTenant::Config->new($config);
85 }
86  
87 1 my $url_map = Plack::App::URLMap->new();
88 1 my @loaded;
89 1 for my $path ( $mt_config->paths() ) {
90 $url_map->map(
91 2 ▶ $path => builder {
Subroutine __ANON__: called CC=1 SCAR=0.0
92 2 mount '/' =>
93 Dezi::Server->app( $mt_config->config_for($path) );
94 }
95 2 );
96 2 push @loaded, $path;
97 }
98  
99 1 ▶ return builder {
Subroutine __ANON__: called CC=2 SCAR=9.2
100  
101 # global logging
102 1 ▶ enable "SimpleLogger", level => $config->{'debug'} ? "debug" : "warn";
Branch: 1 branch
BranchTrueFalseTotal
$config->{'debug'} ? : 0 1 1
103  
104 # optional gzip compression for clients that request it
105 # client must set "Accept-Encoding" request header
106 1 enable "Deflater",
107 content_type => [
108 'text/css', 'text/html',
109 'text/javascript', 'application/javascript',
110 'text/xml', 'application/xml',
111 'application/json',
112 ],
113 vary_user_agent => 1;
114  
115 # JSONP response based on 'callback' param
116 1 enable "JSONP";
117  
118 # / is self-description
119 $url_map->map(
120 0 ▶ '/' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
121 0 my $req = Plack::Request->new(shift);
122 0 return $class->about( $req, \@loaded );
123 }
124 1 );
125  
126 $url_map->map(
127 0 ▶ '/favicon.ico' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
128 0 my $req = Plack::Request->new(shift);
129 0 my $res = $req->new_response();
130 0 $res->redirect( 'http://dezi.org/favicon.ico', 301 );
131 0 $res->finalize();
132 }
133 1 );
134  
135 # TODO /admin
136  
137 1 $url_map->to_app;
138 1 };
139  
140 }
141  
142 =head2 about( I<plack_request>, I<loaded> )
143  
144 Returns Plack::Response-like array ref
145 describing the multi-tenant application.
146  
147 =cut
148  
149 0 ▶sub about {
Subroutine about: not called CC=4 SCAR=30.0
Pod documented
150 0 my ( $class, $req, $loaded ) = @_;
151  
152 0 ▶ if ( $req->path ne '/' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->path ne "/") 0 0 0
153 0 my $resp = 'Resource not found';
154 return [
155 0 404,
156 [ 'Content-Type' => 'text/plain',
157 'Content-Length' => length $resp,
158 ],
159 [$resp]
160 ];
161 }
162  
163 0 my $base_uri = $req->base;
164 0 $base_uri =~ s,/$,,; # zap any trailing /
165 0 my %avail = ();
166 0 for my $i (@$loaded) {
167  
168 # virtual host check
169 0 ▶ if ( $i =~ m!^https?:! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i =~ /^https?:/) { } 0 0 0
170 0 $avail{$i} = $i;
171 }
172 else {
173 0 $avail{$i} = $base_uri . $i;
174 }
175 }
176  
177 0 my $about = {
178 description => $class,
179 version => $VERSION,
180 available => \%avail,
181 };
182 0 my $resp = to_json($about);
183 return [
184 0 200,
185 [ 'Content-Type' => 'application/json',
186 'Content-Length' => length $resp,
187 ],
188 [$resp],
189 ];
190 }
191  
192 3 1;
193  
194 __END__
« MultiTenant/Config.pm ↑ Summary