Skip to content

Commit

Permalink
Let the user set the environments to another path
Browse files Browse the repository at this point in the history
By default, Dancer will load the environment configuration from

    path(setting('appdir'), 'environments')

This change let the user to set the environments in another directory. This is
particulary useful when your configuration is managed outside the application
(like a repository managed by Ops).

A new test file is added to test this feature, and a short documentation about
`DANCER_CONFDIR` and `DANCER_ENVDIR` is added.
  • Loading branch information
fcuny committed Mar 29, 2012
1 parent 991a76b commit 7974085
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES
Expand Up @@ -24,6 +24,8 @@
* Added 'strict_config' option to have the config return an object instead
of a hashref. (Ovid)
* GH #708: Added support for query strings in dancer_request (Jacob Rideout)
* It's possible for the user to set the environments directory using a new
environment variable (DANCER_ENVDIR) or using `set envdir => $path`

1.3093 29.02.2012

Expand Down
22 changes: 21 additions & 1 deletion lib/Dancer/Config.pm
Expand Up @@ -144,16 +144,26 @@ sub conffile { path(setting('confdir') || setting('appdir'), 'config.yml') }

sub environment_file {
my $env = setting('environment');
return path(setting('appdir'), 'environments', "$env.yml");
# XXX for compatibility reason, we duplicate the code from `init_envdir` here
# we don't know how if some application don't already do some weird stuff like
# the test in `t/15_plugins/02_config.t`.
my $envdir = setting('envdir') || path(setting('appdir'), 'environments');
return path($envdir, "$env.yml");
}

sub init_confdir {
return setting('confdir') if setting('confdir');
setting confdir => $ENV{DANCER_CONFDIR} || setting('appdir');
}

sub init_envdir {
return setting('envdir') if setting('envdir');
setting envdir => $ENV{DANCER_ENVDIR} || path(setting('appdir'), 'environments');
}

sub load {
init_confdir();
init_envdir();

# look for the conffile
return 1 unless -f conffile;
Expand Down Expand Up @@ -597,6 +607,16 @@ Dancer will honor your C<before_template_render> code, and all default
variables. They will be accessible and interpolated on automatic
served pages.
=head2 DANCER_CONFDIR and DANCER_ENVDIR
It's possible to set the configuration directory and environment directory using this two
environment variables. Setting `DANCER_CONFDIR` will have the same effect as doing
set confdir => '/path/to/confdir'
and setting `DANCER_ENVDIR` will be similar to:
set envdir => '/path/to/environments'
=head1 AUTHOR
Expand Down
46 changes: 46 additions & 0 deletions t/01_config/08_environments.t
@@ -0,0 +1,46 @@
use strict;
use warnings;
use Test::More import => ['!pass'];

plan tests => 5;

use File::Spec;
use Dancer ':syntax';
use lib File::Spec->catdir('t', 'lib');
use TestUtils;

my $app_dir = File::Temp::tempdir(CLEANUP => 1, TMPDIR => 1);
my $env_dir = File::Temp::tempdir(CLEANUP => 1, TMPDIR => 1);

set appdir => $app_dir;
set envdir => $env_dir;

my $conffile = Dancer::Config->conffile;
my $conf = '
port: 4500
startup_info: 0
charset: "UTF8"
logger: file
log: "debug"
';
write_file($conffile => $conf);

ok(Dancer::Config->load, 'Config load works without conffile');

is(setting('log'), 'debug', 'log setting looks good');

my $prod_env = '
log: "warning"
startup_info: 0
foo_prod: 42
';

setting('environment' => 'prod');
write_file(Dancer::Config->environment_file, $prod_env);

my $path = File::Spec->catfile($env_dir, 'prod.yml');
ok -f $path;

ok(Dancer::Config->load, 'load prod environment');
is(setting('log'), 'warning', 'log setting looks good');
File::Temp::cleanup();

0 comments on commit 7974085

Please sign in to comment.