gpt4 book ai didi

perl - 从环境变量配置 Dancer?

转载 作者:IT老高 更新时间:2023-10-28 21:38:09 24 4
gpt4 key购买 nike

我是 Dancer 的新手,但我正在尝试将其配置为在 Docker 容器中工作。因此,我需要从环境中获取我的数据库设置。

就我而言,我有来自 Docker 的 DB_PORT_3306_TCP_ADDRDB_PORT_3306_TCP_PORT。不幸的是,在我可以更改数据库以使用这些变量之前,Dancer::Plugin::Database 模块出错了。

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

所以问题是,有没有一种从环境变量而不是通过静态 YAML 来配置 Dancer 的好方法?

最佳答案

Runtime ConfigurationDancer::Plugin::Database 文档中:

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

您正在添加一个 ->,这会导致错误。以下应该有效:

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

关于perl - 从环境变量配置 Dancer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26766572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com