Skip to content
This repository has been archived by the owner on Feb 19, 2021. It is now read-only.

Commit

Permalink
refactor to simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
kailuowang committed Oct 6, 2016
1 parent 72a8624 commit bf498d3
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 129 deletions.
Expand Up @@ -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 "/"
Expand All @@ -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
)

Expand Down
Expand Up @@ -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
)(
Expand Down Expand Up @@ -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
Expand All @@ -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])
}
}
}
46 changes: 10 additions & 36 deletions distributed/src/main/scala/asobu/distributed/service/Action.scala
Expand Up @@ -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
Expand All @@ -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 }

Expand Down
18 changes: 8 additions & 10 deletions distributed/src/main/scala/asobu/distributed/service/Syntax.scala
Expand Up @@ -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
Expand Down
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Expand Up @@ -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)
Expand Down
Expand Up @@ -27,13 +27,14 @@ class EndpointsRouterUpdaterSpec extends SpecWithActorCluster {
become { case _ }
})

EndpointDefSimple(
EndpointDefinition(
Prefix("/"),
HttpVerb(verb),
pathOf(pathParts),
"call",
handler.path,
"test",
None,
version
)
}
Expand Down

0 comments on commit bf498d3

Please sign in to comment.