Ratpack Classy and Compact Groovy Web Apps James Williams
About Me <ul><ul><li>Co-founder of Griffon </li></ul></ul><ul><ul><li>Author of &quot;Learning HTML5 Game Programming&quot...
Agenda <ul><ul><li>What are Groovy and Ratpack? </li></ul></ul><ul><ul><li>Routes </li></ul></ul><ul><ul><li>Templates </l...
What is Groovy? <ul><ul><li>Dynamic language on the JVM inspired by Java, Ruby, and Python </li></ul></ul><ul><ul><li>Comp...
What is Ratpack? <ul><ul><li>Apache 2 Licensed </li></ul></ul><ul><ul><li>Inspired by Sinatra (Ruby) </li></ul></ul><ul><u...
Ratpack vs (G)rails  
When to use Grails... <ul><ul><li>Data-driven model </li></ul></ul><ul><ul><li>Large number of models with differing endpo...
When to use Ratpack... <ul><ul><li>Low number of domain classes </li></ul></ul><ul><ul><li>Limited number of routes/endpoi...
Routes <ul><ul><li>Composed of a HTTP verb, an endpoint, and a code block </li></ul></ul><ul><ul><li>Verb can be one of th...
Routes <ul><li>get(&quot;/index&quot;) { </li></ul><ul><li>     // code </li></ul><ul><li>} </li></ul><ul><li>post(&quot;/...
Routes <ul><li>get(&quot;/index&quot;) { </li></ul><ul><li>     // code </li></ul><ul><li>} </li></ul><ul><li>post(&quot;/...
Sample Ratpack App <ul><li>import com.bleedingwolf.ratpack.* </li></ul><ul><li>class App { </li></ul><ul><ul><li>public st...
Demo  
Templates  
Templates <ul><ul><li>Uses Groovy SimpleTemplateEngine by default </li></ul></ul><ul><li>  </li></ul><ul><ul><li>Can swap ...
SimpleTemplateEngine <ul><ul><li>Means to generate text dynamically </li></ul></ul><ul><ul><li>Uses JSP-style instructions...
Markdown <ul><ul><li>Created by John Gruber and Aaron Swartz </li></ul></ul><ul><ul><li>Markup language for converting tex...
Markdown Basics <ul><ul><li>Headers (#) </li></ul></ul><ul><ul><li>Lists (* or numbers) </li></ul></ul><ul><ul><li>Code Sn...
Markdown + SimpleTemplateEngine
Sample Ratpack App <ul><li>import com.petebevin.markdown.* </li></ul><ul><li>import com.bleedingwolf.ratpack.* </li></ul><...
App Structure <ul><li>/ main/src/groovy </li></ul><ul><li>/ templates </li></ul><ul><li>/ public </li></ul><ul><li>/ lib  ...
Creating a Blog App with Ratpack  
App Essentials <ul><ul><li>Simple blog engine </li></ul></ul><ul><ul><li>Uses NoSQL for persistence </li></ul></ul><ul><ul...
Blog CRUD Routes <ul><li>get(&quot;/entry/create&quot;) { </li></ul><ul><ul><li>render '/entry/create.html' </li></ul></ul...
Blog CRUD Routes (cont'd) <ul><li>get(&quot;/post/list&quot;) { </li></ul><ul><ul><li>def list = derby.all() </li></ul></u...
Demo  
Creating a Todo Application <ul><ul><li>Only 103 LOC </li></ul></ul><ul><ul><li>Uses MongoDB for persistence </li></ul></u...
MongoDB <ul><ul><li>Document-oriented store </li></ul></ul><ul><ul><li>Querying </li></ul></ul><ul><ul><li>Client Library ...
Uki <ul><ul><li>34KB gzipped </li></ul></ul><ul><ul><li>UI framework for creating web applications </li></ul></ul><ul><ul>...
Uki example <ul><li>uki({ </li></ul><ul><li>   view: &quot;Button&quot;, text: &quot;Hello world!&quot;, </li></ul><ul><li...
CoffeeScript <ul><ul><li>Child of Ruby, Groovy, Python </li></ul></ul><ul><ul><li>Compiles to JavaScript </li></ul></ul><u...
Live Coding  
Running/Deploying the App  
Gradle <ul><ul><li>Language independent build system leveraging Groovy </li></ul></ul><ul><ul><li>Build system for Grails,...
Gradle Application Plugin <ul><ul><li>Requires Gradle 1.0+ </li></ul></ul><ul><ul><li>Code: plugin 'application' </li></ul...
Gradle File <ul><li>apply plugin: 'groovy' </li></ul><ul><li>apply plugin: 'application' </li></ul><ul><li>repositories { ...
Gradle File (cont'd) <ul><li>installApp { </li></ul><ul><li>into('build/install/'+applicationName){ </li></ul><ul><li>from...
Deploying App as a War  
Gradle File Additions <ul><li>war { </li></ul><ul><ul><li>into('/public') {  </li></ul></ul><ul><ul><ul><li>from('public')...
Web.xml File <ul><li><?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?> </li></ul><ul><li><!DOCTYPE web-app <...
Questions ?  
Links <ul><li>MarkdownJ -  http://markdownj.org </li></ul><ul><li>Ratpack -  http://github.com/bleedingwolf/Ratpack </li><...
Upcoming SlideShare
Loading in...5
×

Ratpack - Classy and Compact Groovy Web Apps

4,810

Published on

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

No Downloads
Views
Total Views
4,810
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
38
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Ratpack - Classy and Compact Groovy Web Apps

  1. 1. Ratpack Classy and Compact Groovy Web Apps James Williams
  2. 2. About Me <ul><ul><li>Co-founder of Griffon </li></ul></ul><ul><ul><li>Author of &quot;Learning HTML5 Game Programming&quot; </li></ul></ul><ul><li>       http://amzn.to/HTML5-Game-Book </li></ul><ul><ul><li>Blog: http://jameswilliams.be </li></ul></ul><ul><ul><li>Twitter: @ecspike </li></ul></ul><ul><ul><li>Google+: http://gplus.to/ecspike </li></ul></ul>
  3. 3. Agenda <ul><ul><li>What are Groovy and Ratpack? </li></ul></ul><ul><ul><li>Routes </li></ul></ul><ul><ul><li>Templates </li></ul></ul><ul><ul><li>Running the App </li></ul></ul><ul><ul><li>Deploying to Servlet Containers </li></ul></ul><ul><ul><li>Deployment </li></ul></ul><ul><ul><li>Demos / Live Coding </li></ul></ul>
  4. 4. What is Groovy? <ul><ul><li>Dynamic language on the JVM inspired by Java, Ruby, and Python </li></ul></ul><ul><ul><li>Compiles Java source - yay! </li></ul></ul><ul><ul><li>Can mix Java and Groovy classes and libraries </li></ul></ul><ul><ul><li>Do alot more with less code </li></ul></ul><ul><ul><li>http://groovy.codehaus.org </li></ul></ul>
  5. 5. What is Ratpack? <ul><ul><li>Apache 2 Licensed </li></ul></ul><ul><ul><li>Inspired by Sinatra (Ruby) </li></ul></ul><ul><ul><li>Logic is largely driven by Routes  </li></ul></ul><ul><ul><li>Powered by Jetty </li></ul></ul><ul><ul><li>Built-in support for Groovy SimpleTemplateEngine </li></ul></ul><ul><ul><li>Github: https://github.com/bleedingwolf/Ratpack </li></ul></ul>
  6. 6. Ratpack vs (G)rails  
  7. 7. When to use Grails... <ul><ul><li>Data-driven model </li></ul></ul><ul><ul><li>Large number of models with differing endpoints </li></ul></ul><ul><ul><li>Spring Security is a definite need  </li></ul></ul><ul><ul><li>Out of the box persistence integration is required </li></ul></ul><ul><ul><li>Plan to make great use of the Grails plugin ecosystem </li></ul></ul>
  8. 8. When to use Ratpack... <ul><ul><li>Low number of domain classes </li></ul></ul><ul><ul><li>Limited number of routes/endpoints </li></ul></ul><ul><ul><li>Model - View - Controller paradigm is not needed </li></ul></ul><ul><ul><li>Small but evolving REST API </li></ul></ul>
  9. 9. Routes <ul><ul><li>Composed of a HTTP verb, an endpoint, and a code block </li></ul></ul><ul><ul><li>Verb can be one of the nine canonical verbs or you can create your own </li></ul></ul><ul><ul><li>Endpoint corresponds to URL fragment. </li></ul></ul><ul><ul><li>:<identifier> injects that value into the urlparams object </li></ul></ul><ul><ul><li>Code block is injected with: </li></ul></ul><ul><ul><ul><li>request </li></ul></ul></ul><ul><ul><ul><li>response </li></ul></ul></ul><ul><ul><ul><li>renderer      </li></ul></ul></ul><ul><ul><ul><li>urlparams </li></ul></ul></ul>
  10. 10. Routes <ul><li>get(&quot;/index&quot;) { </li></ul><ul><li>    // code </li></ul><ul><li>} </li></ul><ul><li>post(&quot;/login&quot;) { </li></ul><ul><li>    // code </li></ul><ul><li>} </li></ul><ul><li>put(&quot;/post/:id&quot;) { </li></ul><ul><li>    // code </li></ul><ul><li>} </li></ul>
  11. 11. Routes <ul><li>get(&quot;/index&quot;) { </li></ul><ul><li>    // code </li></ul><ul><li>} </li></ul><ul><li>post(&quot;/login&quot;) { </li></ul><ul><li>    // code </li></ul><ul><li>} </li></ul><ul><li>put(&quot;/post/:id&quot;) { ==>  /post/3 </li></ul><ul><li>    // code             urlparams.id == 3   </li></ul><ul><li>} </li></ul>
  12. 12. Sample Ratpack App <ul><li>import com.bleedingwolf.ratpack.* </li></ul><ul><li>class App { </li></ul><ul><ul><li>public static void main(String []args) { </li></ul></ul><ul><ul><ul><li>def app = Ratpack.app { </li></ul></ul></ul><ul><ul><ul><ul><li>set 'public', 'public' </li></ul></ul></ul></ul><ul><ul><ul><ul><li>set 'templateRoot', 'templates' </li></ul></ul></ul></ul><ul><ul><ul><ul><li>post(&quot;/&quot;) { </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>request.toString() + &quot;n&quot; +params.toString() </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul><ul><ul><ul><ul><li>get(&quot;/&quot;) { </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>&quot;Hello world&quot; </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>RatpackServlet.serve(app) </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul>
  13. 13. Demo  
  14. 14. Templates  
  15. 15. Templates <ul><ul><li>Uses Groovy SimpleTemplateEngine by default </li></ul></ul><ul><li>  </li></ul><ul><ul><li>Can swap in other template engines </li></ul></ul><ul><ul><li>SimpleTemplateEngine can be used in conjunction with others </li></ul></ul>
  16. 16. SimpleTemplateEngine <ul><ul><li>Means to generate text dynamically </li></ul></ul><ul><ul><li>Uses JSP-style instructions for code blocks <% %>  and <%= %> </li></ul></ul><ul><ul><li>Can also use Groovy expression evaluators ${expression} </li></ul></ul><ul><li>  </li></ul>
  17. 17. Markdown <ul><ul><li>Created by John Gruber and Aaron Swartz </li></ul></ul><ul><ul><li>Markup language for converting text to (X)HTML </li></ul></ul><ul><ul><li>Supported by many frameworks and websites </li></ul></ul><ul><ul><li>In many different languages </li></ul></ul><ul><ul><li>  http://daringfireball.net/projects/markdown/ </li></ul></ul>
  18. 18. Markdown Basics <ul><ul><li>Headers (#) </li></ul></ul><ul><ul><li>Lists (* or numbers) </li></ul></ul><ul><ul><li>Code Snippets (`) </li></ul></ul><ul><ul><li>Emphasis (*) </li></ul></ul><ul><ul><li>Strong/Bold (**) </li></ul></ul><ul><ul><li>Links </li></ul></ul>
  19. 19. Markdown + SimpleTemplateEngine
  20. 20. Sample Ratpack App <ul><li>import com.petebevin.markdown.* </li></ul><ul><li>import com.bleedingwolf.ratpack.* </li></ul><ul><li>class App { </li></ul><ul><ul><li>public static void main(String []args) { </li></ul></ul><ul><ul><ul><li>def app = Ratpack.app { </li></ul></ul></ul><ul><ul><ul><ul><li>set 'public', 'public' </li></ul></ul></ul></ul><ul><ul><ul><ul><li>set 'templateRoot', 'templates' </li></ul></ul></ul></ul><ul><ul><ul><ul><li>                                 get(&quot;/time&quot;) { </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>render &quot;index.html&quot;, [time:new Date()] </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul><ul><ul><ul><ul><li>get(&quot;/&quot;) { </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>def m = new MarkdownProcessor() </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>def p = render 'template.md', [num:4] </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>m.markdown(p) </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>RatpackServlet.serve(app) </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul>
  21. 21. App Structure <ul><li>/ main/src/groovy </li></ul><ul><li>/ templates </li></ul><ul><li>/ public </li></ul><ul><li>/ lib                                  ==> contains Ratpack.jar </li></ul><ul><li>/ resources/web.xml    ==> only needed for packaging war </li></ul><ul><li>/ build.gradle                 ==> contains Ratpack's maven depos </li></ul>
  22. 22. Creating a Blog App with Ratpack  
  23. 23. App Essentials <ul><ul><li>Simple blog engine </li></ul></ul><ul><ul><li>Uses NoSQL for persistence </li></ul></ul><ul><ul><li>Uses a tweaked Tumblr template </li></ul></ul><ul><ul><li>~ 156 lines of Groovy code </li></ul></ul><ul><ul><li>Source: http://github.com/jwill/Conference-Demos/user-groups </li></ul></ul>
  24. 24. Blog CRUD Routes <ul><li>get(&quot;/entry/create&quot;) { </li></ul><ul><ul><li>render '/entry/create.html' </li></ul></ul><ul><li>} </li></ul><ul><li>get(&quot;/entry/show/:id&quot;) { </li></ul><ul><ul><li>def entry = derby.get(urlparams.id) </li></ul></ul><ul><ul><li>render '/entry/show.html', [entry: entry] </li></ul></ul><ul><li>} </li></ul><ul><li>get(&quot;/entry/delete/:id&quot;) { </li></ul><ul><ul><li>derby.remove(urlparams.id) </li></ul></ul><ul><li>} </li></ul>
  25. 25. Blog CRUD Routes (cont'd) <ul><li>get(&quot;/post/list&quot;) { </li></ul><ul><ul><li>def list = derby.all() </li></ul></ul><ul><ul><li>list.sort(sortClosure.curry('dateCreated')) </li></ul></ul><ul><ul><li>list = list.reverse() </li></ul></ul><ul><ul><li>render '/post/index.html', [posts: list] </li></ul></ul><ul><li>} </li></ul><ul><li>post(&quot;/post/save&quot;) { </li></ul><ul><ul><li>def post = new Post(params) </li></ul></ul><ul><ul><li>derby.save(post.properties, {obj -> </li></ul></ul><ul><ul><ul><li>println &quot;Finished saving.&quot; </li></ul></ul></ul><ul><ul><ul><li>new JSONObject([success:true]).toString() </li></ul></ul></ul><ul><ul><li>}) </li></ul></ul><ul><li>} </li></ul>
  26. 26. Demo  
  27. 27. Creating a Todo Application <ul><ul><li>Only 103 LOC </li></ul></ul><ul><ul><li>Uses MongoDB for persistence </li></ul></ul><ul><ul><li>uki.js and CoffeeScript for UI code </li></ul></ul><ul><ul><li>Source: http://bit.ly/s12gx-ratpack </li></ul></ul>
  28. 28. MongoDB <ul><ul><li>Document-oriented store </li></ul></ul><ul><ul><li>Querying </li></ul></ul><ul><ul><li>Client Library Support </li></ul></ul><ul><ul><li>Full Index Support </li></ul></ul><ul><ul><li>Commercial and Web-Based Options </li></ul></ul><ul><ul><li>GridFS  </li></ul></ul><ul><ul><li>http://www.mongodb.org </li></ul></ul>
  29. 29. Uki <ul><ul><li>34KB gzipped </li></ul></ul><ul><ul><li>UI framework for creating web applications </li></ul></ul><ul><ul><li>Rich widget library </li></ul></ul><ul><ul><li>Code-based layout </li></ul></ul><ul><ul><li>Constraints </li></ul></ul><ul><ul><li>JQuery-like selectors </li></ul></ul><ul><ul><li>http://ukijs.org </li></ul></ul>
  30. 30. Uki example <ul><li>uki({ </li></ul><ul><li>  view: &quot;Button&quot;, text: &quot;Hello world!&quot;, </li></ul><ul><li>  rect: &quot;120 80 180 24&quot; </li></ul><ul><li>}).attachTo(document.getElementById(&quot;test&quot;)); </li></ul><ul><li>  </li></ul><ul><li>uki(&quot;Button[text^=Hello]&quot;).click( </li></ul><ul><li>  function() { alert(this.text()); } </li></ul><ul><li>); </li></ul>
  31. 31. CoffeeScript <ul><ul><li>Child of Ruby, Groovy, Python </li></ul></ul><ul><ul><li>Compiles to JavaScript </li></ul></ul><ul><ul><li>Produces *READABLE* Javascript </li></ul></ul><ul><ul><li>Classes / Inheritance / Polymorphism </li></ul></ul><ul><ul><li>Groovy style loops </li></ul></ul><ul><ul><li>Aliases for Comparison Operators (is, isnt, not, and, or, yes/on, no/off) </li></ul></ul><ul><ul><li>CoffeeScript for Groovy Devs - Scott Davis - Fri @ 12:45P </li></ul></ul>
  32. 32. Live Coding  
  33. 33. Running/Deploying the App  
  34. 34. Gradle <ul><ul><li>Language independent build system leveraging Groovy </li></ul></ul><ul><ul><li>Build system for Grails, Spock, and many others </li></ul></ul><ul><ul><li>Uses Maven for dependency management </li></ul></ul><ul><ul><li>Vibrant plugin community </li></ul></ul><ul><ul><li>http://gradle.org </li></ul></ul>
  35. 35. Gradle Application Plugin <ul><ul><li>Requires Gradle 1.0+ </li></ul></ul><ul><ul><li>Code: plugin 'application' </li></ul></ul><ul><ul><li>Provides common tasks for the application lifecycle. </li></ul></ul><ul><ul><li>Provides the following tasks: </li></ul></ul><ul><ul><ul><li>run </li></ul></ul></ul><ul><ul><ul><li>installApp </li></ul></ul></ul><ul><ul><ul><li>distZip </li></ul></ul></ul>
  36. 36. Gradle File <ul><li>apply plugin: 'groovy' </li></ul><ul><li>apply plugin: 'application' </li></ul><ul><li>repositories { </li></ul><ul><ul><li>flatDir name:'lib', dirs:'lib' </li></ul></ul><ul><ul><li>mavenCentral() </li></ul></ul><ul><li>} </li></ul><ul><li>dependencies { </li></ul><ul><ul><li>groovy group:'org.codehaus.groovy', name:'groovy', version:'1.8.0' </li></ul></ul><ul><ul><li>groovy group:'com.bleedingwolf.ratpack', name: 'Ratpack', version:'0.2-SNAPSHOT' </li></ul></ul><ul><ul><li>// Ratpack's other dependencies </li></ul></ul><ul><li>} </li></ul>
  37. 37. Gradle File (cont'd) <ul><li>installApp { </li></ul><ul><li>into('build/install/'+applicationName){ </li></ul><ul><li>from ('templates').into '/templates' </li></ul><ul><li>} </li></ul><ul><li>into('build/install/'+applicationName){ </li></ul><ul><li>from ('public').into '/public' </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>distZip { </li></ul><ul><li>into(applicationName){ </li></ul><ul><li>from ('templates').into applicationName+'/templates' </li></ul><ul><li>} </li></ul><ul><li>into(applicationName){ </li></ul><ul><li>from ('public').into applicationName+'/public' </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>mainClassName = &quot;BlogApp&quot; </li></ul>
  38. 38. Deploying App as a War  
  39. 39. Gradle File Additions <ul><li>war { </li></ul><ul><ul><li>into('/public') { </li></ul></ul><ul><ul><ul><li>from('public') </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>into('/templates') { </li></ul></ul><ul><ul><ul><li>from('templates') </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>classpath fileTree('lib') </li></ul></ul><ul><ul><li>webXml = file('resources/web.xml') </li></ul></ul><ul><li>} </li></ul>
  40. 40. Web.xml File <ul><li><?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?> </li></ul><ul><li><!DOCTYPE web-app </li></ul><ul><li>    PUBLIC &quot;-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN&quot; </li></ul><ul><li>    &quot;http://java.sun.com/dtd/web-app_2_3.dtd&quot;> </li></ul><ul><li><web-app> </li></ul><ul><li><servlet> </li></ul><ul><li>        <servlet-name>AppServlet</servlet-name> </li></ul><ul><li>        <servlet-class>AppServlet</servlet-class> </li></ul><ul><li></servlet> </li></ul><ul><li><servlet-mapping> </li></ul><ul><li>        <servlet-name>AppServlet</servlet-name> </li></ul><ul><li>        <url-pattern>/*</url-pattern> </li></ul><ul><li></servlet-mapping> </li></ul><ul><li></web-app> </li></ul>
  41. 41. Questions ?  
  42. 42. Links <ul><li>MarkdownJ - http://markdownj.org </li></ul><ul><li>Ratpack - http://github.com/bleedingwolf/Ratpack </li></ul><ul><li>Gradle - http://gradle.org </li></ul><ul><li>Demos - http://github.com/jwill/ConferenceDemos/strangeloop </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×