akka-http-router


License

License

Categories

Categories

Akka Container Microservices Reactive libraries
GroupId

GroupId

com.github.hayasshi
ArtifactId

ArtifactId

akka-http-router_2.11
Last Version

Last Version

0.5.1
Release Date

Release Date

Type

Type

jar
Description

Description

akka-http-router
akka-http-router
Project URL

Project URL

https://github.com/hayasshi/akka-http-router
Project Organization

Project Organization

com.github.hayasshi
Source Code Management

Source Code Management

https://github.com/hayasshi/akka-http-router

Download akka-http-router_2.11

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.hayasshi/akka-http-router_2.11/ -->
<dependency>
    <groupId>com.github.hayasshi</groupId>
    <artifactId>akka-http-router_2.11</artifactId>
    <version>0.5.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.hayasshi/akka-http-router_2.11/
implementation 'com.github.hayasshi:akka-http-router_2.11:0.5.1'
// https://jarcasting.com/artifacts/com.github.hayasshi/akka-http-router_2.11/
implementation ("com.github.hayasshi:akka-http-router_2.11:0.5.1")
'com.github.hayasshi:akka-http-router_2.11:jar:0.5.1'
<dependency org="com.github.hayasshi" name="akka-http-router_2.11" rev="0.5.1">
  <artifact name="akka-http-router_2.11" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.hayasshi', module='akka-http-router_2.11', version='0.5.1')
)
libraryDependencies += "com.github.hayasshi" % "akka-http-router_2.11" % "0.5.1"
[com.github.hayasshi/akka-http-router_2.11 "0.5.1"]

Dependencies

compile (1)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.11.12

provided (2)

Group / Artifact Type Version
com.typesafe.akka : akka-stream_2.11 jar 2.5.27
com.typesafe.akka : akka-http_2.11 jar 10.1.11

test (3)

Group / Artifact Type Version
org.scalatest : scalatest_2.11 jar 3.1.0
com.typesafe.akka : akka-stream-testkit_2.11 jar 2.5.27
com.typesafe.akka : akka-http-testkit_2.11 jar 10.1.11

Project Modules

There are no modules declared in this project.

akka-http-router

import akkahttp_router._

import akka.http.scaladsl.model.HttpMethods._
import akka.http.scaladsl.server.Directives._

val controller = new ControllerYouDefined(???)

val categoryId = LongNumber
val productId = LongNumber

val 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 is a great tool kit for building to web service interface!

However, I do not want to deeply nest route definitions, like this:

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.

Installation

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.

val getCategories: Route = ???
val getProducts: Long => Route = ???
val getProduct: ((Long, Long)) => Route = ???

And defined.

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)
)

Versions

Version
0.5.1
0.5.0-rt1
0.5.0
0.4.0
0.3.0
0.2.6
0.2.3
0.2.2
0.2.1
0.2.0
0.1.2
0.1.1
0.1.0