How to manage application.conf in several environments with play 2.0?

后端 未结 6 1501
栀梦
栀梦 2020-12-07 18:21

With Play 1.2, I can prefix the configuration keys with the framework ID or application mode as follows:

# Production configuration
%prod.http.port=80
%prod.         


        
6条回答
  •  佛祖请我去吃肉
    2020-12-07 18:27

    I have had this question for a long time as well and below is the best approach I've learned so far, I got a hint when asking a similar question on the Play 2 google group.

    In you application.config use the following syntax to override a configuration value when a system parameter is present:

    # Local machine fallback URI
    mongodb.uri="mongodb://192.168.56.101:27017/application"
    # Env variable override
    mongodb.uri=${?MONGOLAB_URI}
    

    The question mark means that you don't override with the env variable if it is not set. If you just use ${MONGOLAB_URI} you expect the variable to be set and, I assume, you get an exception of some kind if it is not set.

    For completeness, here is an example of how you'd read the value:

    lazy val mongoUri = current.configuration.getString("mongodb.uri").getOrElse("mongodb:///")
    

    With this approach there is one caveat: make sure you keep your system param configuration in a SCM of some kind.

提交回复
热议问题