Skip to content

Instantly share code, notes, and snippets.

View spinscale's full-sized avatar
💻

Alexander Reelsen spinscale

💻
View GitHub Profile
@spinscale
spinscale / gist:4713671
Created February 5, 2013 10:44
spark-groovy-jrebel-formats
def jsonClosure = { Request request, Response response ->
if (request.contentType() == 'application/json')
json([name: request.attribute('name')])
}
def xmlClosure = { Request request, Response response ->
if (request.contentType() == 'application/xml')
'<name>' + request.attribute('name') + '</name>\n'
}
@spinscale
spinscale / gist:4713664
Created February 5, 2013 10:43
spark-groovy-jrebel-closure-chain
def authCheck = { Request request, Response response ->
if (request.session().attribute('userLogin') != request.params(':name')) {
halt(401, "No permissions to check for ${request.params(":name")}\n")
}
}
get "/greet/:name", authCheck, { Request request, Response response ->
return "Hello " + request.params('name') + "\n"
}
def get(String path, Closure ... closures) {
spark.Spark.get(createClosureBasedRouteForPath(path, closures))
}
private Route createClosureBasedRouteForPath(String path, Closure ... closures) {
new Route(path) {
def handle(Request request, Response response) {
closures*.delegate = this
return closures*.call(request, response).findAll { it }.join()
}
static JadeConfiguration config = new JadeConfiguration()
static {
config.setTemplateLoader(new FileTemplateLoader("src/main/resources/views/", "UTF-8"))
}
def json(Object obj) {
return JSON.default.toJSON(obj)
}
def jade(String template, Object obj) {
@spinscale
spinscale / gist:4713646
Created February 5, 2013 10:40
spark-groovy-jrebel-formats
get "/foo.json", { Request request, Response response ->
json([foo: 'bar'])
}
get "/jade", { Request request, Response response ->
jade "test.jade", [foo: 'bar', pageName: 'My page']
}
@spinscale
spinscale / gist:4713643
Created February 5, 2013 10:40
spark-groovy-jrebel-first-closure-get
def get(String path, Closure closure) {
Spark.get(new Route(path) {
def handle(Request request, Response response) {
closure.delegate = this
return closure.call(request, response)
}
})
}
@spinscale
spinscale / gist:4713635
Created February 5, 2013 10:38
spark-groovy-jrebel-simpler
import spark.*
class WebServer extends SparkGroovy {
static void main(String[] args) {
new WebServer().init();
}
void init() {
before { Request req, Response res ->
@spinscale
spinscale / gist:4713627
Created February 5, 2013 10:37
spark-groovy-jrebel-build.gradle
apply plugin: 'groovy'
apply plugin: 'idea'
repositories {
mavenCentral()
maven { url "http://www.sparkjava.com/nexus/content/repositories/spark/" }
maven { url "https://raw.github.com/neuland/jade4j/master/releases" }
}
// avoid groovy/jade asm clashes
@spinscale
spinscale / gist:4713622
Created February 5, 2013 10:36
spark-groovy-jrebel-create-project
mkdir groovy-spark
cd groovy-spark
mkdir -p src/main/groovy
mkdir -p src/main/resources
mkdir -p src/test/groovy
mkdir -p src/test/resources
@spinscale
spinscale / gist:4713618
Created February 5, 2013 10:35
spark-groovy-jrebel-helloworld-java
import static spark.Spark.*;
import spark.*;
public class HelloWorld {
public static void main(String[] args) {
before(new Filter() { // matches all routes
@Override
public void handle(Request request, Response response) {
boolean authenticated;