I need a way to configure my scala application. Configgy seemed the way to go in Scala but it is deprecated https://github.com/robey/configgy#readme and now this functionality is in Ostrich.
Is there a code example on how to use Ostrich only for configuration? I'm not interested in collecting the statistics.
I'd like to know the official answer as well, but nobody answered so I decided to poke around. Sorry if this answer isn't quite comprehensive.
The best example I found was in
com.twitter.ostrich.admin.RuntimeEnvironment
, especially if you look mainly atloadConfig
.Say you want to configure an instance of class
T
. The basic idea is as follows:java.io.File
that contains Scala source code that evaluates to acom.twitter.util.Config[T]
.Config[T]
you'll need to have adef apply(): T
method. To keep the implementation details out of the config file, you'll want to define a class in your project that extendsConfig[T]
. You can also use this class to define default/required fields.com.twitter.util.Eval
instance, and callapply(file)
to get aConfig[T]
instance.config.validate()
to throw the proper exceptions for malformed config files.config.apply()
to get your fully configured instance ofT
.Here is a simple example, where I configure a new
WidgetService
:And here is
mywidget_config.scala
:Note: you may have to make modifications if you put this in a package. I did everything in the default package for brevity.
To get the dependencies to work, I added this to my SBT config: