Skip to content

Instantly share code, notes, and snippets.

@tomjadams
Created August 11, 2009 02:10
Show Gist options
  • Save tomjadams/165581 to your computer and use it in GitHub Desktop.
Save tomjadams/165581 to your computer and use it in GitHub Desktop.
package com.foo.http
import scapps.http.BaseApp
import scapps.http.route.OptionKleisli._
import scapps.http.route.Route._
import scalaz._
import scalaz.http.servlet.{HttpServletRequest}
import scalaz.http.response._
import scalaz.http.request._
final class MyApplication extends BaseApp {
// how URLs are routed to functions
val routes: Kleisli[Option, Request[Stream], Response[Stream]] =
List(
dir("/") >=> GET >=> root _,
dir("/api") >=> List(
dir("/register") >=> List(
GET >=> root _
),
dir("/notify") >=> List(
GET >=> root _,
POST >=> root _
)
)
)
// entry-point
def route(implicit request: Request[Stream], servletRequest: HttpServletRequest) = routes(request)
// functions that do the work... "actions" in some frameworks
def root(request: Request[Stream]): Option[Response[Stream]] = {
implicit val r = request
Some(jsonResponse("Hello, world!"))
}
def jsonResponse[T](t: T)(implicit request: Request[Stream], jsoner: JSON[T]) = {
OK(ContentType, "application/json") << t.jsonString << "\n"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment