forked from eigengo/akka-patterns
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Now using Spray-JSON (much cleaner) but implicit conversions of futur…
…es/option causing problems
- Loading branch information
Showing
5 changed files
with
132 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.cakesolutions.akkapatterns.api | ||
|
||
import akka.actor.{ActorRef, Props} | ||
import spray._ | ||
import routing._ | ||
import http.{StatusCodes, HttpResponse} | ||
import org.cakesolutions.akkapatterns.core.Core | ||
import akka.util.Timeout | ||
|
||
trait Api { | ||
this: Core => | ||
|
||
val routes = | ||
new HomeService().route :: | ||
new CustomerService().route :: | ||
new UserService().route :: | ||
Nil | ||
|
||
def rejectionHandler: PartialFunction[scala.List[Rejection], HttpResponse] = { | ||
case (rejections: List[Rejection]) => HttpResponse(StatusCodes.BadRequest) | ||
} | ||
|
||
// FIXME: HttpService is now a trait | ||
// @see http://spray.io/documentation/spray-routing/key-concepts/big-picture/#the-httpservice | ||
// this file should probably be rewritten to use the new API | ||
val svc: Route => ActorRef = route => | ||
actorSystem.actorOf(Props(new HttpService(route, rejectionHandler))) | ||
|
||
val rootService = actorSystem.actorOf( | ||
props = Props(new RootService( | ||
svc(routes.head), | ||
routes.tail.map(svc):_* | ||
)), | ||
name = "root-service" | ||
) | ||
|
||
} | ||
|
||
trait DefaultTimeout { | ||
final implicit val timeout = Timeout(3000) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.cakesolutions.akkapatterns.api | ||
|
||
import akka.actor.ActorSystem | ||
import spray.httpx.SprayJsonSupport._ | ||
import spray.routing.Directives | ||
import org.cakesolutions.akkapatterns.domain.Customer | ||
import org.cakesolutions.akkapatterns.core.application._ | ||
import akka.pattern.ask | ||
import org.cakesolutions.akkapatterns.core.application.RegisterCustomer | ||
import org.cakesolutions.akkapatterns.domain.Customer | ||
import org.cakesolutions.akkapatterns.core.application.Get | ||
import org.cakesolutions.akkapatterns.core.application.FindAll | ||
import spray.httpx.SprayJsonSupport | ||
import spray.httpx.marshalling.MetaMarshallers | ||
|
||
/** | ||
* @author janmachacek | ||
*/ | ||
class CustomerService(implicit val actorSystem: ActorSystem) extends Directives with Marshalling with MetaMarshallers with DefaultTimeout { | ||
def customerActor = actorSystem.actorFor("/user/application/customer") | ||
|
||
val route = | ||
path("customers" / JavaUUID) { id => | ||
get { | ||
complete { | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
(customerActor ? Get(id)).mapTo[Option[Customer]] | ||
} | ||
} | ||
} ~ | ||
path("customers") { | ||
get { | ||
complete{ | ||
val bob = (customerActor ? FindAll()).mapTo[List[Customer]] | ||
bob | ||
} | ||
} ~ | ||
post { | ||
content(as[RegisterCustomer]) { rc => | ||
complete((customerActor ? rc).mapTo[Either[NotRegisteredCustomer, RegisteredCustomer]]) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.cakesolutions.akkapatterns.api | ||
|
||
import akka.actor.ActorSystem | ||
import spray.routing.Directives | ||
import java.net.InetAddress | ||
import akka.pattern.ask | ||
import org.cakesolutions.akkapatterns.core.application.{ PoisonPill, GetImplementation, Implementation } | ||
import spray.httpx.SprayJsonSupport | ||
import spray.httpx.marshalling.MetaMarshallers | ||
|
||
case class SystemInfo(implementation: Implementation, host: String) | ||
|
||
class HomeService(implicit val actorSystem: ActorSystem) extends Directives with MetaMarshallers with Marshalling with DefaultTimeout { | ||
|
||
def applicationActor = actorSystem.actorFor("/user/application") | ||
|
||
val route = { | ||
path(Slash) { | ||
get { | ||
complete { | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
val futureInfo = (applicationActor ? GetImplementation()).mapTo[Implementation].map { | ||
SystemInfo(_, InetAddress.getLocalHost.getCanonicalHostName) | ||
} | ||
// how to get this to implicitly return as a future marshaller of a SystemInfo marshaller? | ||
futureInfo | ||
} | ||
} | ||
} ~ | ||
path("poisonpill") { | ||
post { | ||
completeWith { | ||
applicationActor ! PoisonPill() | ||
|
||
"Goodbye" | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |