akka/akka — pull request #15609 by jrudolph (2014-07-31 13:56:53+00:00)
Title: Preview of current state of spray-routing ported to streams

**PREVIEW**

This already works somewhat. State of all the ported directive tests:

```
Tests: succeeded 290, failed 0, canceled 0, ignored 0, pending 45
```

Most notably, the (un)marshalling infrastructure is only stubbed and not yet merged with @sirthias upcoming work. That means that Parameter and FormField directives are mostly not yet working.

Changes from spray:

Route is now `RequestContext ⇒ RouteResult` with `RouteResult` being:

```scala
sealed trait RouteResult
case class CompleteWith(response: HttpResponse) extends RouteResult
case class RouteException(exception: Throwable) extends RouteResult
case class Rejected(rejections: List[Rejection]) extends RouteResult
case class DeferredResult(result: Future[RouteResult]) extends RouteResult
```

`HttpService` is now basically defined as

```scala
object HttpService {
def handleConnections(serverConn: Future[Http.ServerBinding], materializer: FlowMaterializer)(route: Route)(implicit eh: ExceptionHandler, rh: RejectionHandler, rs: RoutingSettings, ec: ExecutionContext): Unit = //...
}
```

with a basic example server looking like this (see `TestServerWithRouting`):

```scala
val bindingFuture = (IO(Http) ? Http.Bind(interface = "localhost", port = 8080)).mapTo[Http.ServerBinding]

import Directives._

HttpService.handleConnections(bindingFuture, materializer) {
get {
path("") {
complete(index)
} ~
path("ping") {
complete("PONG!")
} ~
path("crash") {
complete(sys.error("BOOM!"))
}
}
}
```

Open questions:
* Many directives now need an `ExecutionContext` and/or a `FlowMaterializer` because of the pervasive use of streams in HttpEntity and Futures for marshallers. In the current (= spray) design this means we need lots of magnets to be able to provide those implicitly. This quickly becomes unwieldy. There are these possible alternatives:
- current version that uses magnets everywhere
- enforce that Directives can only be built by implementing / mixing-in a context trait that supplies those implicit values
- supply those context variables through the `RequestContext`
* It seems `ChunkingDirectives` are not as necessary as before because you can always depend on the granularity of the ByteStrings in the HttpEntity data streams to provide basic "chunking", i.e. breaking down the size of byte blocks. Depending on the data source you maybe would still want to "rechunk" but it seems to be much less common than before where chunking was a preliminary to support gzip encoding of files.