Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure Dancer from environment variables?

I'm new to Dancer, but I'm trying to configure it to work within a Docker container. As a result, I need to pick up my database settings from the environment.

In my case, I have DB_PORT_3306_TCP_ADDR, and DB_PORT_3306_TCP_PORT coming from Docker. Unfortunately, the Dancer::Plugin::Database module is erroring before I can change the database to use those variables.

use Dancer ':syntax';
use Dancer::Plugin::Database;

if ($ENV{DB_PORT_3306_TCP}) {## Connected via docker.
    database->({
        driver => 'mysql',
        username => 'username',
        password => 'password',
        host => $ENV{DB_PORT_3306_TCP_ADDR},
        port => $ENV{DB_PORT_3306_TCP_PORT},
        database => $ENV{DB_ENV_MYSQL_DATABASE},
    });
}

So the question is, is there a good way to configure Dancer from environment variables, instead of through static YAML?

like image 224
Jack M. Avatar asked Nov 05 '14 20:11

Jack M.


1 Answers

See Runtime Configuration in the Dancer::Plugin::Database docs:

You can pass a hashref to the database() keyword to provide configuration details to override any in the config file at runtime if desired, for instance:

my $dbh = database({ driver => 'SQLite', database => $filename });

You're adding a ->, which causes an error. The following should work:

use Dancer ':syntax';
use Dancer::Plugin::Database;

if ($ENV{DB_PORT_3306_TCP}) {## Connected via docker.
    database({
        driver => 'mysql',
        username => 'username',
        password => 'password',
        host => $ENV{DB_PORT_3306_TCP_ADDR},
        port => $ENV{DB_PORT_3306_TCP_PORT},
        database => $ENV{DB_ENV_MYSQL_DATABASE},
    });
}
like image 150
ThisSuitIsBlackNot Avatar answered Nov 04 '22 11:11

ThisSuitIsBlackNot