[英]Scala: How to load environment variables into Application.conf using scala?
I have a ConfigLoader which I'm pretty sure works fine.我有一个 ConfigLoader,我很确定它可以正常工作。 I suspect I am not using application.conf file correctly or perhaps my directory containing the prd.env and dev.env is not in the correct place.我怀疑我没有正确使用 application.conf 文件,或者我的包含 prd.env 和 dev.env 的目录不在正确的位置。
What I expect to happen:我期望发生的事情:
I when I enter sbt run
the ConfigLoader reads the application.conf file, sees there are variables within that file.我在输入sbt run
ConfigLoader 读取 application.conf 文件,看到该文件中有变量。 Then checks if prd.env or dev.env depending on which environment it is in and then finally loads the variables.然后根据它所在的环境检查prd.env还是dev.env,然后最后加载变量。 I want this so I can have a different database for dev and prd.我想要这个,所以我可以为 dev 和 prd 使用不同的数据库。
I really appreciate all the help but please try and detail your answers because I am really stuck and short answers often assume I know more than I do.我非常感谢所有帮助,但请尝试详细说明您的答案,因为我真的很困惑,而且简短的答案通常假设我比我知道的更多。 Thanks:)谢谢:)
Tech stack incase relevant:相关的技术堆栈:
application.conf file below: application.conf 文件如下:
appone {
environment = ${ENV}
server-config {
url = ${?BASE_URL}
host = "0.0.0.0"
port = 8080
}
db-config {
driver = "org.postgresql.Driver"
url = ${?DATABASE_URL}
user = ${?APPONE_POSTGRES_USER}
password = ${?PASSWORD}
connection-threads = 4
}
}
Meta/dev.env file (I also have a Meta/prd.env but have shown it here due to contents) Meta/dev.env 文件(我也有一个 Meta/prd.env 但由于内容而在此处显示)
ENV=dev
BASE_URL=http://localhost:8080/
DATABASE_URL=jdbc:postgresql://localhost:5400/bookswapdb
APPONE_POSTGRES_USER=su
PASSWORD=password
LoadConfig file below:加载配置文件如下:
package com.fullstackryan.appone.config
import cats.ApplicativeError
import cats.implicits._
import pureconfig.error.ConfigReaderException
import pureconfig.{ConfigReader, ConfigSource, Derivation}
import shapeless.the
trait LoadConfig[F[_], TConfig] {
def load: F[TConfig]
}
object LoadConfig {
def load[F[_], TConfig](implicit loadConfig: LoadConfig[F, TConfig]): F[TConfig] =
the[LoadConfig[F, TConfig]].load
def apply[F[_], TConfig](
implicit reader: Derivation[ConfigReader[TConfig]], ae: ApplicativeError[F, Throwable]
): LoadConfig[F, TConfig] =
new LoadConfig[F, TConfig] {
def load: F[TConfig] = ApplicativeError[F, Throwable].fromEither {
ConfigSource.default
.at("appone")
.load[TConfig]
.leftMap(ConfigReaderException(_))
}
}
}
error错误
pureconfig.error.ConfigReaderException: Cannot convert configuration to a scala.runtime.Nothing$. Failures are:
at 'appone.db-config':
- (application.conf @ jar:file:/Users/ryanmcavoy/fullStackRyan/appone/target/bg-jobs/sbt_3cc4b1f5/job-11/target/419ddc2c/5befcb57/appone_2.13-0.0.1-SNAPSHOT.jar!/application.conf: 10) Key not found: 'url'.
- (application.conf @ jar:file:/Users/ryanmcavoy/fullStackRyan/appone/target/bg-jobs/sbt_3cc4b1f5/job-11/target/419ddc2c/5befcb57/appone_2.13-0.0.1-SNAPSHOT.jar!/application.conf: 10) Key not found: 'username'.
- (application.conf @ jar:file:/Users/ryanmcavoy/fullStackRyan/appone/target/bg-jobs/sbt_3cc4b1f5/job-11/target/419ddc2c/5befcb57/appone_2.13-0.0.1-SNAPSHOT.jar!/application.conf: 10) Key not found: 'pool-size'.
at com.fullstackryan.appone.config.LoadConfig$$anon$1.$anonfun$load$1(LoadConfig.scala:25)
at cats.syntax.EitherOps$.leftMap$extension(either.scala:172)
at com.fullstackryan.appone.config.LoadConfig$$anon$1.load(LoadConfig.scala:25)
at com.fullstackryan.appone.server.ApponeServer$.$anonfun$stream$1(ApponeServer.scala:32)
at com.fullstackryan.appone.server.ApponeServer$.$anonfun$stream$1$adapted(ApponeServer.scala:31)
at fs2.Stream$.$anonfun$flatMap$1(Stream.scala:1188)
at fs2.internal.FreeC$.go$2(Algebra.scala:609)
at fs2.internal.FreeC$.$anonfun$flatMapOutput$1(Algebra.scala:616)
at fs2.internal.FreeC$$anon$1.cont(Algebra.scala:53)
at fs2.internal.FreeC$ViewL$$anon$9$$anon$10.cont(Algebra.scala:242)
at fs2.internal.FreeC$ViewL$.mk(Algebra.scala:231)
at fs2.internal.FreeC$ViewL$.apply(Algebra.scala:220)
at fs2.internal.FreeC.viewL(Algebra.scala:106)
at fs2.internal.FreeC$.go$1(Algebra.scala:414)
at fs2.internal.FreeC$.$anonfun$compile$8(Algebra.scala:464)
at fs2.internal.FreeC$.$anonfun$compile$1(Algebra.scala:430)
addSbtPlugin("io.github.davidgregory084" % "sbt-tpolecat" % "0.1.14")
addSbtPlugin("io.spray" % "sbt-revolver" % "0.9.1")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.10")
addSbtPlugin("au.com.onegeek" % "sbt-dotenv" % "2.1.204")
// deploy heroku
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.7.6")
addSbtPlugin("com.heroku" % "sbt-heroku" % "2.1.0")
See where the error says application.conf: Key not found: 'username'
?查看错误在哪里显示application.conf: Key not found: 'username'
? That means it's looking for some username
in that application.conf
file and not finding it.这意味着它正在该application.conf
文件中寻找一些username
,但没有找到它。 If you look at your application.conf
file yourself, you can see that indeed there is no setting in there called username
.如果您自己查看application.conf
文件,您会发现其中确实没有名为username
的设置。 There is one called user
, maybe that's what you had intended?有一个叫做user
,也许这就是你的意图?
We also see Key not found: 'pool-size'
, looks like there's no setting for that either.我们还看到Key not found: 'pool-size'
,看起来也没有设置。
Finally, it says Key not found: 'url'
.最后,它说Key not found: 'url'
。 There are two url
keys in your application.conf
, which means that the code isn't looking in the right part of the config file.您的application.conf
中有两个url
键,这意味着代码不在配置文件的正确部分。 Maybe it's looking for appone.url
when you want it to be looking for appone.db-config.url
.当您希望它正在寻找appone.url
appone.db-config.url
。
When running via sbt run
, the environment is set by the shell in which you're running sbt.通过sbt run
时,环境由 shell 设置,您在其中运行 sbt。 Defining a dev.env
file by itself does nothing.自己定义一个dev.env
文件什么都不做。
The mechanism for setting the environment in which sbt is running will vary depending on your shell.设置运行 sbt 的环境的机制将根据您的 shell 而有所不同。
For example if bash is your shell (this is worth trying in other Bourne-compatible shells), prefixing the environment variables with export
, eg:例如,如果 bash 是您的 shell (这值得在其他 Bourne 兼容的 shell 中尝试),在环境变量前面加上export
,例如:
export ENV=dev
Then you incorporate the environment variables you've exported from dev.env
into your bash environment with然后,您将从dev.env
导出的环境变量合并到您的 bash 环境中
source meta/dev.env
Those environment variables will then be set and thus incorporated into your config for the duration of your shell session (ie they'll continue across multiple sbt run
s until you exit the shell).然后将设置这些环境变量,并在您的 shell session 期间将这些环境变量合并到您的配置中(即,它们将继续跨多个sbt run
,直到您退出 shell。 If you change dev.env
, they won't be available until you source meta/dev.env
again.如果您更改dev.env
,在您再次获取source meta/dev.env
之前,它们将不可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.