从环境variablesconfiguration舞者?

我是新来的舞者,但我想configuration它在Docker容器中工作。 因此,我需要从环境中获取我的数据库设置。

在我的情况下,我有来自Docker的DB_PORT_3306_TCP_ADDRDB_PORT_3306_TCP_PORT 。 不幸的是, Dancer::Plugin::Database模块在更改数据库以使用这些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}, }); } 

所以问题是,是否有一个好的方法来configuration舞者从环境variables,而不是通过静态YAML?

请参阅Dancer::Plugin::Database文档中的运行时configuration :

如果需要,可以将hashref传递给database()关键字以提供configuration详细信息以覆盖configuration文件中的任何configuration文件,例如:

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

您正在添加-> ,导致错误。 以下应该工作:

 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}, }); } 

lib / myapp.pm的开头 ,在模块加载之后,添加:

 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}; 

并在config.yml中保存静态configuration(Driver,port)

Interesting Posts