File Coverage

blib/lib/Perl/Server.pm
Criterion Covered Total %
statement 21 90 23.3
branch 2 14 14.2
condition 0 10 0.0
subroutine 7 16 43.7
pod 0 2 0.0
total 30 132 22.7


line stmt bran cond sub pod time code
1             package Perl::Server;
2              
3 5     5   222405 use strict;
  5         27  
  5         121  
4 5     5   20 use warnings;
  5         8  
  5         89  
5 5     5   20 use Cwd;
  5         13  
  5         246  
6 5     5   2043 use Plack::Runner;
  5         72950  
  5         129  
7 5     5   2583 use Term::ANSIColor;
  5         34649  
  5         271  
8 5     5   1945 use Net::EmptyPort qw/check_port/;
  5         139745  
  5         4116  
9              
10             our $VERSION = '0.08';
11              
12             sub new {
13 2     2 0 785 my $class = shift;
14 2         5 my $path = shift;
15            
16 2 100       19 return bless {
17             path => $path ? $path : getcwd,
18             type => ''
19             }, $class;
20             }
21              
22             sub run {
23 0     0 0   my $self = shift;
24 0           my @argv = @_;
25              
26 0           my $type = $self->_type;
27            
28 0           my $middleware = $self->_middleware;
29              
30 0 0         if (exists $type->{module}) {
31 0           push(@argv, '-M');
32 0           push(@argv, $type->{module});
33            
34 0           push(@argv, '-e');
35 0           push(@argv, $middleware . '; ' . $type->{eval});
36             } else {
37 0           push(@argv, '-e');
38 0           push(@argv, $middleware);
39            
40 0           push(@argv, '-a');
41 0           push(@argv, $type->{app});
42             }
43            
44 0 0         unless (grep(/^(-p|--port)$/, @argv)) {
45 0           push(@argv, '-p');
46 0           push(@argv, $self->_port);
47             }
48            
49 0           $ENV{PLACK_ENV} = 'perl-server';
50            
51 0           my $runner = Plack::Runner->new;
52 0           $runner->parse_options(@argv);
53 0           $runner->prepare_devel($runner);
54 0           $self->_message($runner);
55 0           $runner->run;
56             }
57              
58             sub _type {
59 0     0     my $self = shift;
60            
61 0           my $path = $self->{path};
62            
63 0           my $type = {};
64            
65 0 0 0       if (-d $path) {
    0          
66 0           $self->{type} = 'Folder';
67 0           $type->{module} = 'Plack::App::WWW';
68 0           $type->{eval} = "Plack::App::WWW->new(root => '$path')->to_app";
69             } elsif (-e $path && $path =~ /\.(pl|cgi)$/i) {
70 0           $self->{type} = 'File';
71 0           $type->{module} = 'Plack::App::WrapCGI';
72 0           $type->{eval} = "Plack::App::WrapCGI->new(script => '$path')->to_app";
73             } else {
74 0           $self->{type} = 'PSGI';
75 0           $type->{app} = $path;
76             }
77            
78 0           return $type;
79             }
80              
81             sub _message {
82 0     0     my ($self, $runner) = @_;
83            
84 0           push @{$runner->{options}}, server_ready => sub {
85 0     0     my $args = shift;
86 0   0       my $server = $args->{server_software} || ref($args);
87 0   0       my $host = $args->{host} || 0;
88 0   0       my $proto = $args->{proto} || 'http';
89 0           my $port = $args->{port};
90            
91 0           $self->_name;
92 0           $self->_print('Version', $VERSION);
93 0           $self->_print('Server', $server);
94 0           $self->_print('Type', $self->{type});
95 0           $self->_print('Path', $self->{path});
96 0           $self->_print('Available on', "$proto://$host:$port");
97 0           $self->_stop;
98 0           };
99             }
100              
101             sub _middleware {
102 0     0     my $middleware = 'enable "AccessLog", format => \'%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-agent}i"\'';
103            
104 0           return $middleware;
105             }
106              
107             sub _port {
108 0     0     my $port = 3000;
109            
110 0 0         return $port unless check_port($port);
111            
112 0           while ($port++ < 65000) {
113 0 0         last unless check_port($port);
114             }
115            
116 0           return $port;
117             }
118              
119             sub _name {
120 0     0     print STDERR color('bold blue');
121 0           print STDERR "Perl::Server\n\n";
122             }
123              
124             sub _stop {
125 0     0     print STDERR color('reset');
126 0           print STDERR color('white');
127 0           print STDERR "\nHit CTRL-C to stop the perl-server\n\n";
128             }
129              
130             sub _print {
131 0     0     my ($self, $name, $value) = @_;
132            
133 0           print STDERR color('reset');
134 0           print STDERR color('yellow');
135 0           print STDERR "$name: ";
136 0           print STDERR color('reset');
137 0           print STDERR color('green');
138 0           print STDERR "$value\n";
139             }
140              
141             1;
142              
143             __END__