Gretty Light-Weight Web Services Framework

Looking for a quicker prototype framework to do some demos for my clients, i received this tutorial from IBM Developerworks. This framework is much smaller than grails and does not support a full MVC stack, but my goodness, it really does show the power of groovy as an alternative JVM language and the new features in groovy++.

Since any missing jars can be resolved with the built-in Grape feature, the groovy source code can declare the need for jars using the @Grab annotation. There is no need for any other downloads, jars or installs to make gretty work. Hurrah !

For a quick test, you will need an installed version of Groovy 1.8.0 or later plus an internet connection so grape can download missing jar files. Here is an HTML template with the view described as :

<h1>index.gpptl code</h1>
  <p>I/P Address is ${addr}</p>
  <p>Host name is ${hname}</p>
  <p>Canonical name is ${cname}</p>

We can save this template as index.gpptl, then below is the code to create a server on port 8080. I saved this code in a file called grettytest.groovy in the same directory as the above index.gpptl file. Then from a terminal, type groovy grettytest.groovy and wait for the server to start.

RedApple:jim /Volumes/Data/dev/gretty $ groovy grettytest.groovy 
Aug 22, 2011 11:27:34 AM org.mbte.gretty.AbstractServer
INFO: Started server on localhost/
Aug 22, 2011 11:27:45 AM org.mbte.gretty.httpserver.template.GrettyTemplateEngine
INFO: Trying to compile /Volumes/Data/dev/gretty/index.gpptl

Go to a browser and try http://localhost:8080/fred and if all your components are properly installed, you should see a display similar to below.

Snapshot of Display using Gretty Server and index.gpptl Template
Snap of Display using Gretty Server and a Template
import org.mbte.gretty.httpserver.* 


def addr = InetAddress.localHost.hostAddress
def hname = InetAddress.localHost.hostName
def cname =InetAddress.localHost.canonicalHostName

GrettyServer server = []
server.groovy =
    localAddress: new InetSocketAddress("localhost", 8080),
        response.redirect "/"
   		response.html = template("index.gpptl", [message: "Hello ${request.parameters['name']}",addr:"${addr}",hname:"${hname}",cname:"${cname}"    ])
        } // end of get
    } // end of


The grape feature in groovy++ uses the @GrabResolver to pull down needed dependencies from a web URL address. The @Grab annotation triggers the download of org.mbte.groovypp.gretty version 0.4.279 jars and we also need to declare an import for the gretty http server as @Grab also updates the JVM classpath to include the new arrival. Tidy !

The file name of our html template is the first parameter to the ‘template()’ method. Notice in the code how a binding map is fed to the template that builds the response.html. The first map entry ${request.parameters[‘name’]} pulls the text value bound to the incoming  request property ‘name’ to feed the ‘message’ variable in the template. This one gets it’s value, ‘Fred’ in the above example,  from the request parameter ‘name’. Look closely at the address bar in the screenshot. You’ll see that /fred follows the 8080 address. So ‘fred’ fills the ‘name’ request parameter which is then used to populate the ‘message’ variable in the template thus giving us “Hello fred”. Try replacing fred with your name to see the results.

Two other aspects i particularly like are 1) there is no need to restart the server for each/any source code updates as gretty places a continual watch on all files under it’s directory, so all we need is to click the browser refresh button after a source code or configuration code change, and voila, we have live results – great for heavy prototyping and 2) the ability to declare browser bar address attributes like “/:name”.

UPDATE: Ok, changed my mind on the first of those aspects as my google chrome browser is too smart and changes made in source DO NOT update real-time. I had to ctrl-C to kill the gretty server, then restart it. But even so, it’s rather quick to do so no big overhead in the development cycle :}

Further examples of gretty as a server, jackson for JSON consumption and also groovy++ can be found here.

One thought on “Gretty Light-Weight Web Services Framework

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s