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

There's no way to use a "providing middleware" on a Routes nor an HttpApp #2488

Closed
guizmaii opened this issue Oct 18, 2023 · 4 comments · Fixed by #2778
Closed

There's no way to use a "providing middleware" on a Routes nor an HttpApp #2488

guizmaii opened this issue Oct 18, 2023 · 4 comments · Fixed by #2778
Labels

Comments

@guizmaii
Copy link
Member

guizmaii commented Oct 18, 2023

Is your feature request related to a problem? Please describe.

For now, it seems that there's no way to use a "providing middleware" on a Routes nor on an HttpApp.
It can only be used on a Handler, which means that to have some "private routes" I need to repeat the "providing middleware" in each route declaration:

final case class LoggedUser(email: String)
val authProviderMiddleware: HandlerAspect[Any, LoggedUser] = ...
val sharedMiddlewares: Middleware[Any] = ... ++ ... ++ ...

val publicRoute = 
  Routes(
    Method.GET / "example" -> ...
    ...
  )

val privateRoutes =
  Routes(
    Method.GET / "private" / "example" -> authProviderMiddleware -> handler { case (user: LoggedUser, req: Request) => ... } 
    Method.POST / "private" / "example" -> authProviderMiddleware -> handler { case (user: LoggedUser, req: Request) => ... } 
    ...
  )
    
Server.serve(app @ sharedMiddleware)

Describe the solution you'd like

It would be nicer to be able to do something like this:

final case class LoggedUser(email: String)
val authProviderMiddleware: HandlerAspect[Any, LoggedUser] = ...
val sharedMiddlewares: Middleware[Any] = ... ++ ... ++ ...

val publicRoute = 
  Routes(
    Method.GET / "example" -> ...
    ...
  )

val privateRoutes =
  Routes(
    Method.GET / "private" / "example" -> handler { case (user: LoggedUser, req: Request) => ... } 
    Method.POST / "private" / "example" -> handler { case (user: LoggedUser, req: Request) => ... } 
    ...
  ) @@ authProviderMiddleware
    
Server.serve(app @ sharedMiddleware)
@guizmaii guizmaii added the enhancement New feature or request label Oct 18, 2023
@jdegoes
Copy link
Member

jdegoes commented Jan 6, 2024

/bounty $100

@987Nabil Working on a fix for this.

Copy link

algora-pbc bot commented Jan 6, 2024

💎 $100 bounty created by ZIO
🙋 If you start working on this, comment /attempt #2488 along with your implementation plan
👉 To claim this bounty, submit a pull request that includes the text /claim #2488 somewhere in its body
📝 Before proceeding, please make sure you can receive payouts in your country
💵 Payment arrives in your account 2-5 days after the bounty is rewarded
💯 You keep 100% of the bounty award
🙏 Thank you for contributing to zio/zio-http!

👉 Add a bountyShare on socials

Attempt Started (GMT+3) Solution
🟢 @987Nabil #2778

Copy link

algora-pbc bot commented Apr 21, 2024

🎉🎈 @987Nabil has been awarded $100! 🎈🎊

@guizmaii
Copy link
Member Author

Thanks @987Nabil! ❤️

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

Successfully merging a pull request may close this issue.

2 participants