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

Serving Individual Routes That are Ready to be Served #2805

Closed
khajavi opened this issue Apr 25, 2024 · 4 comments · Fixed by #2895
Closed

Serving Individual Routes That are Ready to be Served #2805

khajavi opened this issue Apr 25, 2024 · 4 comments · Fixed by #2895
Labels

Comments

@khajavi
Copy link
Member

khajavi commented Apr 25, 2024

After removing HttpApp, another thing that can make APIs simpler is to have a Server that can serve variable length arguments of individual routes. like this:

object MainApp extends ZIOAppDefault {
   def run = 
     Server.serve(
       Method.GET / "text" -> handler(Response.text("Hello World!")),
       Method.GET / "json" -> handler(Response.json("""{"greetings": "Hello World!"}""")),
       // and so on
     ).provide(Server.default)
}

The Routes is useful to take the last step in making the routes ready to be served, such as handling errors, applying middlewares, sandbox, etc. However, sometimes, we do not need this step, and our routes are ready to be served and all of their error are handled by converting to Response.

It can reduce one level of indirection on use-cases like this:

import zio._

import zio.http._

object HelloWorld extends ZIOAppDefault {
  val textRoute =
    Method.GET / "text" -> handler(Response.text("Hello World!"))

  val jsonRoute =
    Method.GET / "json" -> handler(Response.json("""{"greetings": "Hello World!"}"""))

  // Create HTTP route
  val app = Routes(textRoute, jsonRoute)

  // Run it like any simple app
  override val run = Server.serve(app).provide(Server.default)
}
@khajavi khajavi added the enhancement New feature or request label Apr 25, 2024
@jdegoes
Copy link
Member

jdegoes commented Jun 5, 2024

/bounty $50

Copy link

algora-pbc bot commented Jun 5, 2024

💎 $50 bounty • ZIO

Steps to solve:

  1. Start working: Comment /attempt #2805 with your implementation plan
  2. Submit work: Create a pull request including /claim #2805 in the PR body to claim the bounty
  3. Receive payment: 100% of the bounty is received 2-5 days post-reward. Make sure you are eligible for payouts

Thank you for contributing to zio/zio-http!

Add a bountyShare on socials

Attempt Started (GMT+0) Solution
🟢 @987Nabil #2895

Copy link

algora-pbc bot commented Jun 7, 2024

💡 @987Nabil submitted a pull request that claims the bounty. You can visit your bounty board to reward.

Copy link

algora-pbc bot commented Jun 7, 2024

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

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