DNNX
DNNX

Reputation: 6255

How to use an external service as a source for Grails app configuration?

Section 3.4 of Grails documentation says that Grails app can be configured from an external source:

grails.config.locations = [
    "classpath:${appName}-config.properties",
    "classpath:${appName}-config.groovy",
    "file:${userHome}/.grails/${appName}-config.properties",
    "file:${userHome}/.grails/${appName}-config.groovy" ]

Also, it is possible to load config by specifying a class that is a config script:

grails.config.locations = [com.my.app.MyConfig]

My questions are:

  1. Could you please give an example of how MyConfig class implementation could look like? It is not quite clear from the documentation.
  2. If I want to use some external JSON REST service as a source for my config data, how can this be implemented?

Upvotes: 0

Views: 320

Answers (2)

Marco
Marco

Reputation: 15929

Your application configuration can actually be a Groovy script. So any class which looks like your Config.groovy can act as a configuration class. In our projects we tend to keep the configuration outside the grails application so that the application can be configured without recompiling everything.

Maybe this post of mine will give you a hint on how to use and load external configuration files.

Upvotes: 0

kenota
kenota

Reputation: 5662

Answer for second question: you can do that in BootStrap.groovy init closure, because basically, it allows you to execute any code:

// Inject grails app
def grailsApplication

def init = { servletContext ->
    def externalValue = getItUsingRest(someUrl)
    grailsApplication.config.my.custom.var = externalValue
}

Depending on version of grails you are using, you might need to use

org.codehaus.groovy.grails.commons.ConfigurationHolde.config

to get to config instead.

Upvotes: 1

Related Questions