...
 
Commits (2)
*.class
.idea
target/
lazy val akkaHttpVersion = "10.1.7"
lazy val akkaHttpCirceVersion = "1.25.2"
lazy val akkaVersion = "2.5.20"
lazy val root = (project in file(".")).settings(
inThisBuild(
List(
organization := "de.codecentric",
scalaVersion := "2.12.8"
)),
name := "hello-world",
libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-http" % akkaHttpVersion,
"de.heikoseeberger" %% "akka-http-circe" % akkaHttpCirceVersion,
"com.typesafe.akka" %% "akka-http-xml" % akkaHttpVersion,
"com.typesafe.akka" %% "akka-stream" % akkaVersion,
"com.typesafe.akka" %% "akka-http-testkit" % akkaHttpVersion % Test,
"com.typesafe.akka" %% "akka-testkit" % akkaVersion % Test,
"com.typesafe.akka" %% "akka-stream-testkit" % akkaVersion % Test,
"org.scalatest" %% "scalatest" % "3.0.5" % Test
),
scalafmtOnCompile := true,
fork in Test := true
)
addSbtPlugin("com.lucidchart" % "sbt-scalafmt" % "1.15")
addSbtPlugin("io.spray" % "sbt-revolver" % "0.9.1")
package de.codecentric
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Route
import akka.stream.ActorMaterializer
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, ExecutionContext, Future}
import scala.util.{Failure, Success}
object Main extends App with PartyRoutes {
implicit val system: ActorSystem = ActorSystem("helloworld")
implicit val materializer: ActorMaterializer = ActorMaterializer()
implicit val executionContext: ExecutionContext = system.dispatcher
lazy val routes: Route = partyRoutes
val serverBinding: Future[Http.ServerBinding] =
Http().bindAndHandle(routes, "localhost", 8080)
serverBinding.onComplete {
case Success(bound) =>
println(s"Listening on port: ${bound.localAddress.getPort}")
case Failure(e) =>
Console.err.println(s"Server could not start!")
e.printStackTrace()
system.terminate()
}
Await.result(system.whenTerminated, Duration.Inf)
}
package de.codecentric
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import akka.http.scaladsl.server.directives.PathDirectives.path
import akka.http.scaladsl.server.directives.RouteDirectives.complete
import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport
trait PartyRoutes extends FailFastCirceSupport {
lazy val partyRoutes: Route =
path("hello") {
pathEndOrSingleSlash {
get {
complete("Hello, World!")
}
}
}
}
package de.codecentric
import akka.http.scaladsl.model._
import akka.http.scaladsl.testkit.ScalatestRouteTest
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.{Matchers, WordSpec}
class PartyRoutesSpec
extends WordSpec
with Matchers
with ScalaFutures
with ScalatestRouteTest
with PartyRoutes {
lazy val routes = partyRoutes
classOf[PartyRoutes].getSimpleName should {
"return hello world" in {
val request = HttpRequest(uri = "/hello")
request ~> partyRoutes ~> check {
status should ===(StatusCodes.OK)
contentType should ===(ContentTypes.`application/json`)
entityAs[String] should ===("Hello, World!")
}
}
}
}