Skip to content

Instantly share code, notes, and snippets.

@DeaconDesperado
Created May 10, 2016 01:14
Show Gist options
  • Save DeaconDesperado/bb4100fa19ae912245e48df33ef7a0fc to your computer and use it in GitHub Desktop.
Save DeaconDesperado/bb4100fa19ae912245e48df33ef7a0fc to your computer and use it in GitHub Desktop.
A little less cake
object Main extends App {
val system = ActorSystem("my-system")
val ec = system.dispatcher
//we don't use the actor system ec here, but let's ignore that
val userService = new UserServiceImpl(ec)
val foobarService = new FoobarServiceImpl(userService)
val restActor = system.actorOf(Props(new RestActor(userService, foobarService)) "actor")
}
class RestActor(val userService:UserService, val foobarServiceFoobarService) extends Registry with UserRoutes {
val userRoutes = new UserRoutes(userService)
def receive = {
userRoutes.userRoutesDef ~
otherRoutesFromOtherMixins
}
}
trait AccessDirectives extends Directives {
//Here we still need user service, so do we self type to UserRoutes?
//Or maybe a structural self-type?
self: {val userService:UserService} =>
val letMeIn:Directive1[Option[User]] = optionalHeaderByValueName("user_auth").hflatMap {
case Some(id) :: HNil => onSuccess(userService.get(id))
case _ => reject()
}
}
class UserRoutes(val userService:UserService) with AccessDirectives {
self: Registry =>
val userRoutesDef = {
path(Segment){ userId =>
onSuccess(userService.get(userId)){ user =>
complete(user)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment