1

I'm new to Akka. While learning I've created a sample project on Github. Here this project uses Akka, Akka-HTTP, Slick, Flyway and Macwire. I wanted to implement Akka Actors for HTTP routing. I tried to implement this on redis/ but not working as expected.

Currently the Controller is working as:

class AuthController(userService: UserService[Future]) extends Controller {

  import de.heikoseeberger.akkahttpjson4s.Json4sSupport._
  implicit val serialization: Serialization.type = jackson.Serialization // or native.Serialization
  implicit val formats: DefaultFormats.type = DefaultFormats

  override def route: Route = pathPrefix("users") {
    pathEndOrSingleSlash {
      register
    }
  }

  private def register = {
    (post & entity(as[RegistrationData])) { registrationData =>
      complete(userService.registerUser(registrationData))
    }
  }

}

But I'm trying to implement something like this with some changes in existing code:

class AuthController(userhandler: ActorRef) extends Controller {

  import de.heikoseeberger.akkahttpjson4s.Json4sSupport._
  implicit val serialization: Serialization.type = jackson.Serialization // or native.Serialization
  implicit val formats: DefaultFormats.type = DefaultFormats

  override def route: Route = pathPrefix("users") {
    pathEndOrSingleSlash {
      register
    }
  }

  private def register = {
    (post & entity(as[RegistrationData])) { registrationData =>
      complete(
        (userHandler ? UserHandler.Register(registrationData)).map {
          case true => OK -> s"Thank you ${registrationData.username}"
          case _ => InternalServerError -> "Failed to complete your request. please try later"
        }
      )
    }
  }

}

Can anyone suggest me how can I implement the above? I appreciate your help.

Sujit Baniya
  • 895
  • 9
  • 27

1 Answers1

1

When asking such questions please include the error you're seeing. Here I assume it's about the missing execution context (for the Future to run on) as well as the Timeout, read more about the ask (A.K.A. ?) pattern: https://doc.akka.io/docs/akka/2.5/actors.html#ask-send-and-receive-future

In general what you're missing MAY (since not sure what exactly your problem is) be resolved by:

import system.dispatcher // The ExecutionContext that will be used

system being an actor system, and:

implicit val timeout = Timeout(5 seconds) // needed for?below

You can extract an ActorSystem instance from routes as well btw:

Konrad 'ktoso' Malawski
  • 13,102
  • 3
  • 47
  • 52