Skip to content

Instantly share code, notes, and snippets.

@sumew
Last active December 19, 2017 23:38
Show Gist options
  • Save sumew/8a8844c5911e990cf5b9224c57bdc861 to your computer and use it in GitHub Desktop.
Save sumew/8a8844c5911e990cf5b9224c57bdc861 to your computer and use it in GitHub Desktop.
object WebServer {
def main(args: Array[String]) {
implicit val system = ActorSystem("my-system")
implicit val materializer = ActorMaterializer()
implicit val executionContext = system.dispatcher
var counter = 0
val route = post { counter +=1
complete(s"Counter incremented to $counter")
} ~ get { complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, s"counter is $counter"))
}
val bindingFuture = Http().bindAndHandle(route, "localhost", 9999)
println(s"Server online at http://localhost:9999/\nPress RETURN to stop...")
StdIn.readLine()
bindingFuture.flatMap(_.unbind()).onComplete(_ => system.terminate())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment