Ratpack - Classy and Compact Groovy Web Apps
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Ratpack - Classy and Compact Groovy Web Apps

  • 5,245 views
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
5,245
On Slideshare
5,240
From Embeds
5
Number of Embeds
2

Actions

Shares
Downloads
27
Comments
0
Likes
4

Embeds 5

https://twitter.com 4
http://10.60.60.199 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Ratpack Classy and Compact Groovy Web Apps James Williams
  • 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
      • Google+: http://gplus.to/ecspike
  • 3. Agenda
      • What are Groovy and Ratpack?
      • Routes
      • Templates
      • Running the App
      • Deploying to Servlet Containers
      • Deployment
      • Demos / Live Coding
  • 4. What is Groovy?
      • Dynamic language on the JVM inspired by Java, Ruby, and Python
      • Compiles Java source - yay!
      • Can mix Java and Groovy classes and libraries
      • Do alot more with less code
      • http://groovy.codehaus.org
  • 5. 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
  • 6. Ratpack vs (G)rails  
  • 7. 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
  • 8. 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
  • 9. 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
  • 10. Routes
    • get(&quot;/index&quot;) {
    •     // code
    • }
    • post(&quot;/login&quot;) {
    •     // code
    • }
    • put(&quot;/post/:id&quot;) {
    •     // code
    • }
  • 11. Routes
    • get(&quot;/index&quot;) {
    •     // code
    • }
    • post(&quot;/login&quot;) {
    •     // code
    • }
    • put(&quot;/post/:id&quot;) { ==>  /post/3
    •     // code             urlparams.id == 3  
    • }
  • 12. Sample Ratpack App
    • import com.bleedingwolf.ratpack.*
    • class App {
      • public static void main(String []args) {
        • def app = Ratpack.app {
          • set 'public', 'public'
          • set 'templateRoot', 'templates'
          • post(&quot;/&quot;) {
            • request.toString() + &quot;n&quot; +params.toString()
          • }
          • get(&quot;/&quot;) {
            • &quot;Hello world&quot;
          • }
        • }
        • RatpackServlet.serve(app)
      • }
    • }
  • 13. Demo  
  • 14. Templates  
  • 15. Templates
      • Uses Groovy SimpleTemplateEngine by default
    •  
      • Can swap in other template engines
      • SimpleTemplateEngine can be used in conjunction with others
  • 16. SimpleTemplateEngine
      • Means to generate text dynamically
      • Uses JSP-style instructions for code blocks <% %>  and <%= %>
      • Can also use Groovy expression evaluators ${expression}
    •  
  • 17. Markdown
      • Created by John Gruber and Aaron Swartz
      • Markup language for converting text to (X)HTML
      • Supported by many frameworks and websites
      • In many different languages
      •   http://daringfireball.net/projects/markdown/
  • 18. Markdown Basics
      • Headers (#)
      • Lists (* or numbers)
      • Code Snippets (`)
      • Emphasis (*)
      • Strong/Bold (**)
      • Links
  • 19. Markdown + SimpleTemplateEngine
  • 20. Sample Ratpack App
    • import com.petebevin.markdown.*
    • import com.bleedingwolf.ratpack.*
    • class App {
      • public static void main(String []args) {
        • def app = Ratpack.app {
          • set 'public', 'public'
          • set 'templateRoot', 'templates'
          •                                  get(&quot;/time&quot;) {
            • render &quot;index.html&quot;, [time:new Date()]
          • }
          • get(&quot;/&quot;) {
            • def m = new MarkdownProcessor()
            • def p = render 'template.md', [num:4]
            • m.markdown(p)
          • }
        • }
        • RatpackServlet.serve(app)
      • }
    • }
  • 21. App Structure
    • / main/src/groovy
    • / templates
    • / public
    • / lib                                  ==> contains Ratpack.jar
    • / resources/web.xml    ==> only needed for packaging war
    • / build.gradle                 ==> contains Ratpack's maven depos
  • 22. Creating a Blog App with Ratpack  
  • 23. 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
  • 24. Blog CRUD Routes
    • get(&quot;/entry/create&quot;) {
      • render '/entry/create.html'
    • }
    • get(&quot;/entry/show/:id&quot;) {
      • def entry = derby.get(urlparams.id)
      • render '/entry/show.html', [entry: entry]
    • }
    • get(&quot;/entry/delete/:id&quot;) {
      • derby.remove(urlparams.id)
    • }
  • 25. Blog CRUD Routes (cont'd)
    • get(&quot;/post/list&quot;) {
      • def list = derby.all()
      • list.sort(sortClosure.curry('dateCreated'))
      • list = list.reverse()
      • render '/post/index.html', [posts: list]
    • }
    • post(&quot;/post/save&quot;) {
      • def post = new Post(params)
      • derby.save(post.properties, {obj ->
        • println &quot;Finished saving.&quot;
        • new JSONObject([success:true]).toString()
      • })
    • }
  • 26. Demo  
  • 27. Creating a Todo Application
      • Only 103 LOC
      • Uses MongoDB for persistence
      • uki.js and CoffeeScript for UI code
      • Source: http://bit.ly/s12gx-ratpack
  • 28. MongoDB
      • Document-oriented store
      • Querying
      • Client Library Support
      • Full Index Support
      • Commercial and Web-Based Options
      • GridFS 
      • http://www.mongodb.org
  • 29. Uki
      • 34KB gzipped
      • UI framework for creating web applications
      • Rich widget library
      • Code-based layout
      • Constraints
      • JQuery-like selectors
      • http://ukijs.org
  • 30. Uki example
    • uki({
    •   view: &quot;Button&quot;, text: &quot;Hello world!&quot;,
    •   rect: &quot;120 80 180 24&quot;
    • }).attachTo(document.getElementById(&quot;test&quot;));
    •  
    • uki(&quot;Button[text^=Hello]&quot;).click(
    •   function() { alert(this.text()); }
    • );
  • 31. CoffeeScript
      • Child of Ruby, Groovy, Python
      • Compiles to JavaScript
      • Produces *READABLE* Javascript
      • Classes / Inheritance / Polymorphism
      • Groovy style loops
      • Aliases for Comparison Operators (is, isnt, not, and, or, yes/on, no/off)
      • CoffeeScript for Groovy Devs - Scott Davis - Fri @ 12:45P
  • 32. Live Coding  
  • 33. Running/Deploying the App  
  • 34. Gradle
      • Language independent build system leveraging Groovy
      • Build system for Grails, Spock, and many others
      • Uses Maven for dependency management
      • Vibrant plugin community
      • http://gradle.org
  • 35. Gradle Application Plugin
      • Requires Gradle 1.0+
      • Code: plugin 'application'
      • Provides common tasks for the application lifecycle.
      • Provides the following tasks:
        • run
        • installApp
        • distZip
  • 36. Gradle File
    • apply plugin: 'groovy'
    • apply plugin: 'application'
    • repositories {
      • flatDir name:'lib', dirs:'lib'
      • mavenCentral()
    • }
    • dependencies {
      • groovy group:'org.codehaus.groovy', name:'groovy', version:'1.8.0'
      • groovy group:'com.bleedingwolf.ratpack', name: 'Ratpack', version:'0.2-SNAPSHOT'
      • // Ratpack's other dependencies
    • }
  • 37. Gradle File (cont'd)
    • 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 = &quot;BlogApp&quot;
  • 38. Deploying App as a War  
  • 39. Gradle File Additions
    • war {
      • into('/public') {
        • from('public')
      • }
      • into('/templates') {
        • from('templates')
      • }
      • classpath fileTree('lib')
      • webXml = file('resources/web.xml')
    • }
  • 40. Web.xml File
    • <?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?>
    • <!DOCTYPE web-app
    •     PUBLIC &quot;-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN&quot;
    •     &quot;http://java.sun.com/dtd/web-app_2_3.dtd&quot;>
    • <web-app>
    • <servlet>
    •         <servlet-name>AppServlet</servlet-name>
    •         <servlet-class>AppServlet</servlet-class>
    • </servlet>
    • <servlet-mapping>
    •         <servlet-name>AppServlet</servlet-name>
    •         <url-pattern>/*</url-pattern>
    • </servlet-mapping>
    • </web-app>
  • 41. Questions ?  
  • 42. Links
    • MarkdownJ - http://markdownj.org
    • Ratpack - http://github.com/bleedingwolf/Ratpack
    • Gradle - http://gradle.org
    • Demos - http://github.com/jwill/ConferenceDemos/strangeloop