Configure Dancer from environment variables?

I am new to Dancer, but I am trying to configure it to work in a Docker container. As a result, I need to select my database settings from the environment.

In my case, I have DB_PORT_3306_TCP_ADDR , and DB_PORT_3306_TCP_PORT is Docker. Unfortunately, the Dancer::Plugin::Database module is a bug before I can modify the database to use these 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 and not through static YAML?

+6
source share
2 answers

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

You can pass hashref to the database() keyword to provide configuration information to override any configuration files at run time if necessary, for example:

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

You add -> , 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}, }); } 
+5
source

At the beginning of your lib / myapp.pm, after loading the module, add:

 setting('plugins')->{'Database'}->{'host'}='postgres'; setting('plugins')->{'Database'}->{'database'}=$ENV{POSTGRES_DB}; setting('plugins')->{'Database'}->{'username'}=$ENV{POSTGRES_USER}; setting('plugins')->{'Database'}->{'password'}=$ENV{POSTGRES_PASSWORD}; 

and save the static configuration (driver, port) in config.yml

0
source

Source: https://habr.com/ru/post/977789/


All Articles