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 1 month 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 (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-08-12T21:18:01.000Z (11 months ago)
- Last Synced: 2025-04-15T01:41:08.764Z (3 months ago)
- Topics: akka-http, scala
- Language: Scala
- Homepage:
- Size: 64.5 KB
- Stars: 19
- Watchers: 2
- 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)
)
```