akka-http with multiple route configurations

后端 未结 1 1667
醉酒成梦
醉酒成梦 2021-02-05 16:26

Quick Background

I am running through some examples learning the Akka HTTP stack for creating a new REST project (completely non-UI). I have been using and augmenting

1条回答
  •  说谎
    说谎 (楼主)
    2021-02-05 17:19

    Problem 1 - combine routes in multiple files

    You can combine routes from multiple files quite easy.

    FooRouter.scala

    object FooRouter {
       val route = path("foo") {
           complete {
              Ok -> "foo"
           } 
       }       
    }
    

    BarRouter.scala

    object BarRouter {
       val route = path("bar") {
           complete {
              Ok -> "bar"
           } 
       }       
    }
    

    MainRouter.scala

    import FooRouter
    import BarRouter
    import akka.http.scaladsl.server.Directives._
    import ...
    
    object MainRouter {
       val routes = FooRouter.route ~ BarRouter.route
    }
    
    object AkkaHttpMicroservice extends App with Service {
      ...    
      Http().bindAndHandle(MainRouter.routes, config.getString("http.interface"), config.getInt("http.port"))
    }
    

    Here you have have some docs :

    • http://doc.akka.io/docs/akka-http/current/scala/http/routing-dsl/overview.html
    • http://doc.akka.io/docs/akka-http/current/scala/http/routing-dsl/routes.html

    Problem 2 - seprate routing, marshalling, etc

    Yes, you can separate routing, marshalling and application logic. Here you have activator example: https://github.com/theiterators/reactive-microservices

    Problem 3 - handle routes using annotations

    I don't know any lib that allow you to use annotion to define routing in akka-http. Try to learn more about DSL routing. This represents a different approach to http routing but it is convenient tool too.

    0 讨论(0)
提交回复
热议问题