- import akka.actor.ActorSystem
- import akka.http.scaladsl.Http
- import akka.stream.ActorMaterializer
- import akka.Done
- import akka.http.scaladsl.server.Route
- import akka.http.scaladsl.server.Directives._
- import akka.http.scaladsl.model.StatusCodes
- import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
- import spray.json.DefaultJsonProtocol._
-
- import scala.io.StdIn
-
- import scala.concurrent.Future
-
- object WebServer {
-
- // domain model
- final case class Item(name: String, id: Long)
- final case class Order(items: List[Item])
-
- // formats for unmarshalling and marshalling
- implicit val itemFormat = jsonFormat2(Item)
- implicit val orderFormat = jsonFormat1(Order)
-
- // (fake) async database query api
- def fetchItem(itemId: Long): Future[Option[Item]] = ???
- def saveOrder(order: Order): Future[Done] = ???
-
- def main(args: Array[String]) {
-
- // needed to run the route
- implicit val system = ActorSystem()
- implicit val materializer = ActorMaterializer()
- // needed for the future map/flatmap in the end
- implicit val executionContext = system.dispatcher
-
- val route: Route =
- get {
- pathPrefix("item" / LongNumber) { id =>
- // there might be no item for a given id
- val maybeItem: Future[Option[Item]] = fetchItem(id)
-
- onSuccess(maybeItem) {
- case Some(item) => complete(item)
- case None => complete(StatusCodes.NotFound)
- }
- }
- } ~
- post {
- path("create-order") {
- entity(as[Order]) { order =>
- val saved: Future[Done] = saveOrder(order)
- onComplete(saved) { done =>
- complete("order created")
- }
- }
- }
- }
-
- val bindingFuture = Http().bindAndHandle(route, "localhost", 8080)
- println(s"Server online at http://localhost:8080/\nPress RETURN to stop...")
- StdIn.readLine() // let it run until user presses return
- bindingFuture
- .flatMap(_.unbind()) // trigger unbinding from the port
- .onComplete(_ ⇒ system.terminate()) // and shutdown when done
-
- }
- }
- }
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.