SlideShare ist ein Scribd-Unternehmen logo
1 von 26
Downloaden Sie, um offline zu lesen
Ratpack
Classy 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://jameswilliams.be

● Twitter: @ecspike

● G+: http://gplus.to/ecspike
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

● Built-in support for Groovy SimpleTemplateEngine

● Github: https://github.com/bleedingwolf/Ratpack
Ratpack vs Grails
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
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
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
Routes

get("/index") {
  // code
}

post("/login") {
  // code
}

put("/post/:id") {
  // code
}
Routes

get("/index") {
  // code
}

post("/login") {
  // code
}

put("/post/:id") { ==>   /post/3
  // code                  urlparams.id == 3
}
Demo
Authentication
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
Basic Authentication

class 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;
    }
}
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

● Source: http://github.com/jwill/Conference-Demos/user-
  groups
Blog CRUD Routes

get("/post/create") {
render '/post/create.html', [header:header]
}

get("/post/show/:id") {
def post = derby.get(urlparams.id)
render '/post/show.html', [post: post]
}
Blog CRUD Routes (cont'd)

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()
})
}
Deploying the App
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
Gradle Application Plugin

● Requires Gradle 1.0+

● Code: plugin 'application'

● Provides common tasks for the application lifecycle.

● Provides the following tasks:
   ○ run

   ○ installApp

   ○ distZip
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
}
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 = "BlogApp"
Deploying App as a War
Gradle File Additions

war {
into('/public') {
from('public')
}
into('/templates') {
from('templates')
}
classpath fileTree('lib')
webXml = file('resources/web.xml')
}
Questions ?

Weitere ähnliche Inhalte

Was ist angesagt?

Provisioning with Puppet
Provisioning with PuppetProvisioning with Puppet
Provisioning with Puppet
Joe Ray
 

Was ist angesagt? (20)

Getting started with node.js
Getting started with node.jsGetting started with node.js
Getting started with node.js
 
How to create your own hack environment
How to create your own hack environmentHow to create your own hack environment
How to create your own hack environment
 
Provisioning with Puppet
Provisioning with PuppetProvisioning with Puppet
Provisioning with Puppet
 
Building an API with Django and Django REST Framework
Building an API with Django and Django REST FrameworkBuilding an API with Django and Django REST Framework
Building an API with Django and Django REST Framework
 
Complete MVC on NodeJS
Complete MVC on NodeJSComplete MVC on NodeJS
Complete MVC on NodeJS
 
NodeJS: n00b no more
NodeJS: n00b no moreNodeJS: n00b no more
NodeJS: n00b no more
 
Node ppt
Node pptNode ppt
Node ppt
 
CasperJS
CasperJSCasperJS
CasperJS
 
Composer: Dependency Manager for PHP
Composer: Dependency Manager for PHPComposer: Dependency Manager for PHP
Composer: Dependency Manager for PHP
 
Write an API for Almost Anything: The Amazing Power and Flexibility of Django...
Write an API for Almost Anything: The Amazing Power and Flexibility of Django...Write an API for Almost Anything: The Amazing Power and Flexibility of Django...
Write an API for Almost Anything: The Amazing Power and Flexibility of Django...
 
Forget Grunt and Gulp! Webpack and NPM rule them all!
Forget Grunt and Gulp! Webpack and NPM rule them all!Forget Grunt and Gulp! Webpack and NPM rule them all!
Forget Grunt and Gulp! Webpack and NPM rule them all!
 
Webpack
Webpack Webpack
Webpack
 
Webpack: your final module bundler
Webpack: your final module bundlerWebpack: your final module bundler
Webpack: your final module bundler
 
Getting started with node JS
Getting started with node JSGetting started with node JS
Getting started with node JS
 
Superfast Automated Web Testing with CasperJS & PhantomJS
Superfast Automated Web Testing with CasperJS & PhantomJS Superfast Automated Web Testing with CasperJS & PhantomJS
Superfast Automated Web Testing with CasperJS & PhantomJS
 
Nodejs Intro - Part2 Introduction to Web Applications
Nodejs Intro - Part2 Introduction to Web ApplicationsNodejs Intro - Part2 Introduction to Web Applications
Nodejs Intro - Part2 Introduction to Web Applications
 
Vagrant plugin development intro
Vagrant plugin development introVagrant plugin development intro
Vagrant plugin development intro
 
CasperJs Enjoy Functional Testing
CasperJs Enjoy Functional TestingCasperJs Enjoy Functional Testing
CasperJs Enjoy Functional Testing
 
HTML5 tutorial: canvas, offfline & sockets
HTML5 tutorial: canvas, offfline & socketsHTML5 tutorial: canvas, offfline & sockets
HTML5 tutorial: canvas, offfline & sockets
 
Use Node.js to create a REST API
Use Node.js to create a REST APIUse Node.js to create a REST API
Use Node.js to create a REST API
 

Ähnlich wie SF Grails - Ratpack - Compact Groovy Webapps - James Williams

Ähnlich wie SF Grails - Ratpack - Compact Groovy Webapps - James Williams (20)

Vue.js + Django - configuración para desarrollo con webpack y HMR
Vue.js + Django - configuración para desarrollo con webpack y HMRVue.js + Django - configuración para desarrollo con webpack y HMR
Vue.js + Django - configuración para desarrollo con webpack y HMR
 
Dart Workshop
Dart WorkshopDart Workshop
Dart Workshop
 
Golang Project Layout and Practice
Golang Project Layout and PracticeGolang Project Layout and Practice
Golang Project Layout and Practice
 
Angular for Java Enterprise Developers: Oracle Code One 2018
Angular for Java Enterprise Developers: Oracle Code One 2018Angular for Java Enterprise Developers: Oracle Code One 2018
Angular for Java Enterprise Developers: Oracle Code One 2018
 
Pyramid Deployment and Maintenance
Pyramid Deployment and MaintenancePyramid Deployment and Maintenance
Pyramid Deployment and Maintenance
 
Catalyst MVC
Catalyst MVCCatalyst MVC
Catalyst MVC
 
Revealing ALLSTOCKER
Revealing ALLSTOCKERRevealing ALLSTOCKER
Revealing ALLSTOCKER
 
Introduction to Swagger
Introduction to SwaggerIntroduction to Swagger
Introduction to Swagger
 
How to make a high-quality Node.js app, Nikita Galkin
How to make a high-quality Node.js app, Nikita GalkinHow to make a high-quality Node.js app, Nikita Galkin
How to make a high-quality Node.js app, Nikita Galkin
 
PerlDancer for Perlers (FOSDEM 2011)
PerlDancer for Perlers (FOSDEM 2011)PerlDancer for Perlers (FOSDEM 2011)
PerlDancer for Perlers (FOSDEM 2011)
 
HTML, CSS & Javascript Architecture (extended version) - Jan Kraus
HTML, CSS & Javascript Architecture (extended version) - Jan KrausHTML, CSS & Javascript Architecture (extended version) - Jan Kraus
HTML, CSS & Javascript Architecture (extended version) - Jan Kraus
 
Laravel5 Introduction and essentials
Laravel5 Introduction and essentialsLaravel5 Introduction and essentials
Laravel5 Introduction and essentials
 
WordCamp Montreal 2016 WP-API + React with server rendering
WordCamp Montreal 2016  WP-API + React with server renderingWordCamp Montreal 2016  WP-API + React with server rendering
WordCamp Montreal 2016 WP-API + React with server rendering
 
Power your apps with Gmail, Google Drive, Calendar, Sheets, Slides & more
Power your apps with Gmail, Google Drive, Calendar, Sheets, Slides & morePower your apps with Gmail, Google Drive, Calendar, Sheets, Slides & more
Power your apps with Gmail, Google Drive, Calendar, Sheets, Slides & more
 
Introduction to angular js
Introduction to angular jsIntroduction to angular js
Introduction to angular js
 
What's New In Laravel 5
What's New In Laravel 5What's New In Laravel 5
What's New In Laravel 5
 
Charla EHU Noviembre 2014 - Desarrollo Web
Charla EHU Noviembre 2014 - Desarrollo WebCharla EHU Noviembre 2014 - Desarrollo Web
Charla EHU Noviembre 2014 - Desarrollo Web
 
Sinatra for REST services
Sinatra for REST servicesSinatra for REST services
Sinatra for REST services
 
Tornadoweb
TornadowebTornadoweb
Tornadoweb
 
10 Excellent Ways to Secure Spring Boot Applications - Okta Webinar 2020
10 Excellent Ways to Secure Spring Boot Applications - Okta Webinar 202010 Excellent Ways to Secure Spring Boot Applications - Okta Webinar 2020
10 Excellent Ways to Secure Spring Boot Applications - Okta Webinar 2020
 

Kürzlich hochgeladen

Kürzlich hochgeladen (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 

SF Grails - Ratpack - Compact Groovy Webapps - James Williams

  • 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 ● G+: http://gplus.to/ecspike
  • 3. Agenda ● What is Ratpack? ● Routes ● Templates ● Running the App ● Deploying to Servlet Containers ● Deployment ● Demos
  • 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
  • 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. 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. 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. Routes get("/index") { // code } post("/login") { // code } put("/post/:id") { // code }
  • 10. Routes get("/index") { // code } post("/login") { // code } put("/post/:id") { ==> /post/3 // code urlparams.id == 3 }
  • 11. Demo
  • 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. Basic Authentication class 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. Creating a Blog App with Ratpack
  • 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. Blog CRUD Routes get("/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. Blog CRUD Routes (cont'd) 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() }) }
  • 20. 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
  • 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. 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 }
  • 23. 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 = "BlogApp"
  • 25. Gradle File Additions war { into('/public') { from('public') } into('/templates') { from('templates') } classpath fileTree('lib') webXml = file('resources/web.xml') }