0
votes

I have an old Scala/Akka Http project that I'm trying to simplify and refactor. I was wondering if there's a better way to organize routes and perhaps split them across actors. Here's what I have at the moment (far from ideal):

```

object MyAPI {
  def props(): Props = Props(new MyAPI())
  val routes = pathPrefix("api") {
    pathPrefix("1") {
      SomeActor.route  //More routes can be appended here using ~

    }
  }
}

final class MyAPI extends Actor with ActorLogging {

  implicit lazy val materializer = ActorMaterializer()
  implicit lazy val executionContext = context.dispatcher

  Http(context.system)
    .bindAndHandleAsync(Route.asyncHandler(MyAPI.routes), MyHttpServer.httpServerHostName, MyHttpServer.httpServerPort)
    .pipeTo(self)

  override def receive: Receive = {
    case serverBinding: ServerBinding =>
      log.info(s"Server started on  ${serverBinding.localAddress}")
      context.become(Actor.emptyBehavior)
    case Status.Failure(t) =>
      log.error(t, "Error binding to network interface")
      context.stop(self)
  }
}

```

```

object SomeActor {

  def props(): Props = Props[SomeActor]
  val route = get {
    pathPrefix("actor") {
      pathEnd {
        complete("Completed") //Is there a clean way 'ask' the actor below?
      }
    }
  }

}

class SomeActor extends Actor with ActorLogging {
  implicit lazy val executionContext = context.dispatcher;



  override def receive: Receive = {
    //receive and process messages here

  }

```

So, my question is - is there a clean way to structure and refactor routes instead of lumping them together in one large route definition? I could perhaps create a hierarchy of actors (routers) and the main route definition just delegates it to the routers and we incrementally add more details as we go deeper in the actor hierarchy. But is there a generally accepted patter or two to organize routes?

1

1 Answers

0
votes

I would like to suggest you on the basis of the functionality you can have as many actors you you want, but create one supervisor actors which will keep watch on each child actor. And all the supervision strategies should be written into the supervisor itself, and all the msg you gonna sent to the actors should be forwarded by the supervisor.

As soon as you get the data from the end point, may be get or post method take the data into someRequest case class. Then sent it to some handleReq() method. And then do your processing make traits on functionality basis.

You can structure project something like this. src/ actor//all the actors will be in this package

model// all the case classes and constants

repo// all the db related function you can do here

service// all your routes and endPoint functions

Now you can have package util where all the utilities trait you can put which will be used by any of the actor, or service may be you have lots of validations you can have a package named validator.

The structure is depends on your business. I think it would help.