Created
August 11, 2009 02:10
-
-
Save tomjadams/165581 to your computer and use it in GitHub Desktop.
This file contains 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.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