Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Explains concatenation of akka-http routes
/* Akka Http Routes
* ----------------
* Use of `concat` here:
* Tries the supplied routes in sequence, returning the result of the first route that doesn't reject the request.
*
* Eg.
* In the case of:
* GET /orders - `get(complete(StatusCodes.OK, s"GET All Orders request."))` will be executed.
*
* GET /orders/<some uuid> - `get(complete(StatusCodes.OK, s"GET request for id: $id"))` will be executed.
* GET /orders/<uuid>/price - `(get & path("price"))(complete(StatusCodes.OK, s"Price: DummyPriceValue for id: $id"))`
*
* GET /orders/count - `(get & path("count"))(complete(StatusCodes.OK, "Count: DummyCountNumber"))` will be executed.
*/
val routes: Route =
pathPrefix("orders"){
concat(
pathEndOrSingleSlash {
concat(
get(complete(StatusCodes.OK, s"GET All Orders request.")),
(post & entity(as[Order]))(order => complete(StatusCodes.OK, s"POST request to CREATE Order with id: ${order.id}"))
)
},
pathPrefix(JavaUUID){ id =>
concat(
pathEndOrSingleSlash {
concat(
get(complete(StatusCodes.OK, s"GET request for id: $id")),
delete(complete(StatusCodes.OK, s"DELETE request for id: $id")),
(put & entity(as[Order]))(_ => complete(StatusCodes.Accepted, s"PUT request for id: $id"))
)
},
(get & path("price"))(complete(StatusCodes.OK, s"Price: DummyPriceValue for id: $id"))
)
},
(get & path("count"))(complete(StatusCodes.OK, "Count: DummyCountNumber"))
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment