Skip to content

Commit

Permalink
deprecated Hypnotoad configuration files in favor of more powerful ap…
Browse files Browse the repository at this point in the history
…plication configuration files
  • Loading branch information
kraih committed Feb 6, 2012
1 parent d767348 commit 0c714a2
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 136 deletions.
6 changes: 4 additions & 2 deletions Changes
@@ -1,6 +1,8 @@
This file documents the revision history for Perl extension Mojolicious.

2.47 2012-02-05 00:00:00
2.47 2012-02-06 00:00:00
- Deprecated Hypnotoad configuration files in favor of more powerful
application configuration files.
- Deprecated Mojo::Server::Daemon->prepare_ioloop in favor of
Mojo::Server::Daemon->start.
- Deprecated Mojo::Headers->x_forwarded_for.
Expand Down Expand Up @@ -1358,7 +1360,7 @@ This file documents the revision history for Perl extension Mojolicious.
- Added support for selector groups to Mojo::DOM.
- Added more attribute selectors, pseudo classes and combinators to
Mojo::DOM.
- Added support for mode specific config files to
- Added support for mode specific configuration files to
Mojolicious::Plugin::JsonConfig. (marcus)
- Added reserved route name current.
- Simplified transaction pausing by replacing it with an automatism.
Expand Down
32 changes: 17 additions & 15 deletions lib/Mojo/Server/Hypnotoad.pm
Expand Up @@ -53,7 +53,7 @@ sub run {
# Application
$ENV{HYPNOTOAD_APP} ||= abs_path $app;

# Config
# DEPRECATED in Leaf Fluttering In Wind!
$ENV{HYPNOTOAD_CONFIG} ||= abs_path $config;

# This is a production server
Expand All @@ -66,13 +66,10 @@ sub run {
# Clean start
exec $ENV{HYPNOTOAD_EXE} unless $ENV{HYPNOTOAD_REV}++;

# Preload application
# Preload application and configure server
my $daemon = $self->{daemon} = Mojo::Server::Daemon->new;
warn "APPLICATION $ENV{HYPNOTOAD_APP}\n" if DEBUG;
$daemon->load_app($ENV{HYPNOTOAD_APP});

# Load configuration
$self->_config;
$self->_config($daemon->load_app($ENV{HYPNOTOAD_APP}));

# Testing
_exit('Everything looks good!') if $ENV{HYPNOTOAD_TEST};
Expand Down Expand Up @@ -127,23 +124,26 @@ sub run {
}

sub _config {
my $self = shift;
my ($self, $app) = @_;

# Load configuration from application
my $c = eval { $app->config('hypnotoad') } || {};

# Load config file
# DEPRECATED in Leaf Fluttering In Wind!
my $file = $ENV{HYPNOTOAD_CONFIG};
warn "CONFIG $file\n" if DEBUG;
my $c = {};
if (-r $file) {
warn "Hypnotoad config files are DEPRECATED!\n";
unless ($c = do $file) {
die qq/Can't load config file "$file": $@/ if $@;
die qq/Can't load config file "$file": $!/ unless defined $c;
die qq/Config file "$file" did not return a hashref.\n/
unless ref $c eq 'HASH';
}
}
$self->{config} = $c;

# Hypnotoad settings
$self->{config} = $c;
$c->{graceful_timeout} ||= 30;
$c->{heartbeat_interval} ||= 5;
$c->{heartbeat_timeout} ||= 10;
Expand Down Expand Up @@ -417,7 +417,7 @@ Mojo::Server::Hypnotoad - ALL GLORY TO THE HYPNOTOAD!
use Mojo::Server::Hypnotoad;
my $toad = Mojo::Server::Hypnotoad->new;
$toad->run('./myapp.pl', './hypnotoad.conf');
$toad->run('./myapp.pl');
=head1 DESCRIPTION
Expand Down Expand Up @@ -506,10 +506,12 @@ Stop worker gracefully.
=head1 CONFIGURATION
C<Hypnotoad> configuration files are normal Perl scripts returning a hash.
Since the normal L<Mojolicious> configuration system is reused, you just add
a new section to your L<Mojolicious::Plugin::Config> or
L<Mojolicious::Config::JSONConfig> configuration files.
# hypnotoad.conf
{listen => ['http://*:3000', 'http://*:4000'], workers => 10};
# myapp.conf
{hypnotoad => {listen => ['http://*:3000'], workers => 10}};
The following parameters are currently available:
Expand Down Expand Up @@ -643,7 +645,7 @@ implements the following new ones.
=head2 C<run>
$toad->run('script/myapp', 'hypnotoad.conf');
$toad->run('script/myapp');
Run server.
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Template.pm
Expand Up @@ -457,8 +457,8 @@ Mojo::Template - Perl-ish templates!
L<Mojo::Template> is a minimalistic and very Perl-ish template engine,
designed specifically for all those small tasks that come up during big
projects. Like preprocessing a config file, generating text from heredocs and
stuff like that.
projects. Like preprocessing a configuration file, generating text from
heredocs and stuff like that.
<% Perl code %>
<%= Perl expression, replaced with result %>
Expand Down
4 changes: 1 addition & 3 deletions lib/Mojolicious.pm
Expand Up @@ -122,9 +122,7 @@ sub defaults {

# Set
my $values = ref $_[0] ? $_[0] : {@_};
for my $key (keys %$values) {
$self->{defaults}->{$key} = $values->{$key};
}
$self->{defaults} = {%{$self->{defaults}}, %$values};

return $self;
}
Expand Down
76 changes: 0 additions & 76 deletions lib/Mojolicious/Command/generate/hypnotoad.pm

This file was deleted.

6 changes: 0 additions & 6 deletions lib/Mojolicious/Commands.pm
Expand Up @@ -109,12 +109,6 @@ L<Mojolicious> application.
Generate C<.gitignore> file.
=head2 C<generate hypnotoad>
$ mojo generate hypnotoad
Generate C<hypnotoad.conf> file.
=head2 C<generate lite_app>
$ mojo generate lite_app
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Guides/Cheatsheet.pod
Expand Up @@ -164,7 +164,7 @@ I/O operations but will also use more memory, defaults to C<131072>.

=head2 C<MOJO_CONFIG>

Config file to be used by L<Mojolicious::Plugin::Config> and
Configuration file to be used by L<Mojolicious::Plugin::Config> and
L<Mojolicious::Plugin::JSONConfig>, quite useful for testing.

MOJO_CONFIG=myapp.conf
Expand Down
12 changes: 7 additions & 5 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -71,10 +71,12 @@ specifically for production environments out of the box.
$ hypnotoad script/myapp
Server available at http://127.0.0.1:8080.

Config files are plain Perl scripts for maximal customizability.
Since the normal L<Mojolicious> configuration system is reused, you just add
a new section to your L<Mojolicious::Plugin::Config> or
L<Mojolicious::Config::JSONConfig> configuration files.

# hypnotoad.conf
{listen => ['http://*:80'], workers => 10};
# myapp.conf
{hypnotoad => {listen => ['http://*:80'], workers => 10}};

But one of its biggest advantages is the support for effortless zero downtime
software upgrades. That means you can upgrade L<Mojolicious>, Perl or even
Expand All @@ -88,8 +90,8 @@ You might also want to enable proxy support if you're using Hypnotoad behind
a reverse proxy. This allows L<Mojolicious> to automatically pick up the
C<X-Forwarded-For>, C<X-Forwarded-Host> and C<X-Forwarded-HTTPS> headers.

# hypnotoad.conf
{proxy => 1};
# myapp.conf
{hypnotoad => {proxy => 1}};

=head2 Nginx

Expand Down
23 changes: 7 additions & 16 deletions lib/Mojolicious/Plugin/Config.pm
Expand Up @@ -75,27 +75,18 @@ sub register {
if (-e $file) { $config = $self->load($file, $conf, $app) }

# Check for default
else {

# All missing
die qq/Config file "$file" missing, maybe you need to create it?\n/
unless $conf->{default};
elsif ($conf->{default}) {
$app->log->debug(qq/Config file "$file" missing, using default config./);
}
else { die qq/Config file "$file" missing, maybe you need to create it?\n/ }

# Merge everything
$config = {%$config, %{$self->load($mode, $conf, $app)}}
if defined $mode && -e $mode;
$config = {%{$conf->{default}}, %$config} if $conf->{default};

# Add "config" helper
$app->helper(
config => sub {
my $self = shift;
return $config unless @_;
$config->{$_[0]};
}
);
$app->helper(config => sub { @_ > 1 ? $config->{$_[1]} : $config });

# Add default stash value
$app->defaults(($conf->{stash_key} || 'config') => $config);
Expand Down Expand Up @@ -137,7 +128,7 @@ Mojolicious::Plugin::Config - Perl-ish configuration plugin
L<Mojolicious::Plugin::Config> is a Perl-ish configuration plugin. The
application object can be accessed via the C<app> helper. You can extend the
normal config file C<myapp.conf> with C<mode> specific ones like
normal configuration file C<myapp.conf> with C<mode> specific ones like
C<myapp.$mode.conf>.
=head1 OPTIONS
Expand All @@ -156,7 +147,7 @@ Default configuration.
# Mojolicious::Lite
plugin Config => {ext => 'stuff'};
File extension of config file, defaults to C<conf>.
File extension of configuration file, defaults to C<conf>.
=head2 C<file>
Expand Down Expand Up @@ -194,7 +185,7 @@ L<Mojolicious::Plugin> and implements the following new ones.
$plugin->load($file, $conf, $app);
Loads config file and passes the content to C<parse>.
Loads configuration file and passes the content to C<parse>.
sub load {
my ($self, $file, $conf, $app) = @_;
Expand All @@ -206,7 +197,7 @@ Loads config file and passes the content to C<parse>.
$plugin->parse($content, $file, $conf, $app);
Parse config file.
Parse configuration file.
sub parse {
my ($self, $content, $file, $conf, $app) = @_;
Expand Down
7 changes: 4 additions & 3 deletions script/hypnotoad
Expand Up @@ -22,7 +22,10 @@ my $toad = Mojo::Server::Hypnotoad->new;
my $config = 'hypnotoad.conf';
my $help;
GetOptions(
'c|config=s' => sub { $config = $_[1] },

# DEPRECATED in Leaf Fluttering In Wind!
'c|config=s' => sub { $config = $_[1] },

'f|foreground' => sub { $ENV{HYPNOTOAD_FOREGROUND} = 1 },
'h|help' => sub { $help = 1 },
's|stop' => sub { $ENV{HYPNOTOAD_STOP} = 1 },
Expand All @@ -39,8 +42,6 @@ usage: $0 [OPTIONS] [APPLICATION]
hypnotoad -f myapp.pl
These options are available:
-c, --config <path> Configuration file, defaults to "hypnotoad.conf" in
the same directory as the application script.
-f, --foreground Keep manager process in foreground.
-h, --help Show this message.
-s, --stop Stop server gracefully.
Expand Down
23 changes: 16 additions & 7 deletions t/mojo/hypnotoad.t
Expand Up @@ -30,23 +30,25 @@ my $dir = File::Temp::tempdir(CLEANUP => 1);
chdir $dir;
my $command = Mojo::Command->new;
my $script = $command->rel_file('myapp.pl');
my $port1 = Mojo::IOLoop->generate_port;
my $port2 = Mojo::IOLoop->generate_port;
$command->write_rel_file('myapp.pl', <<EOF);
use Mojolicious::Lite;
plugin Config => {
default => {
hypnotoad =>
{listen => ['http://*:$port1', 'http://*:$port2'], workers => 1}
}
};
app->log->level('fatal');
get '/hello' => {text => 'Hello Hypnotoad!'};
app->start;
EOF

# Prepare config
my $port1 = Mojo::IOLoop->generate_port;
my $port2 = Mojo::IOLoop->generate_port;
$command->write_rel_file('hypnotoad.conf', <<EOF);
{listen => ['http://*:$port1', 'http://*:$port2'], workers => 1};
EOF

# Start
my $prefix = "$FindBin::Bin/../../script";
open my $start, '-|', $^X, "$prefix/hypnotoad", $script;
Expand Down Expand Up @@ -96,6 +98,13 @@ is $tx->res->body, 'Hello Hypnotoad!', 'right content';
$command->write_rel_file('myapp.pl', <<EOF);
use Mojolicious::Lite;
plugin Config => {
default => {
hypnotoad =>
{listen => ['http://*:$port1', 'http://*:$port2'], workers => 1}
}
};
app->log->level('fatal');
get '/hello' => {text => 'Hello World!'};
Expand Down

0 comments on commit 0c714a2

Please sign in to comment.