public
Created — forked from robi42/rest.scala

Basic RESTful service with Finagle

  • Download Gist
rest.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
class Respond extends Service[HttpRequest, HttpResponse] {
def apply(httpRequest: HttpRequest) = {
try {
val request = Request(httpRequest)
request.method -> Path(request.path) match {
case GET -> Root / "todos" => {
Future value Responses.json(Todos.allAsJson)
}
case GET -> Root / "todos" / id => {
val todo = Todos get id
Future value Responses.json(todo.toJson)
}
case POST -> Root / "todos" => {
val content = request.getContent.toString(UTF_8)
val todo = Todos.fromJson(content, create = true)
Future value Responses.json(todo.toJson)
}
case PUT -> Root / "todos" / id => {
val content = request.getContent.toString(UTF_8)
val todo = Todos.fromJson(content, update = true)
Future value Responses.json(todo.toJson)
}
case DELETE -> Root / "todos" / id => {
Todos remove id
Future value Responses.status(OK)
}
case _ =>
Future value Responses.status(NOT_FOUND)
}
} catch {
case e: NoSuchElement => Future value Responses.status(NOT_FOUND)
case e: Exception => {
Future value Responses.error(e.getMessage)
throw e
}
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.