tapir icon indicating copy to clipboard operation
tapir copied to clipboard

Does ZTapir endpoint execute code within a scope?

Open Kalin-Rudnicki opened this issue 1 year ago • 1 comments

Looking at the function signatures for:

    def zServerSecurityLogic[R, U](
        f: SECURITY_INPUT => ZIO[R, ERROR_OUTPUT, U]
    ): ZPartialServerEndpoint[R, SECURITY_INPUT, U, INPUT, ERROR_OUTPUT, OUTPUT, C] =
      ZPartialServerEndpoint(e, f)

  def serverLogic[R0](logic: PRINCIPAL => INPUT => ZIO[R0, ERROR_OUTPUT, OUTPUT]): ZServerEndpoint[R with R0, C] =
    ServerEndpoint(
      endpoint,
      _ => securityLogic(_: SECURITY_INPUT).either.resurrect,
      _ => (u: PRINCIPAL) => (i: INPUT) => logic(u)(i).either.resurrect
    )

Within zServerSecurityLogic I would like to use ZIO.logAnnotateScoped(_, _) to add log context from within the JWT that will be there for all requests. If this is possible, it would be super ideal to add this here, as all endpoints are already calling a shared function that decodes the JWT.

If things are already executed within a scope, I believe the correct signatures would be: (?)

f: SECURITY_INPUT => ZIO[R & Scope, ERROR_OUTPUT, U]

logic: PRINCIPAL => INPUT => ZIO[R0 & Scope, ERROR_OUTPUT, OUTPUT]

Kalin-Rudnicki avatar Sep 19 '24 17:09 Kalin-Rudnicki

It's not currently run in a scope, in fact the environment isn't touched at all. Though it might make sense to implement such a functionality. Here's the code for handling requests - I imagine the changes would have to be done there. Provided that you are using zio-http, of course.

If you'd like to attempt a PR, that would be great :)

adamw avatar Sep 23 '24 14:09 adamw