user2767260
user2767260

Reputation: 283

Having trouble getting environment variable into application.conf

I'm using Scredis, a scala Redis library. To instantiate a Redis object I have to pass it a config and the path to the config values. (As far as I can tell there is no other way to give the Redis object its hostname and port)

Basically, I need to have the Redis port and host name stored in the config. Now the issue is that I'm trying to use Heroku to deploy this, and the hostname and port both reside in an environment variable. I've learned that I can access env vars from the application.conf, but since they are stored as one long string there's no way for me to break it up before I pass it to the Redis object. I know how to grab the env var from the env var in code, but seeing as I can't pass it directly I'm at a complete loss for how to do this.

Here is the configuration information for Scredis: https://github.com/Livestream/scredis/wiki/Configuration

Using redis-cloud for java/scala on heroku: https://devcenter.heroku.com/articles/rediscloud#using-redis-from-java

Upvotes: 2

Views: 2289

Answers (3)

Cem Catikkas
Cem Catikkas

Reputation: 7174

Typesafe Config and hence Play Framework makes this extremely easy for you since Typesafe Config also loads environment variables to resolve config values. You basically have two options.

Option 1: Make sure the environment variables have the same name as the configuration key scredis expects. This is useful in cases where you want to change the given config value on startup - for example you have the following application.conf:

{
  foo = bar
}

But at runtime you want the value of foo to be baz. Then you can either pass the new value through environment variable or -D.

$ foo=baz activator run
$ activator run -Dfoo=baz

Option 2: In you application.conf have a placeholder to read the environment variable. Assuming the enviroment variables Heroku passes you are REDIS_HOST and REDIS_PORT, then in your application.conf you can do the following:

scredis {
  redis {
    host = ${REDIS_HOST}
    port = ${REDIS_PORT}
  }
}

Upvotes: 2

Itamar Haber
Itamar Haber

Reputation: 50052

Disclaimer - I am no Scala expert :)

It looks like scredis is configured via application.conf and the Typesafe Config Library, so one way of doing this is generating the fly on dyno startup.

According to Heroku's build behavior for Scala, the way to do this is by adding a startup script to the mandatory stage task. The startup script should, in your case, parse the contents of the REDISCLOUD_URL env var and write them to the application.conf file.

Upvotes: 1

Guy Lubovitch
Guy Lubovitch

Reputation: 190

According these examples from scredis, it is also possible to create a client placing the configuration in the code.

// Creates a non-blocking client with provided parameters, falling back to default
// configuration for non-provided parameters.
val clientWithParameters = Client(
  host = "192.168.1.1",
  port = 6380,
  passwordOpt = Some("foobar")
)

So, a pseudo-combination of the two would probably be something like:

val redisUri = URI( System.getenv("REDISCLOUD_URL") )
val redisClient =  Client(
    host = redisUri.getHost(),
    port =  redisUri.getPort(),
    passwordOpt = redisUri.getPassword()
)

Upvotes: 2

Related Questions