groovy on google app engine with gaelyk

36
Groovy on Google App Engine with Gaelyk

Upload: kevin-ha-tan

Post on 10-Aug-2015

1.726 views

Category:

Software


1 download

TRANSCRIPT

Groovy on Google App Engine with Gaelyk

Google App Engine

• Platform as a Service (PaaS)

• For Python, and Java (sandboxed JVM, customed Jetty servlet container)

What’s Good about GAE?

• No OS image

• Scaling aspect being handled by Google eg database/session replication, loading balancing etc.

Problem

• Grails app can’t be deployed to GAE as is, because of the proprietary components of GAE.

What is Gaelyk

• Lightweight Groovy toolkit for App Engine Java

Why Gaelyk?

• Groovy programming language goodness, eg dynamic features

• Gradle compliance as of version 2.0

• Layout templating and includes

• Caching mechanism

Pros

• It runs on Google App Engine

• Cold startup time is faster

• Lightweight, not meant to be a full blown web framework

Cons

• Locked down to one single cloud platform without being able to move out since made specifically for GAE

• No generation of views and controllers for admin interface (you have to programmatically generate it yourself)

Project Template

• https://gaelyk.appspot.com/download

How it works

• Groovy files act as scripts instead of classes, except for POGOs (plain old Groovy objects)

Project Structure Overview

View

• .gtpl extension, very similar to JSPs or PHP

View• Example <html>

<body>

<p><%def message = "Hello World!" print message %>

</p> <p><%= message %></p> <p>${message}</p> <ul> <% 3.times { %>

<li>${message}</li> <% } %>

</ul>

</body>

</html>

View• Output <html>

<body>

<p>Hello World!</p>

<p>Hello World!</p>

<p>Hello World!</p>

<ul>

<li>Hello World!</li>

<li>Hello World!</li>

<li>Hello World!</li>

</ul>

</body>

</html>

View• Markup Builder

Write Groovy scripts instead of full blown servlets html.html { body {

[1, 2, 3, 4].each { number ‐> p number }

def now = new Date()

p now

}

}

Controller

• Simple Groovy scripts in WEB-INF/groovy/

• Script based, not class based.

Tips on Groovy Scripts• Injected variables

datastore, memcache, urlFetch etc. (GAE variables)

request, response, context etc. (Eager variables)

and more… Check out the Gaelyk documentation…

URL Mapping/Routing

• /webapp/WEB-INF/routes.groovy

URL Mapping/Routing

get "/article/@year/@month/@day/@title", forward: "/article.groovy?year=@year&month=@month&day=@day&title=@title", validate: { year ==~ /\d{4}/ && month ==~ /\d{2}/ && day ==~ /\d{2}/ }

URL Mapping/Routing• Capability-aware Routing

eg get "/update", forward: { to "/update.groovy"

to("/maintenance.gtpl").on(DATASTORE) .not(ENABLED) to(“/readonly.gtpl") .on(DATASTORE_WRITE).not(ENABLED)}

URL Mapping/RoutingBLOBSTORE DATASTORE DATASTORE_WRITE IMAGES MAIL MEMCACHE TASKQUEUE URL_FETCH XMPP

URL Mapping/Routing

• Namespace scope routing

Model@ToString

@Entity(unindexed = false)

class Person {

String name

@Unindexed String tagline

String email

}

CRUD

• @Entity annotation gives you basic CRUD operations for FREE!

def person = new Person(name:’kevintan’, email:’[email protected]’, tagline:’Android Developer’)

person.save()

Datastore• App engine data store

def entity = new Entity(‘person’)

entity.name = ‘kevintan’

entity.email = ‘[email protected]

entity.save()

Datastore

• For debugging purposes

• http://localhost:8080/_ah/admin/datastore

Response Rendering• JSON rendering by default

json(person: person, message: ‘whateverlah’)

(person is an object)

More info : http://vladimir.orany.cz/everyday%20gaelyk/2013/03/17/everyday-gaelyk-simplify-groovlets-flow-by-handling-its-return-value/

Caching• Defined in routes.groovy (URL mapping file)

• Duration of caching is defined in each requests

• memcache

eg:

get "/news", forward: "/new.groovy", cache: 10.minutes

Email Support

mail.send from: "app‐admin‐[email protected]", to: "[email protected]", subject: "Hello", textBody: "Hello, how are you doing? ‐‐ MrG", attachment: [data: "Chapter 1, Chapter 2".bytes, fileName: "outline.txt"]

Simple Plugin SystemPlugin descriptors in /WEB-INF/plugins/descriptor.groovy

• provide additional groovlets and templates

• contribute new URL routes

• define and bind new variables in the binding (the "global" variables available in groovlets and templates)

• provide any kind of static content, such as JavaScript, HTML, images, etc.

• add new libraries (ie. additional JARs)

• and more generally, let you do any initialization at the startup of your application

Bootstrap some data

• By using /WEB-INF/plugins/sampledescriptor.groovy

• Register descriptor using /WEB-INF/plugins.groovy

install sampledescriptor

Running & Deploying

• ./gradlew tasks (to see available app engine related tasks)

• ./gradlew appEngineRun (to run locally, accessible with http://localhost:8080)

• ./gradlew appEngineStartBackend to deploy to production

DEMO

Sample Project Reference

• https://github.com/glaforge/bloogaey

• https://github.com/kevintanhongann/GaelykSampleApp

Join Groovy User Group Malaysia in Facebook or Google+ for more knowledge sharing goodness!

Thank you!+KevinTanHongAnn (Google+)

@s1lv3rd3m0n (Twitter)