Akka-http with multiple route configurations

Fast background

I am looking at a few examples studying the Akka HTTP stack to create a new REST project (completely non-UI). I use and complement the Akka HTTP Microservice Example to work through a bunch of use cases and configurations, and was pleasantly surprised at how well Scala and Akka HTTP work.

Current setting

I currently have this configuration:

object AkkaHttpMicroservice extends App with Service { override implicit val system = ActorSystem() override implicit val executor = system.dispatcher override implicit val materializer = ActorMaterializer() override val config = ConfigFactory.load() override val logger = Logging(system, getClass) Http().bindAndHandle(routes, config.getString("http.interface"), config.getInt("http.port")) } 

The routes parameter is a simple value that has typical data inside it using path , pathPrefix , etc.

Problem

Is there a way to configure routing in multiple Scala files or an example somewhere out there?

I would really like to be able to define a set of classes that share problems and handle the configuration and processing of the Actor to work with various areas of the application and simply leave marshaling for the root extension of the App .

Perhaps I was thinking too much about how I was doing something in Java using annotations like @javax.ws.rs.Path("/whatever") for my classes. If so, feel free to point out a change in thinking.

I tried to find several different keywords, but I think that I am asking the wrong question (for example, 1 , 2 ).

+8
scala
source share
1 answer

Problem 1 - combining routes into multiple files

You can easily combine routes from multiple files.

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 several documents:

Problem 2 - separation routing, sorting, etc.

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

Problem 3 - handle routes using annotations

I do not know any lib library that allows you to use annotation to determine routing in akka-http. Try to learn more about DSL routing. This is a different approach to http routing, but it is also a handy tool.

+16
source

All Articles