it's possible to add groovy script as config source?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

it's possible to add groovy script as config source?

bobwenx
The ConfigDataBuilder only support json/yaml/properties file as config source. but sometimes i want to setting config item dynamically(eg, evaluating the script code & apply the result as config item).
just wondering if it's possible to add groovy script as config source?
Reply | Threaded
Open this post in threaded view
|

Re: it's possible to add groovy script as config source?

Marcin Erdmann
The Ratpack core team believes that there are some drawbacks to ConfigObject, especially the fact that it comes with autovivification (https://en.wikipedia.org/wiki/Autovivification) which might lead to subtle bugs and therefore is reluctant to add support for it to core even though it's such a commonly used configuration mechanism in the Groovy world.

There is nothing stopping you from providing a custom implementation of ratpack.config.ConfigSource and then registering a source using ServerConfigBuilder.add(ConfigSource) - ServerConfigBuilder is the type backing the closure passed into Groovy.Ratpack.serverConfig().

There is another quick and dirty option of parsing the script in your serverConfig block, transforming it into a Properties instance and then adding that to the server config:

ratpack {
  serverConfig {
    def configScriptUrl = getClass().getResource("Config.groovy")
    props(new ConfigSlurper().parse(configScriptUrl).toProperties())
  }
}

One option of getting the script onto the classpath is putting it inside of the base directory which for the standard setup is located at src/ratpack.