51
01

Rapid web application development with Groovy & Ratpack for GGX 2015

Embed Size (px)

Citation preview

Page 1: Rapid web application development with Groovy & Ratpack for GGX 2015

01

Page 2: Rapid web application development with Groovy & Ratpack for GGX 2015

About me02

Page 3: Rapid web application development with Groovy & Ratpack for GGX 2015

Andrey AdamovichBio: Developer, coach, speaker, author

Company: Aestas/IT (http://aestasit.com)

E­mail: [email protected]

Linkedin: http://www.linkedin.com/in/andreyadamovich

Lanyrd: http://lanyrd.com/profile/andrey­adamovich

GitHub: https://github.com/aadamovich

SO: http://stackoverflow.com/users/162792/andrey­adamovich

Twitter: @codingandrey, @aestasit

••••••••

03

Page 4: Rapid web application development with Groovy & Ratpack for GGX 2015

Quick Start!04

Page 5: Rapid web application development with Groovy & Ratpack for GGX 2015

0. PrerequisitesInstall Java 8+

Install Groovy 2.+

(Optionally) install Gradle 2+ (or just use Gradle Wrapper)

•••

05

Page 6: Rapid web application development with Groovy & Ratpack for GGX 2015

1. Type in...@Grab("org.slf4j:slf4j‐simple:1.7.10")

@Grab("io.ratpack:ratpack‐groovy:1.1.1")

import static ratpack.groovy.Groovy.ratpack

01.

02.

03.

06

Page 7: Rapid web application development with Groovy & Ratpack for GGX 2015

1. Continue...ratpack {

  handlers {

    get {

      response.send new Date().toString()

    }

  }

}

01.

02.

03.

04.

05.

06.

07.

07

Page 8: Rapid web application development with Groovy & Ratpack for GGX 2015

2. Save as...ratpack.groovy01.

08

Page 9: Rapid web application development with Groovy & Ratpack for GGX 2015

3. Start!groovy ratpack.groovy 01.

09

Page 10: Rapid web application development with Groovy & Ratpack for GGX 2015

4. Enable some more loggingJAVA_OPTS=‐Dgroovy.grape.report.downloads=true01.

10

Page 11: Rapid web application development with Groovy & Ratpack for GGX 2015

Ratpack facts IRatpack is a toolset that combines several Java libraries that allows

efficiently developing performing and testable HTTP applications.“11

Page 12: Rapid web application development with Groovy & Ratpack for GGX 2015

Ratpack facts IIInspired by Sinatra framework

Requires Java 8

Does not require a container

Does not implement Servlet API

Goes under Apache 2.0 License

•••••

12

Page 13: Rapid web application development with Groovy & Ratpack for GGX 2015

Ratpack facts IIICore is very minimal and is only based on few simple abstractions

(Handler and Registry)

... and few complex abstractions (Promise)

Many additional modules exist and it's easy to develop new ones

Modules are injected through DI (there is no specialized plugin

system)

Out­of­the­box integration with Guice and Spring

•••

13

Page 14: Rapid web application development with Groovy & Ratpack for GGX 2015

Stack

14

Page 15: Rapid web application development with Groovy & Ratpack for GGX 2015

It's alive andvery active!15

Page 16: Rapid web application development with Groovy & Ratpack for GGX 2015

Release history0.5.2 ­ Jul 21, 2012

0.6.1 ­ Nov 29, 2012

0.9.0 ­ Jan 02, 2014

0.9.1 ­ Feb 01, 2014

0.9.2 ­ Mar 01, 2014

0.9.3 ­ Apr 01, 2014

0.9.4 ­ May 01, 2014

0.9.5 ­ Jun 01, 2014

0.9.6 ­ Jul 01, 2014

••••••••• 16

Page 17: Rapid web application development with Groovy & Ratpack for GGX 2015

Release history0.9.7 ­ Aug 01, 2014

0.9.8 ­ Sep 01, 2014

0.9.9 ­ Oct 01, 2014

0.9.10 ­ Nov 02, 2014

0.9.11 ­ Dec 01, 2014

0.9.12 ­ Jan 01, 2014

0.9.13 ­ Feb 01, 2015

0.9.14 ­ Mar 01, 2015

••••••••

17

Page 18: Rapid web application development with Groovy & Ratpack for GGX 2015

Release history0.9.15 ­ Apr 01, 2015

0.9.16 ­ May 02, 2015

0.9.17 ­ June 01, 2015

0.9.17 ­ June 02, 2015

0.9.18 ­ July 02, 2015

0.9.19 ­ Aug 02, 2015

1.0.0 ­ Sep 15, 2015

1.1.0 ­ Oct 19, 2015

1.1.1 ­ Oct 21, 2015

••••••••• 18

Page 19: Rapid web application development with Groovy & Ratpack for GGX 2015

Commit history

19

Page 20: Rapid web application development with Groovy & Ratpack for GGX 2015

Statistics

20

Page 21: Rapid web application development with Groovy & Ratpack for GGX 2015

Team

21

Page 22: Rapid web application development with Groovy & Ratpack for GGX 2015

Top contributor

22

Page 23: Rapid web application development with Groovy & Ratpack for GGX 2015

Modules IAsynch: reactor, rx

Authentication: pac4j

Build/Packaging: gradle

Common: config, session

Database: h2, hikari

•••••

23

Page 24: Rapid web application development with Groovy & Ratpack for GGX 2015

Modules IIDependency Injection: guice, spring­boot

JSON: jackson

Language: groovy, kotlin

Reliability: hystrix, codehale­metrics, newrelic

Templates: handlebars, thymeleaf, groovy

Testing: test, groovy­test

••••••

24

Page 25: Rapid web application development with Groovy & Ratpack for GGX 2015

IDE supportIntelliJ IDEA recommended

Eclipse has poor support for Groovy, @DelegatesTo and functional

interfaces

NetBeans ­ haven't even tried

••

25

Page 26: Rapid web application development with Groovy & Ratpack for GGX 2015

Diving deeper26

Page 27: Rapid web application development with Groovy & Ratpack for GGX 2015

HandlersAll request processing is done via composition of  Handler s.

Each  Handler  in the  Chain  is asked to respond to a  Request  until

one actually does.

••

27

Page 28: Rapid web application development with Groovy & Ratpack for GGX 2015

A handler canSend a  Response  based on the  Request .

Delegate to the next  Handler  in the  Chain .

Insert  Handler s into the  Chain  and immediately delegate to them.

Change  Context , which represents the current state of the

Request  processing.

••••

28

Page 29: Rapid web application development with Groovy & Ratpack for GGX 2015

Flow

29

Page 30: Rapid web application development with Groovy & Ratpack for GGX 2015

Pathsprefix('api') {

  get('user/:id') {

    render getUser(pathTokens.id)

  }

  get('friends') {

    render getFriendList()

  }

}

01.

02.

03.

04.

05.

06.

07.

08.

30

Page 31: Rapid web application development with Groovy & Ratpack for GGX 2015

Verbspath('user') {

  byMethod {

    get { ... }

    post { ... }

    put { ... }

    delete { ... }

  }

}

01.

02.

03.

04.

05.

06.

07.

08.

31

Page 32: Rapid web application development with Groovy & Ratpack for GGX 2015

Content typespath('user') {

  byContent {

    json { ... }

    xml { ... }

    type("application/vnd.app.org+json;v=1") {

      ...

    }

  }

}

01.

02.

03.

04.

05.

06.

07.

08.

09. 32

Page 33: Rapid web application development with Groovy & Ratpack for GGX 2015

Static contentfiles { 

  dir "public" 

  file "index.html"

}

01.

02.

03.

04.

33

Page 34: Rapid web application development with Groovy & Ratpack for GGX 2015

Templates (ala JSP/GSP)Handler code:

get ("admin") {

  render groovyTemplate('admin.html', result: "")

}

01.

02.

03.

34

Page 35: Rapid web application development with Groovy & Ratpack for GGX 2015

Templates (ala JSP/GSP)admin.html :

<% if (model?.result) { %>

  <% model.result.each { %>

    <li>${it}</li>

  <% } %>

<% } %>

01.

02.

03.

04.

05.

35

Page 36: Rapid web application development with Groovy & Ratpack for GGX 2015

Templates (Groovy­way)Handler code:

render groovyMarkupTemplate(

  "update.gtpl", 

  "title: "Update Book",

  ...

  "price": book.price

)

01.

02.

03.

04.

05.

06.

36

Page 37: Rapid web application development with Groovy & Ratpack for GGX 2015

Templates (Groovy­way)update.gtpl :

layout 'layout.gtpl',

title: title,

msg: msg,

bodyContents: contents {

  h1('Update Book')

  includeGroovy '_book_form.gtpl'

}

01.

02.

03.

04.

05.

06.

07.

37

Page 38: Rapid web application development with Groovy & Ratpack for GGX 2015

TestingTest handler implementations with  RequestFixture

Functional testing with  ApplicationUnderTest  and

TestHttpClient

Nobody canceled testing with Geb (Selenium)!

••

38

Page 39: Rapid web application development with Groovy & Ratpack for GGX 2015

Enough!39

Page 40: Rapid web application development with Groovy & Ratpack for GGX 2015

Demo 1: Dateserver

40

Page 41: Rapid web application development with Groovy & Ratpack for GGX 2015

Demo 2:Groovy shop

41

Page 42: Rapid web application development with Groovy & Ratpack for GGX 2015

Demo 3:Garage travel

42

Page 43: Rapid web application development with Groovy & Ratpack for GGX 2015

Summary43

Page 44: Rapid web application development with Groovy & Ratpack for GGX 2015

Take­awaysRatpack can be used to quickly prototype web APIs and applications.

Learning curve is really small, you can start in seconds.

It can be used to create high performance web applications due to

non­blocking architecture.

Ratpack does not lock you in the way you implement data access,

session handling, logging, etc.

Ratpack has vibrant community and actively evolving code base.

•••

44

Page 45: Rapid web application development with Groovy & Ratpack for GGX 2015

Reading materialhttp://ratpack.io

http://www.slideshare.net/search/slideshow?q=ratpack

https://github.com/ratpack

http://alvarosanchez.github.io/ratpack­101/

••••

45

Page 46: Rapid web application development with Groovy & Ratpack for GGX 2015

Book

46

Page 47: Rapid web application development with Groovy & Ratpack for GGX 2015

Contribute!47

Page 48: Rapid web application development with Groovy & Ratpack for GGX 2015

Demo codehttps://github.com/aestasit/talks2015­ggx­rapid­webapp­development­

with­ratpack­setup.git•

48

Page 49: Rapid web application development with Groovy & Ratpack for GGX 2015

Let the forcebe with you!

49

Page 50: Rapid web application development with Groovy & Ratpack for GGX 2015

Thank you!50

Page 51: Rapid web application development with Groovy & Ratpack for GGX 2015

Questions?51