Coder Social home page Coder Social logo

akka-http-router's Introduction

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

akka-http-router's People

Contributors

hayasshi avatar scala-steward avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.