groovy on google app engine with gaelyk
TRANSCRIPT
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.
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)
How it works
• Groovy files act as scripts instead of classes, except for POGOs (plain old Groovy objects)
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
}
}
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
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
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
Sample Project Reference
• https://github.com/glaforge/bloogaey
• https://github.com/kevintanhongann/GaelykSampleApp