Play Framework 2.4.x - activator~run不能替代application.conf中的env vars

时间:2015-11-01 23:30:05

标签: playframework typesafe-activator

我在heroku上运行我的Play应用程序。在heroku上,想法是通过env变量设置各种设置。例如AWS S3秘密访问密钥及其ID。这是可以理解的。因此,按照良好实践,我将以下设置放入conf / application.conf:

# AWS.
# ~~~~~
aws.accessKeyID = ${?AWS_ACCESS_KEY_ID}
aws.secretAccessKey = ${?AWS_SECRET_ACCESS_KEY}

好的,一切都很好。现在每当我在PROD或DEV模式下运行时,我都可以改变这些并且不用担心硬编码。问题是,当我运行activator~运行以在本地开发我的应用程序时,这些设置没有被读取和设置。它就像激活器〜运行忽略完全替换env变量的选项。在加载索引页面时,我按以下方式阅读并打印它们:

import play.api.Play.current
...
val v1 = current.configuration.getString("aws.accessKeyID")
val v2 = current.configuration.getString("aws.secretAccessKey")
println("v1=" + v1 + ", v2=" + v2)

让我更详细地说明发生了什么:

floyd@floyd-VirtualBox:~/myproj$ echo $AWS_ACCESS_KEY_ID
access-key-id
floyd@floyd-VirtualBox:~/myproj$ echo $AWS_SECRET_ACCESS_KEY
secret-access-key
floyd@floyd-VirtualBox:~/myproj$ activator ~run
[info] Loading project definition from /home/floyd/myproj/project
[info] Set current project to myproj (in build file:/home/floyd/myproj/)

--- (Running the application, auto-reloading is enabled) ---

[info] p.a.l.c.ActorSystemProvider - Starting application default Akka system: application
[info] p.c.s.NettyServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000

(Server started, use Ctrl+D to stop and go back to the console...)

[info] Compiling 1 Scala source to /home/floyd/myproj/target/scala-2.11/classes...
[success] Compiled in 15s
[info] - play.api.libs.concurrent.ActorSystemProvider - Starting application default Akka system: application
[info] - application - ReactiveMongoApi starting...
[info] - application - ReactiveMongoApi successfully started with DB 'test'! Servers:
        [localhost:27017]
[info] - play.api.Play - Application started (Dev)
v1=None, v2=None

两种情况都打印出来,我希望看到存储在env变量中的值。

先感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

我想出的一个可能的解决方案(或者更确切地说是解决方法......),我已经创建了一个包含内容的dev-env文件:

flex

和包含内容的运行开发可执行脚本:

-DVAR1=a \
-DVAR2=b \
-DVAR3=c

这使得我可以使用单独的开发设置,而不需要对conf / application.conf中的任何内容进行硬编码。

不幸的是,我不能让激活器〜运行来读取env变量而不明确地将它们传递给激活器(在引擎盖下这些传递给jvm)。

答案 1 :(得分:1)

我认为答案可能在于您如何设置您的Environment变量。模拟您的体验

使用

的application.conf

foo.bar=${?FOO}

然后

$ FOO=BAR
$ activator ~run
[info] Loading project definition from /Users/pw/dev/play-project/project
[info] Set current project to play-project (in build file:/Users/pw/dev/play-project/)

--- (Running the application, auto-reloading is enabled) ---

[info] p.c.s.NettyServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000

(Server started, use Ctrl+D to stop and go back to the console...)

[success] Compiled in 687ms
[info] - application - Application is started!!! ****
[info] - play.api.Play - Application started (Dev)
v1=None

至关重要的是,如果您导出环境变量,如下面的代码段所示,则实现了预期的行为

$ unset FOO
$ echo $FOO

$ export FOO=ASDF
$ activator ~run
[info] Loading project definition from /Users/pw/dev/play-project/project
[info] Set current project to play-project (in build file:/Users/pw/dev/play-project/)

--- (Running the application, auto-reloading is enabled) ---

[info] p.c.s.NettyServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000

(Server started, use Ctrl+D to stop and go back to the console...)

[success] Compiled in 1s
[info] - application - Application is started!!! ****
[info] - play.api.Play - Application started (Dev)
v1=Some(ASDF)