Bomin
Bomin

Reputation: 1667

Scala way choosing configuration regarding different environment

My application needs to read configuration file either from the resource directory or from s3.

For local development, I need to read it from the local resource directory. So, when build my project, I don't put the configuration file config.properties into my application jar file. In this case, it should read the configuration from S3. When I can think of doing this scala is pretty much like what I do it by java

val stream : InputStream = getClass.getResourceAsStream("/config.properties")
if (stream != null) {
  val lines = scala.io.Source.fromInputStream( stream ).getLines
} else {
  /*read it from S3*/
}

But I think scala gives a more functional programing sytax. Any advice?

Upvotes: 0

Views: 100

Answers (1)

jwvh
jwvh

Reputation: 51271

There are probably better ways to go about what you're after, but here's a more-or-less straight translation of the posted code.

val lines:Iterator[String] = Option(getClass.getResourceAsStream("/config.properties"))
                            .fold{/*read from S3
                                  return Iterator*/}(io.Source.fromInputStream(_).getLines)

Upvotes: 1

Related Questions