Created
January 20, 2013 17:43
-
-
Save capotej/4580182 to your computer and use it in GitHub Desktop.
Output on OS X from https://github.com/capotej/finatra/pull/33
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
| package com.lol.testy | |
| import com.twitter.finatra._ | |
| object App { | |
| class ExampleApp extends Controller { /** * Basic Example * * curl http://localhost:7070/hello => "hello world" */ get("/") { request => render.plain("hello world").toFuture } /** * Route parameters * * curl http://localhost:7070/user/dave => "hello dave" */ get("/user/:username") { request => val username = request.routeParams.getOrElse("username", "default_user") render.plain("hello " + username).toFuture } /** * Setting Headers * * curl -I http://localhost:7070/headers => "Foo:Bar" */ get("/headers") { request => render.plain("look at headers").header("Foo", "Bar").toFuture } /** * Rendering json * * curl -I http://localhost:7070/headers => "Foo:Bar" */ get("/data.json") { request => render.json(Map("foo" -> "bar")).toFuture } /** * Query params * * curl http://localhost:7070/search?q=foo => "no results for foo" */ get("/search") { request => request.params.get("q") match { case Some(q) => render.plain("no results for "+ q).toFuture case None => render.plain("query param q needed").status(500).toFuture } } /** * Uploading files * * curl -F avatar=@/path/to/img http://localhost:7070/profile */ post("/profile") { request => request.multiParams.get("avatar").map { avatar => println("content type is " + avatar.contentType) avatar.writeToFile("/tmp/avatar") //writes uploaded avatar to /tmp/avatar } render.plain("ok").toFuture } /** * Rendering views * * curl http://localhost:7070/posts */ class AnView extends View { val template = "an_view.mustache" val some_val = "random value here" } get("/template") { request => val anView = new AnView render.view(anView).toFuture } /** * Custom Error Handling * * curl http://localhost:7070/error */ get("/error") { request => 1234/0 render.plain("we never make it here").toFuture } /** * Custom Error Handling with custom Exception * * curl http://localhost:7070/unautorized */ class Unauthorized extends Exception get("/unauthorized") { request => throw new Unauthorized } error { request => request.error match { case Some(e:ArithmeticException) => render.status(500).plain("whoops, divide by zero!").toFuture case Some(e:Unauthorized) => render.status(401).plain("Not Authorized!").toFuture case _ => render.status(500).plain("Something went wrong!").toFuture } } /** * Custom 404s * * curl http://localhost:7070/notfound */ notFound { request => render.status(404).plain("not found yo").toFuture } } val app = new ExampleApp | |
| def main(args: Array[String]) = { | |
| FinatraServer.register(app) | |
| FinatraServer.start() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment