简体   繁体   中英

How to use Environment Variables and application.conf files in Scala?

I am unsure of how to get environment variables from my dev.env and prod.env into my application.conf (or reference them from the conf file).

I want to deploy my app to heroku and connect it to heroku's database. In the future, I'll want to deploy to AWS in docker container.

Can some please explain how environment variables work in Scala and explain how to refernce the environement variables from application.conf.

I already have a configLoader.

Below is a picture of my file structure and I have also copied code below:

在此处输入图像描述

application.conf file below:

    akka {
      loggers = ["akka.event.slf4j.Slf4jLogger"]
      loglevel = DEBUG
    }

    server-config {
      host = "0.0.0.0"
      port = 8080
      base-url = ${?BASE_URL}
    }

    db-config {
      url = {?JDBC_DATABASE_URL}
      username = "su"
      password = "password"
      pool-size = 10
      driver="org.h2.Driver"
    }

dev.env below:

ENV=dev

BASE_URL=http://localhost:8080

JDBC_DATABASE_URL=jdbc:postgresql://localhost:5400/bookswapdb

prd.env below:

ENV=${_ENVIRONMENT}

BASE_URL=https://git.heroku.com/appone2021.git

JDBC_DATABASE_URL=${URL_WITH_CREDS_REDACTED}

Assuming that you're using ConfigFactory.load (or having Akka load the configuration for you), the environment variables will "just work", assuming that the JVM process is actually running with the environment variables set.

To get the value of eg, db-config.url , you should just be able to do

val config = ConfigFactory.load()  // or if in an actor, context.system.config
val dbUrl = config.getString("db-config.url")

On Heroku, you would just use config vars . For AWS deployment, the particulars of how you inject environment variables into the container will depend on how you're running the container. Note that setting the environment variables may be difficult or impossible in IntelliJ (since if IntelliJ runs the app inside its own JVM, it will only have the environment variables set when IntelliJ started up).

db={
  default.driver = "org.postgresql.Driver"
  default.url="vendor://username:password@host:port/db"
  db.url=${DATABASE_URL}
  db.profile="jdbc.PostgresProfile"
}

Play will automatically convert this into a JDBC URL for you if you are using one of the built in database connection pools. But other database libraries and frameworks, such as Slick or Hibernate, may not support this format natively. If that's the case, you may try using the dynamic JDBC_DATABASE_URL in place of DATABASE_URL in the configuration like this:

db.default.url=${?JDBC_DATABASE_URL}

to get value from application.conf file:

import com.typesafe.config.Config 
val str1=config.getString("your_string")

reference https://www.playframework.com/documentation/2.8.x/ProductionHeroku

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM