diff --git a/distributed/src/main/scala/asobu/distributed/EndPointDefinition.scala b/distributed/src/main/scala/asobu/distributed/EndPointDefinition.scala index 7dc0536..8c57339 100644 --- a/distributed/src/main/scala/asobu/distributed/EndPointDefinition.scala +++ b/distributed/src/main/scala/asobu/distributed/EndPointDefinition.scala @@ -12,17 +12,17 @@ import shapeless.{HNil, HList} import scala.concurrent.ExecutionContext -/** - * Endpoint definition by the remote handler - */ -sealed trait EndpointDefinition { - def verb: HttpVerb - def path: PathPattern - def call: String - def prefix: Prefix //todo: move prefix to Endpoint set - def handlerActor: ActorPath - def clusterRole: String - def version: Option[Int] //todo: move version to Endpoint set +@SerialVersionUID(2L) +case class EndpointDefinition( + prefix: Prefix, + verb: HttpVerb, + path: PathPattern, + call: String, + handlerActor: ActorPath, + clusterRole: String, + enricherDef: Option[RequestEnricherDefinition] = None, + version: Option[Int] = None +) { val defaultPrefix: String = { if (prefix.value.endsWith("/")) "" else "/" @@ -40,65 +40,27 @@ sealed trait EndpointDefinition { val (verb, path, _) = documentation s"$verb $path" } -} - -@SerialVersionUID(2L) -case class EndpointDefSimple( - prefix: Prefix, - verb: HttpVerb, - path: PathPattern, - call: String, - handlerActor: ActorPath, - clusterRole: String, - version: Option[Int] = None -) extends EndpointDefinition -@SerialVersionUID(2L) -case class EndpointDefWithEnrichment( - prefix: Prefix, - verb: HttpVerb, - path: PathPattern, - call: String, - enricherDef: RequestEnricherDefinition, - handlerActor: ActorPath, - clusterRole: String, - version: Option[Int] = None -) extends EndpointDefinition +} object EndpointDefinition { - def apply( - prefix: Prefix, - route: Route, - handlerActor: ActorPath, - clusterRole: String, - version: Option[Int] = None - ): EndpointDefinition = - EndpointDefSimple( - prefix, - route.verb, - route.path, - route.call.toString(), - handlerActor, - clusterRole, - version - ) def apply( prefix: Prefix, route: Route, - enricherDef: RequestEnricherDefinition, handlerActor: ActorPath, clusterRole: String, + enricherDef: Option[RequestEnricherDefinition], version: Option[Int] ): EndpointDefinition = - EndpointDefWithEnrichment( + EndpointDefinition( prefix, route.verb, route.path, route.call.toString(), - enricherDef, handlerActor, clusterRole, + enricherDef, version ) diff --git a/distributed/src/main/scala/asobu/distributed/gateway/Endpoint.scala b/distributed/src/main/scala/asobu/distributed/gateway/Endpoint.scala index a3005a1..0090a53 100644 --- a/distributed/src/main/scala/asobu/distributed/gateway/Endpoint.scala +++ b/distributed/src/main/scala/asobu/distributed/gateway/Endpoint.scala @@ -32,7 +32,7 @@ trait EndpointHandler { * @param bridgeProps a factory that creates the prop for an bridge actor between * gateway router and actual handling service actor */ -sealed abstract class Endpoint( +abstract class Endpoint( val definition: EndpointDefinition, bridgeProps: HandlerBridgeProps = HandlerBridgeProps.default )( @@ -115,28 +115,6 @@ sealed abstract class Endpoint( protected val enricher: RequestEnricher } -class EndpointWithEnrichment[T <: RequestEnricherDefinition: Interpreter: ClassTag]( - definition0: EndpointDefWithEnrichment, - bridgeProps: HandlerBridgeProps = HandlerBridgeProps.default -)(implicit - arf: ActorRefFactory, - ec: ExecutionContext) extends Endpoint(definition0, bridgeProps) with EndpointRoute with EndpointHandler { - - lazy val enricher: RequestEnricher = Interpreter.interpret(definition0.enricherDef) - -} - -class EndpointSimple( - definition: EndpointDefinition, - bridgeProps: HandlerBridgeProps = HandlerBridgeProps.default -)(implicit - arf: ActorRefFactory, - ec: ExecutionContext) extends Endpoint(definition, bridgeProps) with EndpointRoute with EndpointHandler { - - val enricher: RequestEnricher = Extractor(identity) - -} - object Endpoint { @SerialVersionUID(1L) class Prefix private (val value: String) extends AnyVal @@ -162,9 +140,9 @@ object Endpoint { class EndpointFactoryImpl[T <: RequestEnricherDefinition: Interpreter: ClassTag](bridgeProps: HandlerBridgeProps)(implicit arf: ActorRefFactory, ec: ExecutionContext) extends EndpointFactory { - def apply(endpointDef: EndpointDefinition): Endpoint = endpointDef match { - case ed: EndpointDefWithEnrichment ⇒ new EndpointWithEnrichment(ed, bridgeProps) - case ed: EndpointDefSimple ⇒ new EndpointSimple(ed, bridgeProps) + def apply(endpointDef: EndpointDefinition): Endpoint = new Endpoint(endpointDef, bridgeProps) { + override protected val enricher: RequestEnricher = + endpointDef.enricherDef.fold[RequestEnricher](Extractor(identity))(Interpreter.interpret[T]) } } } diff --git a/distributed/src/main/scala/asobu/distributed/service/Action.scala b/distributed/src/main/scala/asobu/distributed/service/Action.scala index 5093352..78c3da4 100644 --- a/distributed/src/main/scala/asobu/distributed/service/Action.scala +++ b/distributed/src/main/scala/asobu/distributed/service/Action.scala @@ -28,11 +28,20 @@ trait Action { sys.actorOf(Props(new RemoteHandler).withDeploy(Deploy.local), name + "_Handler") } + def enricherDefinition: Option[RequestEnricherDefinition] + def endpointDefinition( route: Route, prefix: Prefix, version: Option[Int] - )(implicit sys: ActorSystem): EndpointDefinition + )(implicit sys: ActorSystem): EndpointDefinition = EndpointDefinition( + prefix, + route, + handlerActor().path, + Cluster(sys).selfRoles.head, + enricherDefinition, + version + ) class RemoteHandler extends Actor { import context.dispatcher @@ -55,41 +64,6 @@ trait Action { } -trait ActionWithEnricher extends Action { - def enricherDefinition: RequestEnricherDefinition - def endpointDefinition( - route: Route, - prefix: Prefix, - version: Option[Int] - )(implicit sys: ActorSystem): EndpointDefinition = { - EndpointDefinition( - prefix, - route, - enricherDefinition, - handlerActor().path, - Cluster(sys).selfRoles.head, - version - ) - } -} - -trait ActionSimple extends Action { - def endpointDefinition( - route: Route, - prefix: Prefix, - version: Option[Int] - )(implicit sys: ActorSystem): EndpointDefinition = { - EndpointDefinition( - prefix, - route, - handlerActor().path, - Cluster(sys).selfRoles.head, - version - ) - } - -} - object Action { type Aux[TMessage0] = Action { type TMessage = TMessage0 } diff --git a/distributed/src/main/scala/asobu/distributed/service/Syntax.scala b/distributed/src/main/scala/asobu/distributed/service/Syntax.scala index d3e9b11..591a1d6 100644 --- a/distributed/src/main/scala/asobu/distributed/service/Syntax.scala +++ b/distributed/src/main/scala/asobu/distributed/service/Syntax.scala @@ -32,23 +32,21 @@ trait Syntax extends ExtractorFunctions { def handle[T]( name: String, extrs: DRequestExtractor[T] - )(bk: (Headers, T) ⇒ Future[DResult]): Action.Aux[T] = { - val name0 = name - new ActionSimple { - val name = actionName(name0) - type TMessage = T - val extractor = extrs - def backend = bk - } - } + )(bk: (Headers, T) ⇒ Future[DResult]): Action.Aux[T] = handle(name, extrs, None)(bk) def handle[T]( name: String, enricherDef: RequestEnricherDefinition, extrs: DRequestExtractor[T] + )(bk: (Headers, T) ⇒ Future[DResult]): Action.Aux[T] = handle(name, extrs, Some(enricherDef))(bk) + + private def handle[T]( + name: String, + extrs: DRequestExtractor[T], + enricherDef: Option[RequestEnricherDefinition] )(bk: (Headers, T) ⇒ Future[DResult]): Action.Aux[T] = { val name0 = name - new ActionWithEnricher { + new Action { val enricherDefinition = enricherDef val name = actionName(name0) type TMessage = T diff --git a/distributed/src/test/scala/asobu/distributed/gateway/EndpointSpec.scala b/distributed/src/test/scala/asobu/distributed/gateway/EndpointSpec.scala index 155d58d..149360c 100644 --- a/distributed/src/test/scala/asobu/distributed/gateway/EndpointSpec.scala +++ b/distributed/src/test/scala/asobu/distributed/gateway/EndpointSpec.scala @@ -3,7 +3,7 @@ package asobu.distributed.gateway import akka.actor._ import asobu.distributed.gateway.enricher.DisabledInterpreter import asobu.distributed.util.{EndpointUtil, TestClusterActorSystem} -import asobu.distributed.{EndpointDefSimple, util, EndpointDefinition} +import asobu.distributed.{util, EndpointDefinition} import asobu.distributed.gateway.Endpoint.{EndpointFactory, Prefix} import asobu.distributed.service.EndpointRoutesParser import org.specs2.mock.Mockito @@ -37,7 +37,9 @@ object EndpointSpec extends PlaySpecification with Mockito { prefix, route, ActorPath.fromString("akka://my-sys/user/service-a/worker1"), - "role" + "role", + None, + None ): EndpointDefinition }) lazy val ep1: EndpointDefinition = endPoints(0) diff --git a/distributed/src/test/scala/asobu/distributed/gateway/EndpointsRouterSpec.scala b/distributed/src/test/scala/asobu/distributed/gateway/EndpointsRouterSpec.scala index ed18fb4..648d85f 100644 --- a/distributed/src/test/scala/asobu/distributed/gateway/EndpointsRouterSpec.scala +++ b/distributed/src/test/scala/asobu/distributed/gateway/EndpointsRouterSpec.scala @@ -29,7 +29,7 @@ class EndpointsRouterSpec extends SpecWithActorCluster { val worker2 = TestProbe() val createEndpointDef = (route: Route, prefix: Prefix) ⇒ { val path = if (route.path.toString.contains("ep1")) worker1.ref.path else worker2.ref.path - EndpointDefinition(prefix, route, path, role) + EndpointDefinition(prefix, route, path, role, None, None) } val endpointDefs = EndpointUtil.parseEndpoints(routeString)(createEndpointDef) diff --git a/distributed/src/test/scala/asobu/distributed/gateway/EndpointsRouterUpdaterSpec.scala b/distributed/src/test/scala/asobu/distributed/gateway/EndpointsRouterUpdaterSpec.scala index a8f1842..9ed890c 100644 --- a/distributed/src/test/scala/asobu/distributed/gateway/EndpointsRouterUpdaterSpec.scala +++ b/distributed/src/test/scala/asobu/distributed/gateway/EndpointsRouterUpdaterSpec.scala @@ -27,13 +27,14 @@ class EndpointsRouterUpdaterSpec extends SpecWithActorCluster { become { case _ ⇒ } }) - EndpointDefSimple( + EndpointDefinition( Prefix("/"), HttpVerb(verb), pathOf(pathParts), "call", handler.path, "test", + None, version ) }