Perl-Server

 view release on metacpan or  search on metacpan

lib/Perl/Server.pm  view on Meta::CPAN

package Perl::Server;

use strict;
use warnings;
use Cwd;
use Plack::Runner;
use Term::ANSIColor;
use Getopt::Long;
use Net::EmptyPort qw/empty_port check_port/;

our $VERSION = '0.10';

sub new {
    my $class = shift;
    my $path  = shift;
    
    return bless {
        path => $path ? $path : getcwd,
        type => ''
    }, $class;
}

sub run {
    my $self = shift;
    my @argv = @_;
    
    local @ARGV = @argv;   
    
    my $parser = Getopt::Long::Parser->new(
        config => [ "no_auto_abbrev", "no_ignore_case", "pass_through" ],
    ); 
    
    my $port;
    
    $parser->getoptions(
        "p|port=s" => \$port
    );      

    my $type = $self->_type;   
    
    my $middleware = $self->_middleware;

    if (exists $type->{module}) {
        push(@argv, '-M');
        push(@argv, $type->{module});
        
        push(@argv, '-e');
        push(@argv, $middleware . '; ' . $type->{eval});                 
    } else {
        push(@argv, '-e');
        push(@argv, $middleware);          
        
        push(@argv, '-a');
        push(@argv, $type->{app});        
    }
    
    if ($port) {
        push(@argv, '-p');
        push(@argv, $port =~ /^e(mpty)?$/i ? $self->_port(1) : ($port =~ /\D/ ? $self->_port : $port));        
    } else {
        push(@argv, '-p');
        push(@argv, $self->_port);      
    }
    
    $ENV{PLACK_ENV} = 'perl-server';       
    
    my $runner = Plack::Runner->new;    
    $runner->parse_options(@argv);   
    $runner->prepare_devel($runner);
    $self->_message($runner);
    $runner->run;
}

sub _type {
    my $self = shift;
    
    my $path = $self->{path};
    
    my $type = {};
        
    if (-d $path) {
        $self->{type} = 'Folder';
        $type->{module} = 'Plack::App::WWW';
        $type->{eval}   = "Plack::App::WWW->new(root => '$path')->to_app";        
    } elsif (-e $path && $path =~ /\.(pl|cgi)$/i) {
        $self->{type} = 'File';
        $type->{module} = 'Plack::App::WrapCGI';



( run in 0.380 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )