kofhearts
kofhearts

Reputation: 3794

how to get certain property values from config file depending on the environment?

I have some keys that i want to keep in the config file. I have two different keys, one for use in development setting and another for use when the environment is set to production. Now, in grails we extract these property values from config file using

grailsApplication.config.[name of the property in config file]

is it possible to have conditional setting on config file that will return the right key depending on whether the environment is set to production or development? I appreciate any help! Thanks!

Upvotes: 2

Views: 576

Answers (1)

Prakash Thete
Prakash Thete

Reputation: 3892

We use the approach of separate external config files for different environments and then include them in "config.groovy" depending on the environments like below

environments {
    test {
        grails.logging.jul.usebridge = true
        grails.config.locations = ["file:${userHome}/.grails/${appName}-config-TEST.groovy"]
    }
    development {
        grails.logging.jul.usebridge = true
        grails.config.locations = ["file:${userHome}/.grails/${appName}-config-DEV.groovy"]
    }
    production {
        grails.logging.jul.usebridge = false
        grails.config.locations = ["file:${userHome}/.grails/${appName}-config-PROD.groovy"]
    }
}

But if you want common file for all the environments then you can use the "Environment" available in "grails.util" package like below

package asia.grails.myexample
import grails.util.Environment
class SomeController {
    def someAction() { 
        if (Environment.current == Environment.DEVELOPMENT) {
            // insert Development environment specific key here
        } else 
        if (Environment.current == Environment.TEST) {
            // insert Test environment specific key here
        } else 
        if (Environment.current == Environment.PRODUCTION) {
            // insert Production environment specific key here
        }
        render "Environment is ${Environment.current}"
    }
}

Upvotes: 3

Related Questions