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?
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},
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With