2

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:

enter image description here

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}

Levi Ramsey
  • 18,884
  • 1
  • 16
  • 30
Ry2254
  • 859
  • 1
  • 10
  • 19
  • Does this answer your question? [specific config by environment in Scala](https://stackoverflow.com/questions/21607745/specific-config-by-environment-in-scala) – Tomer Shetah Jan 19 '21 at 15:13
  • Or this: [How to Add Environment Profile Config to SBT](https://stackoverflow.com/questions/17193795/how-to-add-environment-profile-config-to-sbt) – Tomer Shetah Jan 19 '21 at 15:36
  • I think those solutions would work well if not using heroku as heroku rotates the variables so you can't have it statically anywhere in code or environment variables. – Ry2254 Jan 20 '21 at 12:16

2 Answers2

0

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 e.g., 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).

Levi Ramsey
  • 18,884
  • 1
  • 16
  • 30
0
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

md samual
  • 305
  • 3
  • 15