Last updated by burtbeckwith 3 weeks ago
The Ratpack plugin lets you use
Ratpack inside Grails. It was inspired by
this blog post.
Also see
this blog post for more ideas on how to use the plugin.
Usage
To use it, create one or more "Ratpack" artifacts in your application's grails-app/ratpack folder (this is created when you install the plugin). The format is similar to the standard "app" script that you would use in standalone Ratpack, i.e. a Closure containing method calls for the various HTTP verbs (GET, POST, etc.) along with the url and a Closure that will handle requests on that url. The top-level closure must be called "urls". For example here are two artifact classes:
package com.myappclass FooRatpack { def userService def urls = { get("/") {
def ua = headers['user-agent']
"Your user-agent: $ua from Ratpack"
} get("/foo/:name") {
"Hello, ${urlparams.name}"
}
}
}and
package com.myappclass BarRatpack { def urls = {
get("/person/:id") {
"Person #${urlparams.id}"
}
}
}These will configure 3 url patterns; http://localhost:8080/appname/ratpack, http://localhost:8080/appname/ratpack/person/5, and http://localhost:8080/appname/ratpack/foo/world
Note that you can use dependency injection for Spring beans like in any other Grails artifact; in this example the FooRatpack class has a dependency injection for the "userService" bean.
The plugin also supports reloading in development mode.
Configuration
You can change the mapped url root from "/ratpack/*" using the
grails.plugin.ratpack.urlPattern attribute in Config.groovy, e.g.
grails.plugin.ratpack.urlPattern = '/rp/*'
If you're using file templates, you can specify the template path the traditional way in a
urls closure with the
set command, e.g.
'templateRoot', 'grails-app/ratpack/templates'
but when using the plugin you should set that in
Config.groovy with the
templateRoot configuration option:
grails.plugin.ratpack.templateRoot='grails-app/ratpack/templates'
The benefit of this approach is that the files will get copied into the classpath when building a war file, making them available as files in dev mode and as resources in war mode. Using
Config.groovy also allows environment-specific configuration.
History
- January 29, 2012
- 1.0.1 release
- Added support for
templateRoot to support templates in war files
- January 28, 2012
Please report any issues in
JIRA