SpringOne 2GX 2011

Chicago, October 25-28, 2011

Running Ratpack inside Grails

Posted by: James Williams on 2012-01-24 12:30:00.0
When I would present on Ratpack at conferences, one of the main questions I'd get would be around the migration path from Ratpack to Grails. I'd usually answer noting that you could run Ratpack along side Grails. It turns out it is incredibly simple to run a Ratpack app inside Grails. I don't think it's a long term solution and best as an intermediary step to migrate from one side to the other.
 
1. Grab and build Ratpack.
Provided you already have Gradle installed, run gradle buildDistro. The only file you need is the main Ratpack-x.x file. Grails will provide the Groovy and servlet container run time for you. Add this to the lib directory of your Grails app. 
 
2. Create your Ratpack app class. 
Putting the script into a proper class, as shown below, makes it easier to reference it from our Servlet.
 
import java.text.SimpleDateFormat
import com.bleedingwolf.ratpack.*

class SampleApp {

def app = Ratpack.app {
set 'port', 4999

get("/") {
def ua = headers['user-agent']
    "Your user-agent: ${ua} from Ratpack"
}
    
get("/foo/:name") {
"Hello, ${urlparams.name}"
}
    
get("/person/:id") {
"Person #${urlparams.id}"
}
  }

  public static void main(String[] args) {
SampleServlet.serve(new SampleApp().app)
  }
}
 
3. Subclass *RatpackServlet*.
If you don't mind getting an logging error, you can use something as simple as the following file:
import com.bleedingwolf.ratpack.*

class SampleServlet extends RatpackServlet {
            void init() {
app = new SampleApp().app
            }                    
}
 
4. Add the servlet information to your application.
There is a really confusing way to do it using the resources.groovy file but I prefer just adding it to the web.xml by hand. Run
grails install-templates
and navigate to src/templates/war/web.xml and add the following:
<servlet>
<servlet-name>SampleServlet</servlet-name>
        <servlet-class>SampleServlet</servlet-class>

       </servlet>

<servlet-mapping>
<servlet-name>SampleServlet</servlet-name>
                <url-pattern>/ratpack/*</url-pattern>
</servlet-mapping>
 
The servlet needs to be put on a url-pattern that isn't root (/*) so I used (/ratpack/*). Run grails run-app and you're all set.

 


About James Williams

James Williams

James Williams is a Senior Software Engineer for Taulia based in Silicon Valley and a frequent conference speaker. He is a co-creator of the Griffon project, a rich desktop framework for Java applications. He and his team WalkIN, created a product on a coach bus while riding to SXSW, and were crowned winners of StartupBus 2011. He is the author of the upcoming book "Learning HTML5 Game Programming..." for Addison-Wesley. He blogs at http://jameswilliams.be/blog and tweets as @ecspike.

More About James »

NFJS, the Magazine

2011-12-01 00:00:00.0 Issue Now Available
  • BDD and REST

    by Brian Sletten
  • Mocks and Stubs in Groovy Tests

    by Kenneth Kousen
  • Algorithms for Better Text Search Results

    by John Griffin
  • Knowns and Unknowns of Scrum and Agile

    by Brian Tarbox
Learn More »