Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@vi-kas
Created March 13, 2020 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vi-kas/7b43deb7980f2561ce5c22d926f31573 to your computer and use it in GitHub Desktop.
Save vi-kas/7b43deb7980f2561ce5c22d926f31573 to your computer and use it in GitHub Desktop.
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