Skip to content

Instantly share code, notes, and snippets.

@nsadeh
Last active August 8, 2020 22:59
Show Gist options
  • Save nsadeh/9d403954a31d6cf9030633d7aa933677 to your computer and use it in GitHub Desktop.
Save nsadeh/9d403954a31d6cf9030633d7aa933677 to your computer and use it in GitHub Desktop.
Basic Web Server in Akka HTTP/Scala
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import akka.http.scaladsl.Http
import akka.stream.ActorMaterializer
import scala.io.StdIn
object WebServer extends App {
// implicit values required by the server machinery
implicit val actorSystem = akka.actor.ActorSystem("messaging-actorsystem")
implicit val materializer = ActorMaterializer()
implicit val executionContext = actorSystem.dispatcher
// define a basic route ("/") that returns a welcoming message
def helloRoute: Route = pathEndOrSingleSlash {
complete("Welcome to messaging service")
}
// bind the route using HTTP to the server address and port
val binding = Http().bindAndHandle(helloRoute, "localhost", 8080)
println("Server running...")
// kill the server with input
StdIn.readLine()
binding.flatMap(_.unbind()).onComplete(_ => actorSystem.terminate())
println("Server is shut down")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment