=doc add explanation about Route => Flow conversion

This commit is contained in:
Johannes Rudolph 2015-07-13 14:40:09 +02:00
parent ab38c36cba
commit f9b8125333
3 changed files with 14 additions and 2 deletions

View file

@ -119,6 +119,7 @@ class HttpServerExampleSpec extends WordSpec with Matchers {
} }
} }
// `route` will be implicitly converted to `Flow` using `RouteResult.route2HandlerFlow`
Http().bindAndHandle(route, "localhost", 8080) Http().bindAndHandle(route, "localhost", 8080)
} }

View file

@ -15,7 +15,9 @@ service definition as DRY_ as you might like.
As an alternative Akka HTTP provides a flexible DSL for expressing your service behavior as a structure of As an alternative Akka HTTP provides a flexible DSL for expressing your service behavior as a structure of
composable elements (called :ref:`Directives`) in a concise and readable way. Directives are assembled into a so called composable elements (called :ref:`Directives`) in a concise and readable way. Directives are assembled into a so called
*route structure* which, at its top-level, forms a handler ``Flow`` (or, alternatively, an async handler function) that *route structure* which, at its top-level, forms a handler ``Flow`` (or, alternatively, an async handler function) that
can be directly supplied to a ``bind`` call. can be directly supplied to a ``bind`` call. The conversion from ``Route`` to flow can either be invoked explicitly
using ``Route.handlerFlow`` or, otherwise, the conversion is also provided implicitly by
``RouteResult.route2HandlerFlow`` [1]_.
For example, the service definition from above, written using the routing DSL, would look like this: For example, the service definition from above, written using the routing DSL, would look like this:
@ -37,5 +39,12 @@ regard.
For learning how to work with the Routing DSL you should first understand the concept of :ref:`Routes`. For learning how to work with the Routing DSL you should first understand the concept of :ref:`Routes`.
.. [1] To be picked up automatically, the implicit conversion needs to be provided in the companion object of the source
type. However, as ``Route`` is just a type alias for ``RequestContext => Future[RouteResult]``, there's no
companion object for ``Route``. Fortunately, the `implicit scope`_ for finding an implicit conversion also
includes all types that are "associated with any part" of the source type which in this case means that the
implicit conversion will also be picked up from ``RouteResult.route2HandlerFlow`` automatically.
.. _Unfiltered: http://unfiltered.databinder.net/ .. _Unfiltered: http://unfiltered.databinder.net/
.. _DRY: http://en.wikipedia.org/wiki/Don%27t_repeat_yourself .. _DRY: http://en.wikipedia.org/wiki/Don%27t_repeat_yourself
.. _implicit scope: http://www.scala-lang.org/files/archive/spec/2.11/07-implicits.html#implicit-parameters

View file

@ -31,6 +31,8 @@ object Route {
/** /**
* Turns a `Route` into a server flow. * Turns a `Route` into a server flow.
*
* This conversion is also implicitly available through [[RouteResult.route2HandlerFlow]].
*/ */
def handlerFlow(route: Route)(implicit setup: RoutingSetup): Flow[HttpRequest, HttpResponse, Unit] = def handlerFlow(route: Route)(implicit setup: RoutingSetup): Flow[HttpRequest, HttpResponse, Unit] =
Flow[HttpRequest].mapAsync(1)(asyncHandler(route)) Flow[HttpRequest].mapAsync(1)(asyncHandler(route))