Routes

The “Route” is the central concept of Akka HTTP’s Routing DSL. All the structures you build with the DSL, no matter whether they consists of a single line or span several hundred lines, are typefunction turning a RequestContextRequestContext into a Future[RouteResult]CompletionStage<RouteResult>.

type Route = RequestContext => Future[RouteResult]

It’s a simple alias for a function turning a RequestContextRequestContext into a Future[RouteResult].

A RouteRoute itself is a function that operates on a RequestContextRequestContext and returns a RouteResultRouteResult. The RequestContextRequestContext is a data structure that contains the current request and auxiliary data like the so far unmatched path of the request URI that gets passed through the route structure. It also contains the current ExecutionContext and akka.stream.Materializer, so that these don’t have to be passed around manually.

Generally when a route receives a request (or rather a RequestContextRequestContext for it) it can do one of these things:

  • Complete the request by returning the value of requestContext.complete(...)
  • Reject the request by returning the value of requestContext.reject(...) (see Rejections)
  • Fail the request by returning the value of requestContext.fail(...) or by just throwing an exception (see Exception Handling)
  • Do any kind of asynchronous processing and instantly return a Future[RouteResult]CompletionStage<RouteResult> to be eventually completed later

The first case is pretty clear, by calling complete a given response is sent to the client as reaction to the request. In the second case “reject” means that the route does not want to handle the request. You’ll see further down in the section about route composition what this is good for.

A RouteRoute can be “sealed” using Route.seal, which relies on the in-scope RejectionHandler and ExceptionHandlerExceptionHandler instances to convert rejections and exceptions into appropriate HTTP responses for the client. Sealing a Route is described more in detail later.

Using Route.handlerFlow or Route.asyncHandler a RouteRoute can be lifted into a handler FlowFlow or async handler function to be used with a bindAndHandleXXX call from the Core Server API.

Note: There is also an implicit conversion from RouteRoute to Flow<HttpRequest, HttpResponse, Unit>Flow[HttpRequest, HttpResponse, Unit] defined in the RouteResultRouteResult companion, which relies on Route.handlerFlow.

RequestContext

The request context wraps an HttpRequestHttpRequest instance to enrich it with additional information that are typically required by the routing logic, like an ExecutionContext, MaterializerMaterializer, LoggingAdapterLoggingAdapter and the configured RoutingSettingsRoutingSettings. It also contains the unmatchedPath, a value that describes how much of the request URI has not yet been matched by a Path Directive.

The RequestContextRequestContext itself is immutable but contains several helper methods which allow for convenient creation of modified copies.

RouteResult

RouteResultRouteResult is a simple abstract data type (ADT) that models the possible non-error results of a RouteRoute. It is defined as such:

sealed trait RouteResult

object RouteResult {
  final case class Complete(response: HttpResponse) extends RouteResult
  final case class Rejected(rejections: immutable.Seq[Rejection]) extends RouteResult
}

Usually you don’t create any RouteResultRouteResult instances yourself, but rather rely on the pre-defined RouteDirectives (like complete, reject or redirect) or the respective methods on the RequestContext instead.

Composing Routes

There are three basic operations we need for building more complex routes from simpler ones:

  • Route transformation, which delegates processing to another, “inner” route but in the process changes some properties of either the incoming request, the outgoing response or both
  • Route filtering, which only lets requests satisfying a given filter condition pass and rejects all others
  • Route chaining, which tries a second route if a given first one was rejected

The last point is achieved with the concatenation operator ~, which is an extension method that becomes available when you import akka.http.scaladsl.server.Directives._. The first two points are provided by so-called Directives of which a large number is already predefined by Akka HTTP and which you can also easily create yourself. Directives deliver most of Akka HTTP’s power and flexibility.

The Routing Tree

Essentially, when you combine directives and custom routes via nesting and the ~ operatoralternative, you build a routing structure that forms a tree. When a request comes in it is injected into this tree at the root and flows down through all the branches in a depth-first manner until either some node completes it or it is fully rejected.

Consider this schematic example:

val route =
  a {
    b {
      c {
        ... // route 1
      } ~
      d {
        ... // route 2
      } ~
      ... // route 3
    } ~
    e {
      ... // route 4
    }
  }
import static akka.http.javadsl.server.Directives.*;

Route route =
  directiveA(route(() ->
    directiveB(route(() ->
      directiveC(
        ... // route 1
      ),
      directiveD(
        ... // route 2
      ),
      ... // route 3
    )),
    directiveE(
      ... // route 4
    )
  ));

Here five directives form a routing tree.

  • Route 1 will only be reached if directives a, b and c all let the request pass through.
  • Route 2 will run if a and b pass, c rejects and d passes.
  • Route 3 will run if a and b pass, but c and d reject.

Route 3 can therefore be seen as a “catch-all” route that only kicks in, if routes chained into preceding positions reject. This mechanism can make complex filtering logic quite easy to implement: simply put the most specific cases up front and the most general cases in the back.

Sealing a Route

As described in Rejections and Exception Handling, there are generally two ways to handle rejections and exceptions.

In the first case your handlers will be “sealed”, (which means that it will receive the default handler as a fallback for all cases your handler doesn’t handle itself) and used for all rejections/exceptions that are not handled within the route structure itself.

Route.seal() method to modify HttpResponse

In application code, unlike test code, you don’t need to use the Route.seal() method to seal a route. As long as you bring implicit rejection and/or exception handlers to the top-level scope, your route is sealed.

However, you can use Route.seal() to perform modification on HttpResponse from the route. For example, if you want to add a special header, but still use the default rejection handler, then you can do the following. In the below case, the special header is added to rejected responses which did not match the route, as well as successful responses which matched the route.

Scala
val route = respondWithHeader(RawHeader("special-header", "you always have this even in 404")) {
  Route.seal(
    get {
      pathSingleSlash {
        complete {
          "Captain on the bridge!"
        }
      }
    }
  )
}
Java
public class RouteSealExample extends AllDirectives {

  public static void main(String [] args) throws IOException {
    RouteSealExample app = new RouteSealExample();
    app.runServer();
  }

  public void runServer(){
    ActorSystem system = ActorSystem.create();
    final ActorMaterializer materializer = ActorMaterializer.create(system);

    Route sealedRoute = get(
      () -> pathSingleSlash( () ->
        complete("Captain on the bridge!")
      )
    ).seal(system, materializer);

    Route route = respondWithHeader(
      RawHeader.create("special-header", "you always have this even in 404"),
      () -> sealedRoute
    );

    final Http http = Http.get(system);
    final Flow<HttpRequest, HttpResponse, NotUsed> routeFlow = route.flow(system, materializer);
    final CompletionStage<ServerBinding> binding = http.bindAndHandle(routeFlow, ConnectHttp.toHost("localhost", 8080), materializer);
  }
}
The source code for this page can be found here.