All Projects → hayasshi → akka-http-router

hayasshi / akka-http-router

Licence: Apache-2.0 license
A simple library for route definition of akka-http.

Programming Languages

scala
5932 projects

Labels

Projects that are alternatives of or similar to akka-http-router

akka-http-quickstart-java.g8
akka.io
Stars: ✭ 17 (-15%)
Mutual labels:  akka-http
akka-cookbook
提供清晰、实用的Akka应用指导
Stars: ✭ 30 (+50%)
Mutual labels:  akka-http
akka-http-actor-per-request
Example akka application that uses the actor per request model
Stars: ✭ 16 (-20%)
Mutual labels:  akka-http
fdp-modelserver
An umbrella project for multiple implementations of model serving
Stars: ✭ 47 (+135%)
Mutual labels:  akka-http
akka-http-hal
HAL (Hypermedia Application Language) specification support for akka-http
Stars: ✭ 18 (-10%)
Mutual labels:  akka-http
swakka
A Scala library for creating Swagger definitions in a type-safe fashion wth Akka-Http
Stars: ✭ 74 (+270%)
Mutual labels:  akka-http
reducio
❱ 🐇 URL shortener service is written in Scala using Akka-Http and Redis ❰
Stars: ✭ 69 (+245%)
Mutual labels:  akka-http
akka-http-streaming-response-examples
A list of examples that involve streaming with Akka Streams and used together with Akka HTTP
Stars: ✭ 73 (+265%)
Mutual labels:  akka-http
facebook4s
An asynchronous non-blocking Scala client for Facebook Graph API (Facebook api REST)
Stars: ✭ 18 (-10%)
Mutual labels:  akka-http
akka-http-circe-json-template
Akka HTTP REST API Project Template using Akka HTTP 10.0.4 with Circe 0.7.0 targeting Scala 2.12.x
Stars: ✭ 21 (+5%)
Mutual labels:  akka-http
akka-cqrs-activator
Issue tracker PoC application written in Scala (Akka) and JavaScript (React) that demonstrates event sourcing and CQRS
Stars: ✭ 33 (+65%)
Mutual labels:  akka-http
Quark
Quark is a streaming-first Api Gateway using Akka
Stars: ✭ 13 (-35%)
Mutual labels:  akka-http
sbt-guardrail
Principled code generation from OpenAPI specifications
Stars: ✭ 24 (+20%)
Mutual labels:  akka-http
akka-http-docker-sample
example of running an Sbt application in Docker based on openjdk:jre-alpine
Stars: ✭ 20 (+0%)
Mutual labels:  akka-http
jwt-akka-http
An example how to implement a very simple authentication and authorization with Akka HTTP. Related to https://blog.codecentric.de/en/2017/09/jwt-authentication-akka-http
Stars: ✭ 23 (+15%)
Mutual labels:  akka-http
akka-http-realworld-example
Exemplary real world application built with Scala + Akka HTTP https://realworld.io
Stars: ✭ 30 (+50%)
Mutual labels:  akka-http
slicebox
Microservice for safe sharing and easy access to medical images
Stars: ✭ 18 (-10%)
Mutual labels:  akka-http
playsonify
An opinionated micro-framework to help you build practical JSON APIs with Play Framework (or akka-http)
Stars: ✭ 42 (+110%)
Mutual labels:  akka-http
ecommerce
A project for exploring Akka with Scala
Stars: ✭ 24 (+20%)
Mutual labels:  akka-http
khermes
A distributed fake data generator based in Akka.
Stars: ✭ 94 (+370%)
Mutual labels:  akka-http

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)
)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].