This document provides an overview and introduction to Akka HTTP, a Scala library built on Akka Streams for HTTP-based applications. Some key points:
- Akka HTTP uses Akka Streams to model HTTP requests and responses as streaming data flows.
- It allows building both HTTP clients and servers by composing stream processing stages together.
- Common directives and operations like routing, marshalling, validation, and testing are supported through a high-level API.
- Examples demonstrate basic usage like creating a route that returns XML, running a server, and writing tests against routes.
4. PROJET EN COURS DE MATURATION
version lors de cette présentation2.0.1
mélange entre Spray et Akka
J'ai eu l'idée de cette présentation à la version 1.0, il était temps !
5. AKKA STREAMS
Présenté au SLUG par en février 2015Frédéric Masion
Source ~> Flow ~> Sink
Source décrit une source de données
Flow représente une transformation de ces données
Sink une opération terminale
11. C'EST BON, MONTRE-MOI LE CODE
Les exemples sont pour la plupart tirés de la documentation
d'Akka
12. EXEMPLE SIMPLE
import akka.http.scaladsl.Http
import akka.http.scaladsl.marshallers.xml.ScalaXmlSupport._
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer
object Main extends App {
implicit val system = ActorSystem("my-system")
implicit val materializer = ActorMaterializer()
implicit val ec = system.dispatcher
val route =
path("hello") {
get {
complete {
<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...")
Console.readLine() // for the future transformations
bindingFuture
.flatMap(_.unbind()) // trigger unbinding from the port
.onComplete(_ ⇒ system.shutdown()) // and shutdown when done
}
13. EXEMPLE SIMPLE
import akka.http.scaladsl.Http
import akka.http.scaladsl.marshallers.xml.ScalaXmlSupport._
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer
object Main extends App {
implicit val system = ActorSystem("my-system")
implicit val materializer = ActorMaterializer()
implicit val ec = system.dispatcher
val route =
path("hello") {
get {
complete {
<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...")
Console.readLine() // for the future transformations
bindingFuture
.flatMap(_.unbind()) // trigger unbinding from the port
.onComplete(_ ⇒ system.shutdown()) // and shutdown when done
}
14. EXEMPLE SIMPLE
import akka.http.scaladsl.Http
import akka.http.scaladsl.marshallers.xml.ScalaXmlSupport._
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer
object Main extends App {
implicit val system = ActorSystem("my-system")
implicit val materializer = ActorMaterializer()
implicit val ec = system.dispatcher
val route =
path("hello") {
get {
complete {
<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...")
Console.readLine() // for the future transformations
bindingFuture
.flatMap(_.unbind()) // trigger unbinding from the port
.onComplete(_ ⇒ system.shutdown()) // and shutdown when done
}
15. EXEMPLE SIMPLE
import akka.http.scaladsl.Http
import akka.http.scaladsl.marshallers.xml.ScalaXmlSupport._
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer
object Main extends App {
implicit val system = ActorSystem("my-system")
implicit val materializer = ActorMaterializer()
implicit val ec = system.dispatcher
val route =
path("hello") {
get {
complete {
<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...")
Console.readLine() // for the future transformations
bindingFuture
.flatMap(_.unbind()) // trigger unbinding from the port
.onComplete(_ ⇒ system.shutdown()) // and shutdown when done
}
27. EXEMPLE
val route =
path("order" / IntNumber) { id =>
(get | put) {
extractMethod { m =>
complete(s"Received ${m.name} request for order $id")
}
}
}
28. EXEMPLE 2
val orderGetOrPutWithMethod =
path("order" / IntNumber) & (get | put) & extractMethod
val route = orderGetOrPutWithMethod { (id, m) =>
complete(s"Received ${m.name} request for order $id")
}
30. EXTRACTION CASE CLASS
case class Color(red: Int, green: Int, blue: Int)
val route = path("color") {
parameters('red.as[Int], 'green.as[Int], 'blue.as[Int]).as(Color)
{
color =>
// utiliser color
}
}
31. VALIDATION CASE CLASS
case class Color(name: String, red: Int, green: Int, blue: Int) {
require(!name.isEmpty, "color name must not be empty")
require(0 <= red && red <= 255, "red color component must be betwe
en 0 and 255")
require(0 <= green && green <= 255, "green color component must be
between 0 and 255")
require(0 <= blue && blue <= 255, "blue color component must be be
tween 0 and 255")
}
ValidationRejection si require ne passe pas
Par défaut : 400 Bad Request
33. Pour la route suivante
val smallRoute =
get {
pathSingleSlash {
complete {
"Hello World !"
}
} ~
path("ping") {
complete("PONG !")
}
}
Test
"Hello World ! non renvoyé sur /" in {
Get() ~> smallRoute ~> check {
status === StatusCodes.OK
responseAs[String] shouldEqual "Hello World !"
}
}
34. Pour la même route ...
val smallRoute =
get {
pathSingleSlash {
complete {
"Hello World !"
}
} ~
path("ping") {
complete("PONG !")
}
}
Test
"GET sur chemin inconnu non pris en compte" in {
Get("/pouet") ~> smallRoute ~> check {
handled shouldBe false
}
}