Advertisement
Guest User

Untitled

a guest
Mar 18th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.33 KB | None | 0 0
  1. package com.ctilogic.cvd.webtr
  2.  
  3.  
  4. import akka.pattern.{pipe, ask}
  5. import akka.actor.{Props, ActorRef, ActorLogging, Actor}
  6. import akka.util.Timeout
  7. import com.ctilogic.cvd.acl.api.REST.{AuthRequest, AuthResponse}
  8. import com.ctilogic.cvd.acl.api.REST.Marshalling._
  9. import com.ctilogic.cvd.common.ActorHelpers.ExtendedDispatchers
  10. import com.ctilogic.cvd.common.{ActorHelpers, CommonConfig}
  11. import com.ctilogic.cvd.common.http.CommonPaths
  12. import com.ctilogic.cvd.common.messaging.ErrorResponse
  13. import com.github.levkhomich.akka.tracing.{TracingSupport, ActorTracing}
  14. import com.github.levkhomich.akka.tracing.http.TracingDirectives
  15. import shapeless._
  16. import spray.http.HttpRequest
  17. import spray.httpx.marshalling._
  18. import spray.httpx.unmarshalling._
  19. import spray.routing.{Route, HttpService}
  20.  
  21. import scala.concurrent.{ExecutionContext, Future}
  22.  
  23. import com.ctilogic.cvd.acl.api.REST.Marshalling._
  24. import com.ctilogic.cvd.acl.api.REST._
  25. import com.ctilogic.cvd.common.marshalling.CommonMarshalling._
  26.  
  27.  
  28.  
  29. class StubRequestProcessor extends Actor with ActorTracing {
  30.     override def receive = {
  31.         case r: TracingSupport ⇒ sender() ! Left(ErrorResponse.ServiceUnavailable.asChildOf(r))
  32.     }
  33. }
  34.  
  35.  
  36.  
  37. class HttpRequestDispatcher
  38.         extends Actor
  39.         with ActorTracing
  40.         with HttpService
  41.         with TracingDirectives
  42.         with ActorLogging
  43.         with CommonPaths
  44.         with ActorHelpers
  45. {
  46.     override implicit def actorRefFactory = context
  47.  
  48.     type RestResult[T] = Future[Either[ErrorResponse, T]]
  49.  
  50.     implicit protected def requestTimeout: Timeout = CommonConfig.Timeouts.Background
  51.     implicit protected val executionContext: ExecutionContext = context.system.dispatchers.lookupHttpDispatcher
  52.  
  53.     /**
  54.      * Request processor actor. All requests routed to this actor by call [[restProcess]].
  55.      */
  56.     protected val requestProcessor = context.actorOf(Props[StubRequestProcessor])
  57.  
  58.  
  59.     /**
  60.      * Process rest request by `requestProcessor`
  61.      * @param request request.
  62.      * @tparam A type of request
  63.      * @tparam B type of response
  64.      * @return future with response or error.
  65.      */
  66.     protected def restProcess[A, B](request: A): RestResult[B] = {
  67.         (requestProcessor ? request).mapTo[Either[ErrorResponse, B]]
  68.     }
  69.  
  70.  
  71.     // TODO: FIXME tracing support
  72.     /**
  73.      * Handle request using [[restProcess]] with tracing.
  74.      * @param um request unmarshaller
  75.      * @param m response marshaller
  76.      * @tparam A request type
  77.      * @tparam B response type
  78.      * @return spray route which handle the request
  79.      */
  80.     def tracedHandleRest[A <: TracingSupport, B]
  81.     (implicit um: FromRequestUnmarshaller[A],   m: ToResponseMarshaller[B])
  82.     : Route = tracedHandleWith(restProcess[A, B])
  83.  
  84.     def tracedHandleWith[A <: TracingSupport, B, G <: HList](extracted: G)(f: A ⇒ B)
  85.             (implicit um: Deserializer[HttpRequest :: G, A], m: ToResponseMarshaller[B], ma: Manifest[A]): Route = {
  86.         implicit val umm = wrap(extracted)
  87.         tracedHandleWith(f)
  88.     }
  89.  
  90.     private def wrap[A, G <: HList](extracted: G)(implicit um: Deserializer[HttpRequest :: G, A]): FromRequestUnmarshaller[A] =
  91.         new Deserializer[HttpRequest, A] {
  92.             override def apply(httpRequest: HttpRequest): Deserialized[A] = {
  93.                 um(::(httpRequest, extracted))
  94.             }
  95.         }
  96.  
  97.  
  98.     override def receive = runRoute(aclRoute)
  99.  
  100.     val aclRoute =
  101.     pathPrefix("api") {
  102.         pathPrefix(AuthPath) {
  103.             (pathEnd & post) {
  104.                 tracedHandleRest[AuthRequest, AuthResponse]
  105.             }
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement