trait DefaultResourceRoutes[M <: Serializable] {
this: HttpService with Actor =>
def resourceRoutes(resourceName: String)(implicit um: FromRequestUnmarshaller[M]) = path(resourceName) {
get {
complete(s"$resourceName-getAll")
} ~
post {
entity(as[M]) { entity =>
complete(s"$resourceName-create")
}
} ~
patch {
entity(as[M]) { entity =>
complete(s"$resourceName-update")
}
}
} ~
path(resourceName / LongNumber) { entityId =>
get {
complete(s"$resourceName-get")
}
} ~
path(resourceName / "name" / Segment) { namePattern =>
get {
complete(s"$resourceName-getByName")
}
}
}
object Boot extends App {
implicit val system = ActorSystem("API")
/* Use Akka to create our Spray Service */
val router= system.actorOf(Props[Router], "api-router")
/* and bind to Akka's I/O interface */
IO(Http) ! Http.Bind(router, "0.0.0.0", 8888)
}
class Router extends HttpServiceActor with ApiService with ActorLogging {
def receive = runRoute(fooRoutes ~ barRoutes)
}
trait ApiService extends HttpService with FooService with BarService
trait FooService extends HttpService with Actor with DefaultResourceRoutes[Foo] {
import Json4sProtocol._
def fooRoutes = resourceRoutes("foo")
}
trait BarService extends HttpService with Actor with DefaultResourceRoutes[Bar] {
import Json4sProtocol._
def barRoutes = resourceRoutes("bar")
}
object Json4sProtocol extends Json4sSupport {
override implicit def json4sFormats: Formats = DefaultFormats
}
[error] src/main/scala/Boot.scala:19: illegal inheritance;[error] class Router inherits different type instances of trait DefaultResourceRoutes:[error] DefaultResourceRoutes[Bar] and DefaultResourceRoutes[Foo][error] class Router extends HttpServiceActor with ApiService with ActorLogging {[error] ^[error] src/main/scala/Boot.scala:23: illegal inheritance;[error] trait ApiService inherits different type instances of trait DefaultResourceRoutes:[error] DefaultResourceRoutes[Bar] and DefaultResourceRoutes[Foo][error] trait ApiService extends FooService with BarService[error] ^[error] two errors found[error] (compile:compileIncremental) Compilation failed[error] Total time: 4 s, completed 28-Aug-2015 12:15:04 PM