Skip to content

Instantly share code, notes, and snippets.

@fmamud
Last active November 4, 2020 14:42
Show Gist options
  • Save fmamud/20607540ed5cb3a4b783714a4f427539 to your computer and use it in GitHub Desktop.
Save fmamud/20607540ed5cb3a4b783714a4f427539 to your computer and use it in GitHub Desktop.
Ratpack simple example that serves first json file found in current directory
@Grab('io.ratpack:ratpack-groovy:1.8.0')
import static ratpack.groovy.Groovy.ratpack
import static ratpack.jackson.Jackson.json
import groovy.json.JsonSlurper
def parser = new JsonSlurper()
ratpack {
handlers {
get {
def file = new File('.').listFiles().find { it.name.endsWith('.json') }
def text = file ? file.text : '{}'
render json(parser.parseText(text))
}
post("event-types") {
parse(jsonNode())
.then { n ->
println(n.toString())
render n.toString()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment