首页 > 解决方案 > 缺少环境变量

问题描述

我正在使用库https://cir.is/docs/overview来加载我的环境变量,如下所示:

object EnvironmentLoader {

  val db: ConfigValue[Postgres] =
    (
      env("POSTGRES_URL").as[NonEmptyString],
      env("POSTGRES_USER").as[NonEmptyString],
      env("POSTGRES_PW").as[DatabasePassword].secret
    ).parMapN(Postgres)

}

我在shell中设置了变量:

POSTGRES_URL=jdbc:postgresql://localhost:5432/users
POSTGRES_USER=dbuser
POSTGRES_PW=password

当我运行sbt test它显示:

[error] - GET /user test with invalid UUID
[error]   ConfigException$$anon$1: configuration loading failed with the following errors.
[error] 
[error]     - Missing environment variable POSTGRES_URL.
[error]     - Missing environment variable POSTGRES_USER.
[error]     - Missing environment variable POSTGRES_PW.
[error] 
[error]   ConfigException.scala:57      ciris.ConfigException$#apply
[error]   ConfigError.scala:114         ciris.ConfigError#throwable
[error]   ConfigValue.scala:176         ciris.ConfigValue#$anonfun$load$1
[error]   Resource.scala:118            cats.effect.Resource#$anonfun$fold$1
[error]   <snipped>                     cats.effect.internals.<...>
[error]   JFunction0$mcV$sp.scala:18    scala.runtime.java8.JFunction0$mcV$sp#apply
[error]   BlockContext.scala:94         scala.concurrent.BlockContext$#withBlockContext
[error]   <snipped>                     cats.effect.internals.<...>
[error]   <snipped>                     java.util.concurrent.<...>

如何设置环境变量sbt

标签: scalascala-cats

解决方案


推荐阅读