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.
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 theask(A.K.A.?) pattern: https://doc.akka.io/docs/akka/2.5/actors.html#ask-send-and-receive-futureIn 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 usedsystem being an actor system, and:
implicit val timeout = Timeout(5 seconds) // needed for?belowYou can extract an
ActorSysteminstance from routes as well btw: