This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| get "/foo.json", { Request request, Response response -> | |
| json([foo: 'bar']) | |
| } | |
| get "/jade", { Request request, Response response -> | |
| jade "test.jade", [foo: 'bar', pageName: 'My page'] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } | |
| }) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import spark.* | |
| class WebServer extends SparkGroovy { | |
| static void main(String[] args) { | |
| new WebServer().init(); | |
| } | |
| void init() { | |
| before { Request req, Response res -> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |