Created
March 13, 2020 14:59
-
-
Save vi-kas/7b43deb7980f2561ce5c22d926f31573 to your computer and use it in GitHub Desktop.
Explains concatenation of akka-http routes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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