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 prepending input path to HttpInterpreter generated Tapir endpoints #2188

Merged

Conversation

olisikh
Copy link
Contributor

@olisikh olisikh commented Apr 13, 2024

Hi,

I did not manage to implement full endpoints mapping because mapping over endpoints would depend on Streams and I can't figure out the type parameters 😞, since streams instance is provided only when .serverEndpoints is called (last step in the builder).

But I managed to expose Tapir's .prependIn endpoint combinator to add an extra EndpointInput[Unit] for the PublicEndpoint that HttpInterpreter makes.

It does the job, I can use HttpInterpreter like this now (tested with real project):

val status = ...
val api = ...
val graphql = HttpInterpreter(gqlInterpreter)
  .prependInput("graphql" / "api")
  .intercept[Any](myInterceptor)
  .serverEndpoints[Env, ZioStreams](ZioStreams)
  
 NettyZioSever[Env]()
   .addEndpoints(status)
   .addEndpoints(api)
   .addEndpoints(swagger)
   ...
   .start()

Any help / feedback is very welcome 🙏

Issue #2185

Oleksii

@ghostdogpr
Copy link
Owner

I gave it a try and I see what you mean about the streams type. This solution is a little bit specific but the implementation is short and simple so it seems ok. @kyri-petrou @paulpdaniels any better idea or shall we just ship this?

@paulpdaniels
Copy link
Collaborator

Is there anything we can do with this other than append path prefixes? Perhaps it would be better in that case to just expose a prependPath(path: String) then?

@olisikh
Copy link
Contributor Author

olisikh commented Apr 13, 2024

Hi @paulpdaniels,

I think it has to be EndpointInput[Unit].

The path with multiple segments and without output into serverLogic is encoded as EndpointInput[Unit] in Tapir, if we were to make it a String, it would make 1-segment paths and that is not very useful.

To my knowledge this is how it works:

.in("hello")             // makes it a /hello
.in("hello" / "world")   // makes it a /hello/world
.in("hello/world")       // makes it a hello%2Fworld (Tapir would encode slashes within a String)

PS I can think of making the input as def prependPath(pathPrefix: List[String]) instead and then combine the list elements with / to make it EndpointInput[Unit]

Example:

HttpInterpreter(graphql)
  .prependPath("graphql" :: "api" :: Nil)
  ...
  .serverEndpoints[Env, ZioStreams](ZioStreams)

@olisikh olisikh force-pushed the http-interpreter-map-over-endpoints branch from 9ae7083 to 0c87512 Compare April 13, 2024 16:39
@olisikh olisikh force-pushed the http-interpreter-map-over-endpoints branch from 0c87512 to 56aada0 Compare April 13, 2024 16:40
@olisikh
Copy link
Contributor Author

olisikh commented Apr 13, 2024

Narrowed it down to just .prependPath(path: List[String]), and I also like it more. Please let me know if it looks good for you and feel free to change anything the way you like 🙏

PS I tried to make input as EndpointInput.FixedPath[Unit] but at the call-site compiler can't infer that "hello" / "world" is a FixedPath[Unit] 🤷‍♂️

@ghostdogpr
Copy link
Owner

How about the WebSocketInterpreter? Can it be added there too?

@ghostdogpr ghostdogpr merged commit 3ddc3e0 into ghostdogpr:series/2.x Apr 14, 2024
10 checks passed
@ghostdogpr
Copy link
Owner

Thanks!

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

Successfully merging this pull request may close these issues.

None yet

3 participants