Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Boot and HTTP transport

  • Loading branch information...
commit 3ff0ef584a78fc416b126a76c2cfeb812b1d546a 1 parent bc9c586
@janm janm authored
View
1  sbt/build.sbt
@@ -63,4 +63,3 @@ testOptions := Seq(Tests.Filter(s =>
/** Console */
initialCommands in console := "import org.cakesolutions.akkapatterns._"
-EclipseKeys.withSource := true
View
22 sbt/src/main/scala/org/cakesolutions/akkapatterns/api/boot.scala
@@ -7,32 +7,18 @@ import http.{StatusCodes, HttpResponse}
import org.cakesolutions.akkapatterns.core.Core
import akka.util.Timeout
-trait Api {
+trait Api extends RouteConcatenation {
this: Core =>
val routes =
- new HomeService().route ::
- new CustomerService().route ::
- new UserService().route ::
- Nil
+ new HomeService().route ~
+ new CustomerService().route
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"
- )
+ val rootService = actorSystem.actorOf(Props(new RoutedHttpService(routes)))
}
View
4 sbt/src/main/scala/org/cakesolutions/akkapatterns/api/home.scala
@@ -23,7 +23,7 @@ class HomeService(implicit val actorSystem: ActorSystem) extends Directives with
}
}
}
- } ~
+ } /*~
path("poisonpill") {
post {
complete {
@@ -32,7 +32,7 @@ class HomeService(implicit val actorSystem: ActorSystem) extends Directives with
"Goodbye"
}
}
- }
+ }*/
}
}
View
1  sbt/src/main/scala/org/cakesolutions/akkapatterns/api/marshalling.scala
@@ -2,7 +2,6 @@ package org.cakesolutions.akkapatterns.api
import java.util.UUID
import spray.json._
-import spray.httpx.marshalling._
import spray.httpx.SprayJsonSupport
import org.cakesolutions.akkapatterns.domain._
import org.cakesolutions.akkapatterns.core.application._
View
16 sbt/src/main/scala/org/cakesolutions/akkapatterns/api/services.scala
@@ -0,0 +1,16 @@
+package org.cakesolutions.akkapatterns.api
+
+import akka.actor.Actor
+import spray.routing._
+
+/**
+ * @author janmachacek
+ */
+class RoutedHttpService(route: Route) extends Actor with HttpService {
+
+ implicit def actorRefFactory = context
+
+ def receive =
+ runRoute(route)
+
+}
View
17 sbt/src/main/scala/org/cakesolutions/akkapatterns/main/Main.scala
@@ -3,18 +3,21 @@ package org.cakesolutions.akkapatterns.main
import akka.actor.ActorSystem
import org.cakesolutions.akkapatterns.domain.Configuration
import org.cakesolutions.akkapatterns.core.Core
+import org.cakesolutions.akkapatterns.web.Web
+import org.cakesolutions.akkapatterns.api.Api
-object Main extends App {
+object Main {
- implicit val system = ActorSystem("AkkaPatterns")
+ def main(args: Array[String]) {
+ implicit val system = ActorSystem("AkkaPatterns")
- class Application(val actorSystem: ActorSystem) extends Core with Configuration {
- }
+ class Application(val actorSystem: ActorSystem) extends Core with Configuration with Api with Web
- new Application(system)
+ new Application(system)
- sys.addShutdownHook {
- system.shutdown()
+ sys.addShutdownHook {
+ system.shutdown()
+ }
}
}
View
33 sbt/src/main/scala/org/cakesolutions/akkapatterns/web/boot.scala
@@ -0,0 +1,33 @@
+package org.cakesolutions.akkapatterns.web
+
+import org.cakesolutions.akkapatterns.core.Core
+import org.cakesolutions.akkapatterns.api.Api
+import spray.io.{SingletonHandler, IOBridge}
+import spray.can.server.HttpServer
+import akka.actor.Props
+
+trait Web {
+ this: Api with Core =>
+
+ // every spray-can HttpServer (and HttpClient) needs an IOBridge for low-level network IO
+ // (but several servers and/or clients can share one)
+ val ioBridge = new IOBridge(actorSystem).start()
+
+ // create and start the spray-can HttpServer, telling it that
+ // we want requests to be handled by our singleton service actor
+ val httpServer = actorSystem.actorOf(
+ Props(new HttpServer(ioBridge, SingletonHandler(rootService))),
+ name = "http-server"
+ )
+
+ // a running HttpServer can be bound, unbound and rebound
+ // initially to need to tell it where to bind to
+ httpServer ! HttpServer.Bind("localhost", 8080)
+
+ // finally we drop the main thread but hook the shutdown of
+ // our IOBridge into the shutdown of the applications ActorSystem
+ actorSystem.registerOnTermination {
+ ioBridge.stop()
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.