Skip to content

Commit

Permalink
added experimental config method to Mojo
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 6, 2012
1 parent 0c714a2 commit 1ac4555
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 48 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -6,6 +6,7 @@ This file documents the revision history for Perl extension Mojolicious.
- Deprecated Mojo::Server::Daemon->prepare_ioloop in favor of
Mojo::Server::Daemon->start.
- Deprecated Mojo::Headers->x_forwarded_for.
- Added EXPERIMENTAL config method to Mojo.
- Added EXPERIMENTAL ca attribute to Mojo::UserAgent.
- Added EXPERIMENTAL drain event to Mojo::Content.
- Added EXPERIMENTAL drain event to Mojo::Transaction::WebSocket.
Expand Down
31 changes: 31 additions & 0 deletions lib/Mojo.pm
Expand Up @@ -38,6 +38,23 @@ sub new {

sub build_tx { Mojo::Transaction::HTTP->new }

sub config {
my $self = shift;

# Hash
$self->{config} ||= {};
return $self->{config} unless @_;

# Get
return $self->{config}->{$_[0]} unless @_ > 1 || ref $_[0];

# Set
my $values = ref $_[0] ? $_[0] : {@_};
$self->{config} = {%{$self->{config}}, %$values};

return $self;
}

# "D’oh."
sub handler { croak 'Method "handler" not implemented in subclass' }

Expand Down Expand Up @@ -130,6 +147,20 @@ directory.
Transaction builder, defaults to building a L<Mojo::Transaction::HTTP>
object.
=head2 C<config>
my $config = $app->config;
my $foo = $app->config('foo');
$app = $app->config({foo => 'bar'});
$app = $app->config(foo => 'bar');
Application configuration. Note that this method is EXPERIMENTAL and might
change without warning!
$app->config->{foo} = 'bar';
my $foo = $app->config->{foo};
delete $app->config->{foo};
=head2 C<handler>
$tx = $app->handler($tx);
Expand Down
8 changes: 4 additions & 4 deletions lib/Mojo/Server/Hypnotoad.pm
Expand Up @@ -126,8 +126,8 @@ sub run {
sub _config {
my ($self, $app) = @_;

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

# DEPRECATED in Leaf Fluttering In Wind!
my $file = $ENV{HYPNOTOAD_CONFIG};
Expand Down Expand Up @@ -506,8 +506,8 @@ Stop worker gracefully.
=head1 CONFIGURATION
Since the normal L<Mojolicious> configuration system is reused, you just add
a new section to your L<Mojolicious::Plugin::Config> or
Since the normal application configuration system is reused, you usually just
add a new section to your L<Mojolicious::Plugin::Config> or
L<Mojolicious::Config::JSONConfig> configuration files.
# myapp.conf
Expand Down
14 changes: 12 additions & 2 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -71,13 +71,23 @@ specifically for production environments out of the box.
$ hypnotoad script/myapp
Server available at http://127.0.0.1:8080.

Since the normal L<Mojolicious> configuration system is reused, you just add
a new section to your L<Mojolicious::Plugin::Config> or
Since the normal application configuration system is reused, you usually just
add a new section to your L<Mojolicious::Plugin::Config> or
L<Mojolicious::Config::JSONConfig> configuration files.

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

Or change the application configuration directly.

use Mojolicious::Lite;

app->config(hypnotoad => {listen => ['http://*:3000']});

get '/' => {text => 'ALL GLORY TO THE HYPNOTOAD!'};

app->start;

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
system libraries at runtime without ever stopping the server or losing a
Expand Down
38 changes: 7 additions & 31 deletions lib/Mojolicious/Plugin/Config.pm
Expand Up @@ -84,14 +84,11 @@ sub register {
$config = {%$config, %{$self->load($mode, $conf, $app)}}
if defined $mode && -e $mode;
$config = {%{$conf->{default}}, %$config} if $conf->{default};
my $current = $app->config;
%$current = (%$current, %$config);
$app->defaults(config => $current);

# Add "config" helper
$app->helper(config => sub { @_ > 1 ? $config->{$_[1]} : $config });

# Add default stash value
$app->defaults(($conf->{stash_key} || 'config') => $config);

return $config;
return $current;
}

1;
Expand All @@ -115,14 +112,11 @@ Mojolicious::Plugin::Config - Perl-ish configuration plugin
# Mojolicious::Lite
my $config = plugin 'Config';
# Reads myapp.conf by default and puts the parsed version into the stash
my $config = $self->stash('config');
# Reads "myapp.conf" by default
my $config = app->config;
# Everything can be customized with options
my $config = plugin Config => {
file => '/etc/myapp.stuff',
stash_key => 'conf'
};
my $config = plugin Config => {file => '/etc/myapp.stuff'};
=head1 DESCRIPTION
Expand Down Expand Up @@ -158,24 +152,6 @@ File extension of configuration file, defaults to C<conf>.
Configuration file, defaults to the value of the C<MOJO_CONFIG> environment
variable or C<myapp.conf> in the application home directory.
=head2 C<stash_key>
# Mojolicious::Lite
plugin Config => {stash_key => 'conf'};
Configuration stash key.
=head1 HELPERS
L<Mojolicious::Plugin::Config> implements the following helpers.
=head2 C<config>
%= config 'something'
%= config->{something}
Access config values.
=head1 METHODS
L<Mojolicious::Plugin::Config> inherits all methods from
Expand Down
9 changes: 9 additions & 0 deletions lib/Mojolicious/Plugin/DefaultHelpers.pm
Expand Up @@ -26,6 +26,9 @@ sub register {
);
}

# Add "config" helper
$app->helper(config => sub { shift->app->config(@_) });

# Add "content" helper
$app->helper(content => sub { shift->render_content(@_) });

Expand Down Expand Up @@ -139,6 +142,12 @@ L<Mojolicious::Plugin::DefaultHelpers> implements the following helpers.
Alias for L<Mojolicious::Controller/"app">.
=head2 C<config>
%= config 'something'
Alias for L<Mojo/"config">.
=head2 C<content>
%= content
Expand Down
14 changes: 3 additions & 11 deletions lib/Mojolicious/Plugin/JSONConfig.pm
Expand Up @@ -70,14 +70,11 @@ Mojolicious::Plugin::JSONConfig - JSON configuration plugin
# Mojolicious::Lite
my $config = plugin 'JSONConfig';
# Reads myapp.json by default and puts the parsed version into the stash
my $config = $self->stash('config');
# Reads "myapp.json" by default
my $config = app->config;
# Everything can be customized with options
my $config = plugin JSONConfig => {
file => '/etc/myapp.conf',
stash_key => 'conf'
};
my $config = plugin JSONConfig => {file => '/etc/myapp.conf'};
=head1 DESCRIPTION
Expand All @@ -98,11 +95,6 @@ L<Mojolicious::Plugin::Config> and supports the following new ones.
Template options.
=head1 HELPERS
L<Mojolicious::Plugin::JSONConfig> inherits all helpers from
L<Mojolicious::Plugin::Config>.
=head1 METHODS
L<Mojolicious::Plugin::JSONConfig> inherits all methods from
Expand Down

0 comments on commit 1ac4555

Please sign in to comment.