Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow server implementation to access extra attributes #144

Open
i10416 opened this issue Nov 2, 2024 · 1 comment
Open

Allow server implementation to access extra attributes #144

i10416 opened this issue Nov 2, 2024 · 1 comment

Comments

@i10416
Copy link
Contributor

i10416 commented Nov 2, 2024

Current method signature: (t:T, ctx: Headers) lacks ability to get attributes from outside.

Imagine when you implement an auth interceptor that put a user id to request's attributes, how can you get the user id in service implementation?

val routes = TestService.toRoutes(impl)
val withAuth = HttpRoutes[IO] { req =>
    OptionT.liftF(decodeJWT(req.headers.get(ci"Authorization").head)).flatMap {
      claims => routes.run(req.withAttributes(userIdKey, claims.sub))
    }
}

I have two (similar) idea to implement this feature.

The first idea, which draws inspiration from Rust's tonic, is to define gRPC Request type to carry extra data and generated method signature becomes t: Request[T].

trait Request[T] {
  def message: T
  def headers: Headers // For accuracy, we should us e grpc MetadataMap type instead of http.Headers
  def attributes: Vault
}

// ...
  def unaryToUnary[F[_]: Temporal, A, B](
      decode: Decoder[A],
      encode: Encoder[B],
      serviceName: String,
      methodName: String,
  )( // Stuff we apply at invocation
-     f: (A, Headers) => F[B]
+.    f: (Request[A]) => F[B]
 ): HttpRoutes[F] = HttpRoutes.of[F] {

The second idea is to just add Vault type to f arguments.

// ...
  def unaryToUnary[F[_]: Temporal, A, B](
      decode: Decoder[A],
      encode: Encoder[B],
      serviceName: String,
      methodName: String,
  )( // Stuff we apply at invocation
-     f: (A, Headers) => F[B]
+.    f: (A, Headers, Vault) => F[B]
 ): HttpRoutes[F] = HttpRoutes.of[F] {
@i10416 i10416 changed the title Allow implementation to access attributes Allow implementation to access extra attributes Nov 2, 2024
@i10416 i10416 changed the title Allow implementation to access extra attributes Allow server implementation to access extra attributes Nov 2, 2024
@i10416
Copy link
Contributor Author

i10416 commented Nov 2, 2024

I found another approach at fs2-grpc.
typelevel/fs2-grpc#668

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant