|
@@ -1,5 +1,6 @@
|
|
package com.typesafe.akka.http.benchmark.handlers
|
|
package com.typesafe.akka.http.benchmark.handlers
|
|
|
|
|
|
|
|
+import akka.http.scaladsl.marshalling.{Marshaller, ToEntityMarshaller}
|
|
import akka.http.scaladsl.model.HttpCharsets._
|
|
import akka.http.scaladsl.model.HttpCharsets._
|
|
import akka.http.scaladsl.model.MediaTypes._
|
|
import akka.http.scaladsl.model.MediaTypes._
|
|
import akka.http.scaladsl.model._
|
|
import akka.http.scaladsl.model._
|
|
@@ -8,21 +9,24 @@ import akka.http.scaladsl.server.Route
|
|
import com.typesafe.akka.http.benchmark.Infrastructure
|
|
import com.typesafe.akka.http.benchmark.Infrastructure
|
|
import com.typesafe.akka.http.benchmark.Templating
|
|
import com.typesafe.akka.http.benchmark.Templating
|
|
import com.typesafe.akka.http.benchmark.datastore.DataStore
|
|
import com.typesafe.akka.http.benchmark.datastore.DataStore
|
|
-
|
|
|
|
-import scala.concurrent.Future
|
|
|
|
|
|
+import com.typesafe.akka.http.benchmark.entity.Fortune
|
|
|
|
|
|
trait FortunesHandler { _: Infrastructure with DataStore with Templating =>
|
|
trait FortunesHandler { _: Infrastructure with DataStore with Templating =>
|
|
|
|
+
|
|
def fortunesEndpoint: Route =
|
|
def fortunesEndpoint: Route =
|
|
get {
|
|
get {
|
|
path("fortunes") {
|
|
path("fortunes") {
|
|
- complete(response)
|
|
|
|
|
|
+ onSuccess(getFortunes)(complete(_))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- def response: Future[HttpResponse] =
|
|
|
|
- getFortunes.map {
|
|
|
|
- fortunes =>
|
|
|
|
- val body = layout("/templates/fortunes.mustache", Map("fortunes" -> fortunes))
|
|
|
|
- HttpResponse(StatusCodes.OK, entity = HttpEntity(body).withContentType(`text/html`.withCharset(`UTF-8`)))
|
|
|
|
|
|
+ private implicit lazy val fortunesMarshaller: ToEntityMarshaller[Seq[Fortune]] = {
|
|
|
|
+ val fortunesTemplate = templateEngine.load("/templates/fortunes.mustache")
|
|
|
|
+ Marshaller.opaque { fortunes =>
|
|
|
|
+ HttpEntity(
|
|
|
|
+ contentType = `text/html`.withCharset(`UTF-8`),
|
|
|
|
+ string = templateEngine.layout("", fortunesTemplate, Map("fortunes" -> fortunes))
|
|
|
|
+ )
|
|
}
|
|
}
|
|
|
|
+ }
|
|
}
|
|
}
|