I want to return from Akka Actor a string response through api code. However, I'm keep getting timeouts and I don't have a clue what can be wrong with my code.
Actor:
object CredentialsActor {
def props(implicit timeout: Timeout) = Props(new CredentialsActor)
}
class CredentialsActor extends Actor with ActorLogging {
override def receive: Receive = {
case RegisterRequest => sender() ! "REGISTER"
}
}
Api code:
class RestApi(system: ActorSystem, timeout: Timeout) extends Routes {
implicit def requestTimeout: Timeout = timeout
implicit def executionContext: ExecutionContextExecutor = system.dispatcher
def createCredentialsActor(): ActorRef = system.actorOf(CredentialsActor.props)
}
trait Routes extends CredentialsApi {
val routes: Route =
pathPrefix("app") {
credentialsRoute
}
}
trait CredentialsApi {
def createCredentialsActor(): ActorRef
implicit def requestTimeout: Timeout
lazy val credentialsActor: ActorRef = createCredentialsActor()
val credentialsRoute: Route =
path("register") {
get {
pathEndOrSingleSlash {
entity(as[RegisterRequest]) {
request => {
System.out.println(request.name, request.password, request.passwordRepeated)
val response: Future[String] =
(credentialsActor ? request).mapTo[String]
complete(OK, response)
}
}
}
}
}
}
Request
final case class RegisterRequest(name: String, password: String, passwordRepeated: String)
You don't give the type of RegisterRequest
, but if it is a case class
then you want this:
case _: RegisterRequest => sender() ! "REGISTER"
As it stands you are matching the companion object rather than an instance of the class.
To extract the details from the request, do this:
case RegisterRequest(name, pw, pwRep) =>
sender ! s"Register user $name with password $pw($pwRep)"