File Coverage

blib/lib/Pinto/Server.pm
Criterion Covered Total %
statement 57 57 100.0
branch 6 8 75.0
condition 2 3 66.6
subroutine 17 17 100.0
pod 2 3 66.6
total 84 88 95.4


line stmt bran cond sub pod time code
1             # ABSTRACT: Web interface to a Pinto repository
2              
3             package Pinto::Server;
4              
5 13     13   1732449 use Moose;
  13         4596848  
  13         80  
6 13     13   88025 use MooseX::ClassAttribute;
  13         953631  
  13         44  
7 13     13   3319446 use MooseX::Types::Moose qw(Int HashRef);
  13         573471  
  13         108  
8              
9 13     13   54319 use Carp;
  13         18  
  13         671  
10 13     13   1338 use Path::Class;
  13         92021  
  13         561  
11 13     13   77 use Class::Load;
  13         30  
  13         451  
12 13     13   72 use Scalar::Util qw(blessed);
  13         17  
  13         487  
13 13     13   3991 use IO::Interactive qw(is_interactive);
  13         8527  
  13         81  
14 13     13   3354 use Plack::Middleware::Auth::Basic;
  13         62620  
  13         357  
15              
16 13     13   3346 use Pinto::Types qw(Dir);
  13         52  
  13         84  
17 13     13   68869 use Pinto::Constants qw(:server);
  13         27  
  13         924  
18 13     13   6056 use Pinto::Server::Router;
  13         40  
  13         426  
19 13     13   4396 use Pinto::Repository;
  13         41  
  13         4392  
20              
21             #-------------------------------------------------------------------------------
22              
23             our $VERSION = '0.13'; # VERSION
24              
25             #-------------------------------------------------------------------------------
26              
27              
28             has root => (
29             is => 'ro',
30             isa => Dir,
31             required => 1,
32             coerce => 1,
33             );
34              
35              
36             has auth => (
37             is => 'ro',
38             isa => HashRef,
39             traits => ['Hash'],
40             handles => { auth_options => 'elements' },
41             );
42              
43              
44             has router => (
45             is => 'ro',
46             isa => 'Pinto::Server::Router',
47             default => sub { Pinto::Server::Router->new },
48             lazy => 1,
49             );
50              
51              
52             class_has default_port => (
53             is => 'ro',
54             isa => Int,
55             default => $PINTO_SERVER_DEFAULT_PORT,
56             );
57              
58             #-------------------------------------------------------------------------------
59              
60             sub BUILD {
61 13     13 0 26460 my ($self) = @_;
62              
63 13         448 my $repo = Pinto::Repository->new( root => $self->root );
64 13         70 $repo->assert_sanity_ok;
65              
66 13         321 return $self;
67             }
68              
69             #-------------------------------------------------------------------------------
70              
71              
72             sub to_app {
73 13     13 1 136 my ($self) = @_;
74              
75 13     140   101 my $app = sub { $self->call(@_) };
  140         2444210  
76              
77 13 100       587 if ( my %auth_options = $self->auth_options ) {
78              
79             my $backend = delete $auth_options{backend}
80 2 50       12 or carp 'No auth backend provided!';
81              
82 2         8 my $class = 'Authen::Simple::' . $backend;
83 2 50       12 print "Authenticating using $class\n" if is_interactive;
84 2         40 Class::Load::load_class($class);
85              
86 2         20148 $app = Plack::Middleware::Auth::Basic->wrap( $app, authenticator => $class->new(%auth_options) );
87             }
88              
89 13         527 return $app;
90             }
91              
92             #-------------------------------------------------------------------------------
93              
94              
95             sub call {
96 140     140 1 465 my ( $self, $env ) = @_;
97              
98 140         4669 my $response = $self->router->route( $env, $self->root );
99              
100 130 100 66     1978 $response = $response->finalize
101             if blessed($response) && $response->can('finalize');
102              
103 130         15395 return $response;
104             }
105              
106             #-------------------------------------------------------------------------------
107             1;
108              
109             __END__
110              
111             =pod
112              
113             =encoding UTF-8
114              
115             =for :stopwords Jeffrey Ryan Thalhammer
116              
117             =head1 NAME
118              
119             Pinto::Server - Web interface to a Pinto repository
120              
121             =head1 VERSION
122              
123             version 0.13
124              
125             =head1 ATTRIBUTES
126              
127             =head2 root
128              
129             The path to the root directory of your Pinto repository. The
130             repository must already exist at this location. This attribute is
131             required.
132              
133             =head2 auth
134              
135             The hashref of authentication options, if authentication is to be used within
136             the server. One of the options must be 'backend', to specify which
137             Authen::Simple:: class to use; the other key/value pairs will be passed as-is
138             to the Authen::Simple class.
139              
140             =head2 router
141              
142             An object that does the L<Pinto::Server::Handler> role. This object
143             will do the work of processing the request and returning a response.
144              
145             =head2 default_port
146              
147             Returns the default port number that the server will listen on. This
148             is a class attribute.
149              
150             =head1 METHODS
151              
152             =head2 to_app()
153              
154             Returns the application as a subroutine reference.
155              
156             =head2 call( $env )
157              
158             Invokes the application with the specified environment. Returns a
159             PSGI-compatible response.
160              
161             There is nothing to see here.
162              
163             Look at L<pintod> if you want to start the server.
164              
165             =head1 AUTHOR
166              
167             Jeffrey Ryan Thalhammer <jeff@stratopan.com>
168              
169             =head1 COPYRIGHT AND LICENSE
170              
171             This software is copyright (c) 2015 by Jeffrey Ryan Thalhammer.
172              
173             This is free software; you can redistribute it and/or modify it under
174             the same terms as the Perl 5 programming language system itself.
175              
176             =cut