Created
July 9, 2012 14:56
-
-
Save soheilhy/3077008 to your computer and use it in GitHub Desktop.
A generic routing service
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
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