Created
April 3, 2015 21:42
-
-
Save gamsd/a2a222d6650514a8c2c5 to your computer and use it in GitHub Desktop.
Akka HTTP extremely simple example
This file contains hidden or 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
import akka.actor.ActorSystem | |
import akka.http.Http | |
import akka.http.marshallers.sprayjson.SprayJsonSupport._ | |
import akka.http.model.StatusCodes._ | |
import akka.http.server.Directives._ | |
import akka.http.server.PathMatchers.IntNumber | |
import akka.stream.{ActorFlowMaterializer, FlowMaterializer} | |
import spray.json.{DefaultJsonProtocol, _} | |
import scala.concurrent.ExecutionContextExecutor | |
case class Whatever(id: Int, name: String) | |
trait WhateverService extends DefaultJsonProtocol { | |
implicit val whateverFormat = jsonFormat2(Whatever.apply) | |
implicit val system: ActorSystem | |
implicit def executor: ExecutionContextExecutor | |
implicit val materializer: FlowMaterializer | |
val routes = { | |
(get & path("whatever")) { complete(OK -> "Lots of whatever") } ~ | |
(get & path("whatever" / IntNumber)) { int => complete(OK -> Whatever(int, "whatever").toJson.prettyPrint) } ~ | |
(get & path("whatever" / IntNumber / "wat")) { w => complete(OK -> s"Wat for whatever $w") } ~ | |
(post & path("whatever") & entity(as[Whatever])) { w => complete(OK -> s"$w created succesfully") } ~ | |
(put & path("whatever" / IntNumber) & entity(as[Whatever])) { (id, w) => complete(OK -> s"Whatever $id updated with $w") } | |
} | |
} | |
object WhateverHttp extends App with WhateverService { | |
override implicit val system = ActorSystem() | |
override implicit val executor = system.dispatcher | |
override implicit val materializer = ActorFlowMaterializer() | |
val interface = "0.0.0.0" | |
val port = 9000 | |
Http().bindAndHandle(routes, interface, port) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment