Skip to content

Instantly share code, notes, and snippets.

@soheilhy
Created July 9, 2012 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soheilhy/3077008 to your computer and use it in GitHub Desktop.
Save soheilhy/3077008 to your computer and use it in GitHub Desktop.
A generic routing service
class RoutingService[REQUEST <: Request, RESPONSE](
val routes: PartialFunction[Request, Service[REQUEST, RESPONSE]])
extends Service[REQUEST, RESPONSE] {
protected[this] val requestToService = routes
def apply(request: REQUEST): Future[RESPONSE] = {
val service = requestToService(request)
service(request)
}
}
object MyRoutingService {
def byRequest[REQUEST, RESPONSE](routes: PartialFunction[Request, Service[REQUEST, RESPONSE]]) =
new RoutingService(
new PartialFunction[Request, Service[REQUEST, RESPONSE]] {
def apply(request: Request) = routes(request)
def isDefinedAt(request: Request) = routes.isDefinedAt(request)
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment