Skip to content

Instantly share code, notes, and snippets.

@ataylor284
Created November 16, 2010 19:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ataylor284/702337 to your computer and use it in GitHub Desktop.
Save ataylor284/702337 to your computer and use it in GitHub Desktop.
inspired by gist: 675667 -- expose any groovy object over the web
import com.sun.net.httpserver.*
Object.metaClass.webapp = {
{ path ->
try {
def attrs = path.split('/')[1..-1]
[200, owner.delegate.invokeMethod(attrs.head(), attrs.tail() as Object[]) as String]
} catch (Exception e) {
[500, e as String]
}
}
}
def runWebApp(app, port = 9292) {
HttpServer server = HttpServer.create(new InetSocketAddress(port),0)
server.createContext('/', { HttpExchange exchange ->
def (code, body) = app.call(exchange.requestURI.path)
exchange.sendResponseHeaders(code,0);
exchange.responseBody.write(body.bytes)
exchange.responseBody.close();
} as HttpHandler)
server.start();
}
runWebApp([].webapp())
// ^^^^^^^^^^^
// | (x)
// ROFLSCALE DB ---/
//
// http://localhost:9292/push/1 -> true
// http://localhost:9292/push/2 -> true
// http://localhost:9292/push/3 -> true
// http://localhost:9292/toString -> [1, 2, 3]
// http://localhost:9292/pop -> 3
// http://localhost:9292/shift ... no shift in groovy :(
@dnoseda
Copy link

dnoseda commented Apr 17, 2012

cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment