akka-http with multiple route configurations

隐身守侯 提交于 2019-12-03 05:58:35
mgosk

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 :

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!