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

📝 [Proposal]: Branching handlers and with rules #3260

Open
3 tasks done
dozheiny opened this issue Dec 21, 2024 · 11 comments
Open
3 tasks done

📝 [Proposal]: Branching handlers and with rules #3260

dozheiny opened this issue Dec 21, 2024 · 11 comments

Comments

@dozheiny
Copy link
Contributor

Feature Proposal Description

HTTP servers usually use HTTP methods (like GET, POST, and PATCH) to separate and isolate the requests sent to them.
I'm thinking about more ways to separate and isolate requests. For example, I could separate requests by specific header value or specific value on URL query.

Alignment with Express API

I didn't find anything

HTTP RFC Standards Compliance

Confirmed

API Stability

package main

import "github.com/gofiber/fiber/v2"

func main() {
	app := fiber.New()

	app.Get("/", handler1, rules.Header("foo", "bar"))
        app.Get("/", handler2, rules.Header("key", "value"))

	app.Listen(":3000")
}

func handler1(c *fiber.Ctx) error {
    return c.SendString("Hello, World!")
}

func handler2(c *fiber.Ctx) error {
    return c.SendString("Goodbye, World!")
}
> curl http://127.0.0.1:3000/ -H "foo:bar"
< Hello, World!
> curl http://127.0.0.1:3000/ -H "key:value"
< GoodBye, World!

Feature Examples

app.Get("/", handler1, rules.Header("foo", "bar"))
app.Get("/", handler2, rules.Header("key", "value"))


> curl http://127.0.0.1:3000/ -H "foo:bar"
< Hello, World!

> curl http://127.0.0.1:3000/ -H "key:value"
< GoodBye, World!

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have searched for existing issues that describe my proposal before opening this one.
  • I understand that a proposal that does not meet these guidelines may be closed without explanation.
@gaby
Copy link
Member

gaby commented Dec 21, 2024

Pretty sure this would make the router slower, since checking headers is an expensive operation on top of checking the handler path.

@dozheiny
Copy link
Contributor Author

It could be optional if it causes performance issues.

@JIeJaitt
Copy link
Contributor

@dozheiny What are the situations in which this recommendation could be used in actual production development work?

@ReneWerner87
Copy link
Member

right the performance should drop if you activate the feature because more checks are necessary

but still it is an interesting feature

@coderabbitai
please give examples where it is used in other languages and frameworks and show usage examples

@JIeJaitt
Copy link
Contributor

coderabbitai doesn't seem to be giving any replies. @ReneWerner87 Can this bot be used like chatgpt inside a GitHub issue?

@ReneWerner87
Copy link
Member

@coderabbitai please give examples where it is used in other languages and frameworks and show usage examples

@dozheiny
Copy link
Contributor Author

@dozheiny What are the situations in which this recommendation could be used in actual production development work?

It can be used for different user types like admins and normal users.
For example, we have the GET /products HTTP/1.1 API.

It can route different users to different controllers by using a specific header value or something else.

@gaby
Copy link
Member

gaby commented Dec 23, 2024

I believe you can do this already using redirect

https://docs.gofiber.io/next/api/redirect

@dozheiny
Copy link
Contributor Author

@gaby True, but my point is to reduce middleware implementations or redirections.

@gaby
Copy link
Member

gaby commented Dec 23, 2024

Only thing I can think off is adding a middleware named Rules. Changing the router/app to add this doesn't make sense given its a unique use-case.

This kind of access control is not normally done using headers. There's other middlewares that check auth headers to handle stuff like this.

@dozheiny
Copy link
Contributor Author

It's okay; that was just an idea that came to mind. I think we can close the issue since there are no examples or implementations in other frameworks.

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

No branches or pull requests

4 participants