awakesecurity / gRPC-haskell

Haskell gRPC support
https://hackage.haskell.org/package/grpc-haskell
Apache License 2.0
236 stars 74 forks source link

Handle State #165

Closed MohsenNz closed 2 months ago

MohsenNz commented 2 months ago

Can we handle state in RPC functions ? E.g I have this service :

service PhoneAuthService {
  rpc Login(LoginRequest) returns (LoginResponse);
}

message LoginRequest {
  string phone_number = 1;
}

message LoginResponse {
  string verification_code = 1;
}

And here is my code :

handlers :: PhoneAuthService ServerRequest ServerResponse
handlers =
    PhoneAuthService
        { phoneAuthServiceLogin = loginHandler
        }

loginHandler
    :: ServerRequest 'Normal LoginRequest LoginResponse
    -> IO (ServerResponse 'Normal LoginResponse)
loginH (ServerNormalRequest _ (LoginRequest phoneNumber)) = do
    vcode <- generateVerificationCode 6
    let resp = LoginResponse vcode
    pure $ ServerNormalResponse resp [] StatusOk ""

In function loginHandler I want to store phoneNumber in a state (in-memory or any database), but I think with the type signature of this function It's not possible.

riz0id commented 2 months ago

I suggest you familiarize yourself with Data.IORef.

MohsenNz commented 2 months ago

Thanks a lot.

ixmatus commented 2 months ago

@MohsenNz please reserve this repository's issues for bugs or feature requests regarding gRPC-haskell. If you need help with Haskell and how to implement things in Haskell, please post questions to StackOverflow or Haskell Discord.