Akka:Http
Contents
- Akka:Http (template edit)
- Akka:Http:RoutingDSL
- Akka:Http:Directives
- Marshalling Directives
Extensions
There are several third party libraries that expand the functionality of Akka Http.
Among those, we want to highlight the following:
- akka-http-json: Integrate some of the best JSON libs in Scala with Akka HTTP
- Swakka: A Scala library for creating Swagger definitions in a type-safe fashion wth Akka-Http. Generates Open API (a.k.a. Swagger) from code
- Guardrail: Guardrail is a code generation tool, capable of reading from OpenAPI/Swagger specification files and generating Akka HTTP code
- akka-http-cors: Akka Http directives implementing the CORS specifications defined by W3C
- akka-http-session: Web & mobile client-side akka-http sessions, with optional JWT support
- sttp: Library that provides a clean, programmer-friendly API to define HTTP requests and execute them using one of the wrapped backends, akka-http among them.
Using Akka HTTP
sbt설정은 아래와 같다.
"com.typesafe.akka" %% "akka-http" % "10.1.7"
"com.typesafe.akka" %% "akka-stream" % "2.5.19" // or whatever the latest version is
Alternatively, you can bootstrap a new sbt project with Akka HTTP already configured using the Giter8 template:
Example
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer
import scala.io.StdIn
object WebServer {
def main(args: Array[String]) {
implicit val system = ActorSystem("my-system")
implicit val materializer = ActorMaterializer()
// needed for the future flatMap/onComplete in the end
implicit val executionContext = system.dispatcher
val route =
path("hello") {
get {
complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Say hello to akka-http</h1>"))
}
}
val bindingFuture = Http().bindAndHandle(route, "localhost", 8080)
println(s"Server online at http://localhost:8080/\nPress RETURN to stop...")
StdIn.readLine() // let it run until user presses return
bindingFuture
.flatMap(_.unbind()) // trigger unbinding from the port
.onComplete(_ => system.terminate()) // and shutdown when done
}
}