Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hayasshi/akka-http-router
A simple library for route definition of akka-http.
https://github.com/hayasshi/akka-http-router
akka-http scala
Last synced: about 2 months ago
JSON representation
A simple library for route definition of akka-http.
- Host: GitHub
- URL: https://github.com/hayasshi/akka-http-router
- Owner: hayasshi
- License: apache-2.0
- Created: 2017-02-19T12:32:40.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2023-06-10T17:21:32.000Z (over 1 year ago)
- Last Synced: 2023-08-12T19:51:07.912Z (over 1 year ago)
- Topics: akka-http, scala
- Language: Scala
- Homepage:
- Size: 64.5 KB
- Stars: 20
- Watchers: 1
- Forks: 5
- Open Issues: 42
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# akka-http-router
```scala
import akkahttp_router._import akka.http.scaladsl.model.HttpMethods._
import akka.http.scaladsl.server.Directives._val controller = new ControllerYouDefined(???)
val categoryId = LongNumber
val productId = LongNumberval router = Router(
route(GET, "category", controller.getCategories),
route(POST, "category", controller.postCategory),route(GET, "category" / categoryId, controller.getCategory),
route(GET, "category" / categoryId / "products", controller.getProducts),
route(POST, "category" / categoryId / "products", controller.postProduct),route(GET, "category" / categoryId / "products" / productId, controller.getProduct)
)
val v1api = pathPrefix("v1")(router.route)Http().bindAndHandle(v1api, "localhost", 8080)
```## Motivation
The [akka-http](http://doc.akka.io/docs/akka-http/current/index.html) is a great tool kit for building to web service interface!
However, I do not want to deeply nest route definitions, like this:
```scala
val route = pathPrefix("v1") {
path("category") {
get {
???
} ~
post {
???
}
} ~
path("category" / LongNumber) { cid =>
get {
???
} ~
path("products") {
get {
???
} ~
post {
???
} ~
} ~
path("products" / LongNumber) { pid =>
???
}
}
}
```And, `Directives` create more nested. :-(
I think this route definition is contained two problems.
- Bad visibility.
- To become fat `Route`.This tool separates route definition and application logic like the [PlayFramework's router](https://www.playframework.com/documentation/2.5.x/ScalaRouting).
## Installation
```scala
resolvers += Resolver.sonatypeRepo("releases")libraryDependencies += "com.github.hayasshi" %% "akka-http-router" % "0.5.1"
```## Usage
Define a function matching the number of URL path parameters.
```scala
val getCategories: Route = ???
val getProducts: Long => Route = ???
val getProduct: ((Long, Long)) => Route = ???
```And defined.
```scala
import akkahttp_router._import akka.http.scaladsl.model.HttpMethods._
import akka.http.scaladsl.server.Directives._val router = Router(
route(GET, "category", getCategories),route(GET, "category" / LongNumber / "products", getProducts),
route(GET, "category" / LongNumber / "products" / LongNumber, getProduct)
)
```