X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FScript%2FServer.pm;h=caa148ac93228bdd6bc8903c77fc64f356a44714;hb=34be7d45d60869f19ba27d64dc1bbf0df4c6ccba;hp=e40da5ea021fb8d4e29230b00855b1d5c931cd29;hpb=60bfba3ff3546d7e646699b944a75a4b01e50fae;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Script/Server.pm b/lib/Catalyst/Script/Server.pm index e40da5e..caa148a 100644 --- a/lib/Catalyst/Script/Server.pm +++ b/lib/Catalyst/Script/Server.pm @@ -6,12 +6,13 @@ BEGIN { } use Moose; -use Catalyst::Restarter; use MooseX::Types::Moose qw/ArrayRef Str Bool Int/; use namespace::autoclean; with 'Catalyst::ScriptRole'; +__PACKAGE__->meta->get_attribute('help')->cmd_aliases('?'); + has debug => ( traits => [qw(Getopt)], cmd_aliases => 'd', @@ -22,10 +23,11 @@ has debug => ( has host => ( traits => [qw(Getopt)], + cmd_aliases => 'h', isa => Str, is => 'ro', default => 'localhost', - documentation => 'Specify a host for the server to run on', + documentation => 'Specify an IP on this host for the server to bind to', ); has fork => ( @@ -33,16 +35,17 @@ has fork => ( cmd_aliases => 'f', isa => Bool, is => 'ro', - documentation => 'Fork the server', + default => 0, + documentation => 'Fork the server to be able to serve multiple requests at once', ); -has listen => ( +has port => ( traits => [qw(Getopt)], - cmd_aliases => 'l', + cmd_aliases => 'p', isa => Int, is => 'ro', default => 3000, - documentation => 'Specify a different listening port', + documentation => 'Specify a different listening port (to the default port 3000)', ); has pidfile => ( @@ -58,8 +61,8 @@ has keepalive => ( cmd_aliases => 'k', isa => Bool, is => 'ro', - documentation => 'Server keepalive', - + default => 0, + documentation => 'Support keepalive', ); has background => ( @@ -67,6 +70,7 @@ has background => ( cmd_aliases => 'bg', isa => Bool, is => 'ro', + default => 0, documentation => 'Run in the background', ); @@ -75,7 +79,8 @@ has restart => ( cmd_aliases => 'r', isa => Bool, is => 'ro', - documentation => 'use Catalyst::Restarter to detect code changes', + default => 0, + documentation => 'use Catalyst::Restarter to detect code changes and restart the application', ); has restart_directory => ( @@ -83,26 +88,26 @@ has restart_directory => ( cmd_aliases => 'rdir', isa => ArrayRef[Str], is => 'ro', - predicate => '_has_restart_directory', documentation => 'Restarter directory to watch', + predicate => '_has_restart_directory', ); has restart_delay => ( traits => [qw(Getopt)], - cmd_aliases => 'rdel', + cmd_aliases => 'rd', isa => Int, is => 'ro', - predicate => '_has_restart_delay', documentation => 'Set a restart delay', + predicate => '_has_restart_delay', ); has restart_regex => ( traits => [qw(Getopt)], - cmd_aliases => 'rxp', + cmd_aliases => 'rr', isa => Str, is => 'ro', - predicate => '_has_restart_regex', documentation => 'Restart regex', + predicate => '_has_restart_regex', ); has follow_symlinks => ( @@ -110,48 +115,51 @@ has follow_symlinks => ( cmd_aliases => 'sym', isa => Bool, is => 'ro', - predicate => '_has_follow_symlinks', + default => 0, documentation => 'Follow symbolic links', - + predicate => '_has_follow_symlinks', ); -sub run { - my ($self) = shift; +sub _restarter_args { + my $self = shift; - if ( $self->debug ) { - $ENV{CATALYST_DEBUG} = 1; - } + my %args = ( + argv => $self->ARGV, + start_sub => sub { $self->_run_application }, + ($self->_has_follow_symlinks ? (follow_symlinks => $self->follow_symlinks) : ()), + ($self->_has_restart_delay ? (sleep_interval => $self->restart_delay) : ()), + ($self->_has_restart_directory ? (directories => $self->restart_directory) : ()), + ($self->_has_restart_regex ? (filter => qr/${\$self->restart_regex}/) : ()), + ); + + return %args; +} - # If we load this here, then in the case of a restarter, it does not - # need to be reloaded for each restart. - require Catalyst; +sub run { + my $self = shift; - # If this isn't done, then the Catalyst::Devel tests for the restarter - # fail. - $| = 1 if $ENV{HARNESS_ACTIVE}; + local $ENV{CATALYST_DEBUG} = 1 + if $self->debug; if ( $self->restart ) { die "Cannot run in the background and also watch for changed files.\n" if $self->background; + # If we load this here, then in the case of a restarter, it does not + # need to be reloaded for each restart. + require Catalyst; + + # If this isn't done, then the Catalyst::Devel tests for the restarter + # fail. + $| = 1 if $ENV{HARNESS_ACTIVE}; + require Catalyst::Restarter; my $subclass = Catalyst::Restarter->pick_subclass; - my %args; - $args{follow_symlinks} = $self->follow_symlinks - if $self->_has_follow_symlinks; - $args{directories} = $self->restart_directory - if $self->_has_restart_directory; - $args{sleep_interval} = $self->restart_delay - if $self->_has_restart_delay; - $args{filter} = qr/$self->restart_regex/ - if $self->_has_restart_regex; - + my $restarter = $subclass->new( - %args, - start_sub => sub { $self->_run_application }, - argv => $self->ARGV, + $self->_restarter_args() ); $restarter->run_and_watch; @@ -166,7 +174,7 @@ sub run { sub _application_args { my ($self) = shift; return ( - $self->listen, + $self->port, $self->host, { map { $_ => $self->$_ } qw/ @@ -197,8 +205,8 @@ Catalyst::Script::Server - Catalyst test server -d --debug force debug mode -f --fork handle each request in a new process (defaults to false) - -h --help display this help and exits - --host host (defaults to all) + --help display this help and exits + -h --host host (defaults to all) -p --port port (defaults to 3000) -k --keepalive enable keep-alive connections -r --restart restart when files get modified