Posts‎ > ‎

A Groovy Web Server to serve static and Groovlet-generated content

posted May 24, 2014, 5:02 AM by Renato Athaydes   [ updated May 24, 2014, 5:28 AM ]
Groovy makes creating a lot of things extremely easy! For example, you can create a GUI application or a library to "talk" to your DB with just a few lines of Groovy code.

Another thing that is really easy to do with Groovy is create your own HTTP server to provide both static content (mapping URLs to files in your file system) or code-generated pages using Groovlets.
But unfortunately, most tutorials you'll find on the Internet to show you how to do this are quite out of date.

So, unless you're reading this much later than May 2014, here's how you achieve that with Groovy and one of the latest versions of Jetty:

webServer.groovy

#!/usr/bin/env groovy

import org.eclipse.jetty.server.Server
import org.eclipse.jetty.servlet.*
import groovy.servlet.*
 
@Grab(group='org.eclipse.jetty.aggregate', module='jetty-all', version='7.6.15.v20140411')
def startJetty() {
def server = new Server(8080)
 
def handler = new ServletContextHandler(ServletContextHandler.SESSIONS)
handler.contextPath = '/'
handler.resourceBase = '.'
handler.addServlet(GroovyServlet, '/scripts/*')
def filesHolder = handler.addServlet(DefaultServlet, '/')
filesHolder.setInitParameter('resourceBase', './public')
 
server.handler = handler
server.start()
}
 
println "Starting Jetty, press Ctrl+C to stop."
startJetty()



As Groovy 2.3.1 (latest as of writing) still uses version 2.4 of the servlet-api, we can't use the latest version of Jetty yet (as it already used 3.+ which has incompatible changes)...

This script will start an embedded Jetty server that will serve static content from directory ./public and execute Groovlets located in directory ./scripts.
You can find more information about how to use Jetty as an embedded server in the Jetty docs.

If you haven't seen a Groovlet before, here's a simple example:

scripts/simpleGroovlet.groovy

if (!session) {
session = request.getSession(true);
}
 
if (!session.counter) {
session.counter = 1
}
 
html.html {
head {
title 'Simple Groovlet'
}
body {
h1 'Welcome to my Groovlet'
p "The current time at this server is ${new Date()}"
p "The session counter is now at ${sesson.counter}"
br()
p "System properties:"
ul {
for ( prop in System.properties.keySet()) {
li "$prop: ${System.getProperty(prop)}"
}
}
}
}
 
session.counter += 1


As you can see, from a Groovlet, you have access to certain variables such as request, session, and html (an instance of MarkupBuilder) which are really handy! For a complete list, check the Groovlet documentation.

And that's it. To run your server, just type:

./webServer.groovy

You may need to make the script executable first in Linux:

chmod +x ./webServer.groovy

If your system does not support the Groovy shebang, just do:

groovy webServer.groovy

Now, you should be able to see your ./public directory listing (just add some files to it so you can see something) on:

http://localhost:8080

And your Groovlet-generated page on:

http://localhost:8080/scripts/simpleGroovlet.groovy

Have fun!

Comments