RatpackClassy and Compact Groovy Web Apps        James Williams
About Me● Co-founder of Griffon● Author of "Learning HTML5  Game Programming"  http://amzn.to/HTML5-Game-Book● Blog: http:...
Agenda● What is Ratpack?● Routes● Templates● Running the App● Deploying to Servlet Containers● Deployment● Demos
What is Ratpack?● Apache 2 Licensed● Inspired by Sinatra (Ruby)● Logic is largely driven by Routes● Powered by Jetty● Buil...
Ratpack vs Grails
When to use Grails... ● Data-driven model ● Large number of models with differing endpoints ● Spring Security is a definit...
When to use Ratpack...● Low number of domain classes● Limited number of routes/endpoints● Model - View - Controller paradi...
Routes● Composed of a HTTP verb, an endpoint, and a code block● Verb can be one of the nine canonical verbs or you can  cr...
Routesget("/index") {  // code}post("/login") {  // code}put("/post/:id") {  // code}
Routesget("/index") {  // code}post("/login") {  // code}put("/post/:id") { ==>   /post/3  // code                  urlpar...
Demo
Authentication
Basic Authentication● Client-side:   ○ Username and password are concatenated separated by      a colon   ○ Resulting stri...
Basic Authenticationclass Auth {static doAuth = {app ->def handler = { req ->        def authHeader = req.getHeader("Autho...
Creating a Blog App with        Ratpack
App Essentials● Simple blog engine● Uses NoSQL for persistence● Uses a tweaked Tumblr template● ~ 156 lines of Groovy code...
Blog CRUD Routesget("/post/create") {render /post/create.html, [header:header]}get("/post/show/:id") {def post = derby.get...
Blog CRUD Routes (contd)get("/post/list") {def list = derby.all()list.sort(sortClosure.curry(dateCreated))list = list.reve...
Deploying the App
App Structure/ main/src/groovy/ templates/ public/ lib                   ==> contains Ratpack.jar/ resources/web.xml   ==>...
Gradle Application Plugin● Requires Gradle 1.0+● Code: plugin application● Provides common tasks for the application lifec...
Gradle Fileapply plugin: groovyapply plugin: applicationrepositories {flatDir name:lib, dirs:libmavenCentral()}dependencie...
Gradle File (contd)installApp {into(build/install/+applicationName){from (templates).into /templates}into(build/install/+a...
Deploying App as a War
Gradle File Additionswar {into(/public) {from(public)}into(/templates) {from(templates)}classpath fileTree(lib)webXml = fi...
Questions ?
Upcoming SlideShare
Loading in …5
×

SF Grails - Ratpack - Compact Groovy Webapps - James Williams

2,197 views

Published on

Slides for SF Grails meetup.

Ratpack, a micro web framework for Groovy, "inspired by the excellent Sinatra framework for Ruby, aims to make Groovy web development more classy."

We are happy to have James Williams (Co-Creator of Griffon) to be presenting!

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,197
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
10
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

SF Grails - Ratpack - Compact Groovy Webapps - James Williams

  1. 1. RatpackClassy and Compact Groovy Web Apps James Williams
  2. 2. About Me● Co-founder of Griffon● Author of "Learning HTML5 Game Programming" http://amzn.to/HTML5-Game-Book● Blog: http://jameswilliams.be● Twitter: @ecspike● G+: http://gplus.to/ecspike
  3. 3. Agenda● What is Ratpack?● Routes● Templates● Running the App● Deploying to Servlet Containers● Deployment● Demos
  4. 4. What is Ratpack?● Apache 2 Licensed● Inspired by Sinatra (Ruby)● Logic is largely driven by Routes● Powered by Jetty● Built-in support for Groovy SimpleTemplateEngine● Github: https://github.com/bleedingwolf/Ratpack
  5. 5. Ratpack vs Grails
  6. 6. When to use Grails... ● Data-driven model ● Large number of models with differing endpoints ● Spring Security is a definite need ● Out of the box persistence integration is required ● Plan to make great use of the Grails plugin ecosystem
  7. 7. When to use Ratpack...● Low number of domain classes● Limited number of routes/endpoints● Model - View - Controller paradigm is not needed● Small but evolving REST API
  8. 8. Routes● Composed of a HTTP verb, an endpoint, and a code block● Verb can be one of the nine canonical verbs or you can create your own● Endpoint corresponds to URL fragment.● :<identifier> injects that value into the urlparams object● Code block is injected with: ○ request ○ response ○ renderer ○ urlparams
  9. 9. Routesget("/index") { // code}post("/login") { // code}put("/post/:id") { // code}
  10. 10. Routesget("/index") { // code}post("/login") { // code}put("/post/:id") { ==> /post/3 // code urlparams.id == 3}
  11. 11. Demo
  12. 12. Authentication
  13. 13. Basic Authentication● Client-side: ○ Username and password are concatenated separated by a colon ○ Resulting string is base 64 encoded ○ Attached to request header● Server-side: ○ Header is pulled from request ○ Decoded ○ Validated ○ Sends response
  14. 14. Basic Authenticationclass Auth {static doAuth = {app ->def handler = { req -> def authHeader = req.getHeader("Authorization") def encodedValue = authHeader.split(" ")[1] def decodedValue = new String(encodedValue.decodeBase64())?.split(":") // do some sort of validation here if (decodedValue[0] == "") { return "Unauthorized" } else { decodedValue } } app.metaClass.doAuth = handler; }}
  15. 15. Creating a Blog App with Ratpack
  16. 16. App Essentials● Simple blog engine● Uses NoSQL for persistence● Uses a tweaked Tumblr template● ~ 156 lines of Groovy code● Source: http://github.com/jwill/Conference-Demos/user- groups
  17. 17. Blog CRUD Routesget("/post/create") {render /post/create.html, [header:header]}get("/post/show/:id") {def post = derby.get(urlparams.id)render /post/show.html, [post: post]}
  18. 18. Blog CRUD Routes (contd)get("/post/list") {def list = derby.all()list.sort(sortClosure.curry(dateCreated))list = list.reverse()render /post/index.html, [posts: list]}post("/post/save") {def post = new Post(params)derby.save(post.properties, {obj ->println "Finished saving."new JSONObject([success:true]).toString()})}
  19. 19. Deploying the App
  20. 20. App Structure/ main/src/groovy/ templates/ public/ lib ==> contains Ratpack.jar/ resources/web.xml ==> only needed for packaging war/ build.gradle ==> contains Ratpacks maven depos
  21. 21. Gradle Application Plugin● Requires Gradle 1.0+● Code: plugin application● Provides common tasks for the application lifecycle.● Provides the following tasks: ○ run ○ installApp ○ distZip
  22. 22. Gradle Fileapply plugin: groovyapply plugin: applicationrepositories {flatDir name:lib, dirs:libmavenCentral()}dependencies {groovy group:org.codehaus.groovy, name:groovy, version:1.8.0groovy group:com.bleedingwolf.ratpack, name: Ratpack, version:0.2-SNAPSHOT// Ratpacks other dependencies}
  23. 23. Gradle File (contd)installApp {into(build/install/+applicationName){from (templates).into /templates}into(build/install/+applicationName){from (public).into /public}}distZip {into(applicationName){from (templates).into applicationName+/templates}into(applicationName){from (public).into applicationName+/public}}mainClassName = "BlogApp"
  24. 24. Deploying App as a War
  25. 25. Gradle File Additionswar {into(/public) {from(public)}into(/templates) {from(templates)}classpath fileTree(lib)webXml = file(resources/web.xml)}
  26. 26. Questions ?

×