Skip to content

Commit

Permalink
cleaned up the whole script system
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 28, 2009
1 parent d80c315 commit 0112933
Show file tree
Hide file tree
Showing 22 changed files with 344 additions and 252 deletions.
4 changes: 4 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
This file documents the revision history for Perl extension Mojo.

0.991242 2009-07-20 00:00:00
- Cleaned up the whole script system, this change is mostly backwards
compatible except for a few cases.
"daemon $port" now becomes "daemon -p $port"
"mojolicious mojo $script" becomes "mojolicious $script"
- Added HTML escape expression marks "<%==" and "%==" to
Mojo::Template.
- Added more Mojolicious::Lite examples and reformatted them into a
Expand Down
1 change: 1 addition & 0 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ WriteMakefile(
'File::Spec::Functions' => 0,
'File::Temp' => 0,
'FindBin' => 0,
'Getopt::Long' => 0,
'IO::File' => 0,
'IO::Poll' => 0,
'IO::Socket' => 0,
Expand Down
17 changes: 17 additions & 0 deletions lib/Mojo/Script.pm
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use Mojo::Template;
__PACKAGE__->attr('description', default => 'No description.');
__PACKAGE__->attr('quiet', default => 0);
__PACKAGE__->attr('renderer', default => sub { Mojo::Template->new });
__PACKAGE__->attr('usage', default => "usage: $0\n");

sub chmod_file {
my ($self, $path, $mod) = @_;
Expand Down Expand Up @@ -113,6 +114,12 @@ sub get_data {
return;
}
sub help {
my $self = shift;
print $self->usage;
exit;
}
sub rel_dir {
my ($self, $path) = @_;
Expand Down Expand Up @@ -221,6 +228,7 @@ Mojo::Script - Script Base Class
1;
__DATA__
@@ foo_bar
% for (1 .. 5) {
Hello World!
Expand All @@ -244,6 +252,11 @@ L<Mojo::Script> implements the following attributes.
my $quiet = $script->quiet;
$script = $script->quiet(1);
=head2 C<usage>
my $usage = $script->usage;
$script = $script->usage('Foo!');
=head1 METHODS
L<Mojo::Script> inherits all methods from L<Mojo::Base> and implements the
Expand Down Expand Up @@ -277,6 +290,10 @@ following new ones.
my $data = $script->get_data('foo_bar');
=head2 C<help>
$script->help;
=head2 C<rel_dir>
my $path = $script->rel_dir('foo/bar');
Expand Down
33 changes: 29 additions & 4 deletions lib/Mojo/Script/Cgi.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,35 @@ use base 'Mojo::Script';

use Mojo::Server::CGI;

use Getopt::Long 'GetOptionsFromArray';

__PACKAGE__->attr('description', default => <<'EOF');
* Start the cgi script. *
Takes no options.
cgi
Start application with CGI backend.
EOF
__PACKAGE__->attr('usage', default => <<"EOF");
usage: $0 cgi [OPTIONS]
These options are available:
--nph Enable non-parsed-header mode.
--help Display this message and exit.
EOF

# Hi, Super Nintendo Chalmers!
sub run {
Mojo::Server::CGI->new->run;
my $self = shift;
my $cgi = Mojo::Server::CGI->new;

# Options
my @options = @_ ? @_ : @ARGV;
GetOptionsFromArray(
\@options,
'help' => sub { $self->help },
'nph' => sub { $cgi->nph(1) }
);

# Run
$cgi->run;

return shift;
}

Expand Down Expand Up @@ -49,6 +69,11 @@ implements the following new ones.
my $description = $cgi->description;
$cgi = $cgi->description('Foo!');
=head2 C<usage>
my $usage = $cgi->usage;
$cgi = $cgi->usage('Foo!');
=head1 METHODS
L<Mojo::Script::Cgi> inherits all methods from L<Mojo::Script> and implements
Expand Down
49 changes: 40 additions & 9 deletions lib/Mojo/Script/Daemon.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,49 @@ use base 'Mojo::Script';

use Mojo::Server::Daemon;

use Getopt::Long 'GetOptionsFromArray';

__PACKAGE__->attr('description', default => <<'EOF');
* Start the daemon. *
Takes a port as option, by default 3000 will be used.
daemon
daemon 8080
Start application with HTTP 1.1 backend.
EOF
__PACKAGE__->attr('usage', default => <<"EOF");
usage: $0 daemon [OPTIONS]
These options are available:
--clients <limit> Set maximum number of concurrent clients, defaults
to 1000.
--group <name> Set group name of process.
--help Display this message and exit.
--keepalive <seconds> Set keep-alive timeout, defaults to 15.
--port <port> Set port to start daemon on, defaults to 3000.
--queue <size> Set listen queue size, defaults to SOMAXCONN.
--requests <limit> Set the maximum number of requests per keep-alive
connection, defaults to 100.
--user <name> Set user name of process.
EOF


# This is the worst thing you've ever done.
# You say that so often that it lost its meaning.
sub run {
my ($self, $port) = @_;

# Start server
$port ||= 3000;
my $self = shift;
my $daemon = Mojo::Server::Daemon->new;
$daemon->port($port);

# Options
my @options = @_ ? @_ : @ARGV;
GetOptionsFromArray(
\@options,
'clients=i' => sub { $daemon->max_clients($_[1]) },
'group=s' => sub { $daemon->group($_[1]) },
'help' => sub { $self->help },
'keepalive=i' => sub { $daemon->keep_alive_timeout($_[1]) },
'port=i' => sub { $daemon->port($_[1]) },
'queue=i' => sub { $daemon->listen_queue_size($_[1]) },
'requests=i' => sub { $daemon->max_keep_alive_requests($_[1]) },
'user=s' => sub { $daemon->user($_[1]) },
);

# Run
$daemon->run;

return $self;
Expand Down Expand Up @@ -60,6 +86,11 @@ implements the following new ones.
my $description = $daemon->description;
$daemon = $daemon->description('Foo!');
=head2 C<usage>
my $usage = $daemon->usage;
$daemon = $daemon->usage('Foo!');
=head1 METHODS
L<Mojo::Script::Daemon> inherits all methods from L<Mojo::Script> and
Expand Down
72 changes: 64 additions & 8 deletions lib/Mojo/Script/DaemonPrefork.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,73 @@ use base 'Mojo::Script';

use Mojo::Server::Daemon::Prefork;

use Getopt::Long 'GetOptionsFromArray';

__PACKAGE__->attr('description', default => <<'EOF');
* Start the prefork daemon. *
Takes a port as option, by default 3000 will be used.
daemon_prefork
daemon_prefork 8080
Start application with preforking HTTP 1.1 backend.
EOF
__PACKAGE__->attr('usage', default => <<"EOF");
usage: $0 daemon_prefork [OPTIONS]
These options are available:
--clients <limit> Set maximum number of concurrent clients per child,
defaults to 1.
--daemonize Daemonize process.
--group <name> Set group name for child processes.
--help Display this message and exit.
--idle <seconds> Set time processes have to be idle before being
killed, defaults to 30.
--interval <seconds> Set interval for process maintainance, defaults to
15.
--keepalive <seconds> Set keep-alive timeout, defaults to 15.
--maxspare <number> Set maximum amount of idle children, defaults to 10.
--minspare <number> Set minimum amount of idle children, defaults to 5.
--pid <path> Set path to pid file, defaults to a random
temporary file.
--port <port> Set port to start daemon on, defaults to 3000.
--queue <size> Set listen queue size, defaults to SOMAXCONN.
--requests <number> Set the maximum number of requests per keep-alive
connection, defaults to 100.
--servers <number> Set the maximum number of child processes, defaults
to 100.
--start <number> Set number of children to spawn at startup,
defaults to 5.
--user <name> Set user name for child processes.
EOF

# Dear Mr. President, there are too many states nowadays.
# Please eliminate three.
# P.S. I am not a crackpot.
sub run {
my ($self, $port) = @_;

# Start server
my $self = shift;
my $daemon = Mojo::Server::Daemon::Prefork->new;
$daemon->port($port) if $port;

# Options
my $daemonize;
my @options = @_ ? @_ : @ARGV;
GetOptionsFromArray(
\@options,
'clients=i' => sub { $daemon->max_clients($_[1]) },
'daemonize' => \$daemonize,
'group=s' => sub { $daemon->group($_[1]) },
'help' => sub { $self->help },
'idle=i' => sub { $daemon->idle_timeout($_[1]) },
'interval=i' => sub { $daemon->cleanup_interval($_[1]) },
'keepalive=i' => sub { $daemon->keep_alive_timeout($_[1]) },
'maxspare=i' => sub { $daemon->max_spare_servers($_[1]) },
'minspare=i' => sub { $daemon->min_spare_servers($_[1]) },
'pid=s' => sub { $daemon->pid_file($_[1]) },
'port=i' => sub { $daemon->port($_[1]) },
'queue=i' => sub { $daemon->listen_queue_size($_[1]) },
'requests=i' => sub { $daemon->max_keep_alive_requests($_[1]) },
'servers=i' => sub { $daemon->max_servers($_[1]) },
'user=s' => sub { $daemon->user($_[1]) }
);

# Daemonize
$daemon->daemonize if $daemonize;

# Run
$daemon->run;

return $self;
Expand Down Expand Up @@ -59,6 +110,11 @@ and implements the following new ones.
my $description = $daemon->description;
$daemon = $daemon->description('Foo!');
=head2 C<usage>
my $usage = $daemon->usage;
$daemon = $daemon->usage('Foo!');
=head1 METHODS
L<Mojo::Script::Daemon::Prefork> inherits all methods from L<Mojo::Script>
Expand Down
12 changes: 9 additions & 3 deletions lib/Mojo/Script/Fastcgi.pm
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ use base 'Mojo::Script';
use Mojo::Server::FastCGI;

__PACKAGE__->attr('description', default => <<'EOF');
* Start the fastcgi script. *
Takes no options.
fastcgi
Start application with FastCGI backend.
EOF
__PACKAGE__->attr('usage', default => <<"EOF");
usage: $0 fastcgi
EOF

# Oh boy! Sleep! That's when I'm a Viking!
Expand Down Expand Up @@ -49,6 +50,11 @@ implements the following new ones.
my $description = $fastcgi->description;
$fastcgi = $fastcgi->description('Foo!');
=head2 C<usage>
my $usage = $fastcgi->usage;
$fastcgi = $fastcgi->usage('Foo!');
=head1 METHODS
L<Mojo::Script::Fastcgi> inherits all methods from L<Mojo::Script> and
Expand Down
30 changes: 21 additions & 9 deletions lib/Mojo/Script/Generate.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,22 @@ use warnings;
use base 'Mojo::Scripts';

__PACKAGE__->attr('description', default => <<'EOF');
* Generate files and directories from templates. *
Takes a generator script as option, by default it will list generators.
generate
Generate files and directories from templates.
EOF
__PACKAGE__->attr('message', default => <<'EOF');
Below you will find a list of available generators with descriptions.
__PACKAGE__->attr('hint', default => <<"EOF");
See '$0 generate help GENERATOR' for more information on a specific generator.
EOF
__PACKAGE__->attr('message', default => <<"EOF");
usage: $0 generate GENERATOR [OPTIONS]
These generators are currently available:
EOF
__PACKAGE__->attr('namespaces',
default => sub { ['Mojo::Script::Generate'] });
__PACKAGE__->attr('usage', default => <<"EOF");
usage: $0 generate GENERATOR [OPTIONS]
EOF
__PACKAGE__->attr('namespace', default => 'Mojo::Script::Generate');

# If The Flintstones has taught us anything,
# it's that pelicans can be used to mix cement.
Expand Down Expand Up @@ -49,15 +56,20 @@ implements the following new ones.
my $description = $generator->description;
$generator = $generator->description('Foo!');
=head2 C<hint>
my $hint = $generator->hint;
$generator = $generator->hint('Foo!');
=head2 C<message>
my $message = $generator->message;
$generator = $generator->message('Bar!');
=head2 C<namespace>
=head2 C<namespaces>
my $namespace = $generator->namespace;
$generator = $generator->namespace('Mojo::Script::Generate');
my $namespaces = $generator->namespaces;
$generator = $generator->namespaces(['Mojo::Script::Generate']);
=head1 METHODS
Expand Down
12 changes: 9 additions & 3 deletions lib/Mojo/Script/Generate/App.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ use warnings;
use base 'Mojo::Script';

__PACKAGE__->attr('description', default => <<'EOF');
* Generate application directory structure. *
Takes a name as option, by default MyMojoApp will be used.
generate app TestApp
Generate application directory structure.
EOF
__PACKAGE__->attr('usage', default => <<"EOF");
usage: $0 generate app [NAME]
EOF

# Okay folks, show's over. Nothing to see here, show's... Oh my god!
Expand Down Expand Up @@ -64,6 +65,11 @@ and implements the following new ones.
my $description = $app->description;
$app = $app->description('Foo!');
=head2 C<usage>
my $usage = $app->usage;
$app = $app->usage('Foo!');
=head1 METHODS
L<Mojo::Script::Generate::App> inherits all methods from L<Mojo::Script> and
Expand Down
Loading

0 comments on commit 0112933

Please sign in to comment.