Skip to content

Instantly share code, notes, and snippets.

@johanandren
Created August 3, 2016 15:10
Show Gist options
  • Select an option

  • Save johanandren/1d16f4cbf717b49284bb39a87bf42ac3 to your computer and use it in GitHub Desktop.

Select an option

Save johanandren/1d16f4cbf717b49284bb39a87bf42ac3 to your computer and use it in GitHub Desktop.
import akka.actor.{Actor, ActorSystem, Props}
import akka.http.scaladsl.Http
import akka.http.scaladsl.marshalling._
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer
import http.ActorPerRequest.RequestHandler.Handle
import scala.io.StdIn
object ActorPerRequest extends App {
object RequestHandler {
case class Handle(complete: String => Unit)
}
class RequestHandler extends Actor {
def receive = {
case Handle(complete) =>
complete("ok")
context.stop(self)
}
}
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
val route =
pathEndOrSingleSlash {
get {
completeWith(implicitly[ToResponseMarshaller[String]]) { f =>
system.actorOf(Props[RequestHandler]) ! RequestHandler.Handle(f)
}
}
}
Http().bindAndHandle(route, "localhost", 8080)
println("ENTER to terminate")
StdIn.readLine()
system.terminate()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment