-
Notifications
You must be signed in to change notification settings - Fork 120
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
Abstract Fiber Context with Interface for Version Independence #1202
base: main
Are you sure you want to change the base?
Conversation
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
@coderabbitai what would be your suggestion to solve the existing problem and solve the idea of the independent version theoretically, other middlewares could also use other functions, which also give themselves back again |
Tip For best results, initiate chat on the files or code changes. To solve the existing problem and achieve version independence, one approach is to redefine the Here’s how you might redefine the type Context interface {
// Other necessary methods...
Status(status int) Context
SendString(body string) error
// Include all chainable methods, ensuring they return Context
} Then, implement this interface with a wrapper around type FiberContextWrapper struct {
*fiber.Ctx
}
func (c *FiberContextWrapper) Status(status int) Context {
c.Ctx.Status(status)
return c
}
func (c *FiberContextWrapper) SendString(body string) error {
return c.Ctx.SendString(body)
}
// Implement other methods as needed... By wrapping Considerations:
Example Usage in Middleware: func Middleware(next func(Context) error) func(Context) error {
return func(c Context) error {
// Middleware logic...
return next(c)
}
} This way, your middleware is fully abstracted over the Let me know if you'd like further clarification or assistance with implementing this solution. |
@coderabbitai but using the fiber.Ctx would mean that the code is not independent of the version used |
Tip For best results, initiate chat on the files or code changes. You're right; using An alternative solution is to define a custom Here's how you might proceed:
By doing this, your middleware remains independent of any specific Fiber version, as it only depends on the custom Benefits:
Considerations:
Example: Define the type Context interface {
Status(status int) Context
SendString(body string) error
// Additional methods...
} Implement the interface for Fiber v2: type FiberV2Context struct {
ctx *fiber.Ctx // Fiber v2 context
}
func (c *FiberV2Context) Status(status int) Context {
c.ctx.Status(status)
return c
}
func (c *FiberV2Context) SendString(body string) error {
return c.ctx.SendString(body)
}
// Additional method implementations... Implement for Fiber v3 similarly. Usage in Middleware: func YourMiddleware(next func(Context) error) func(Context) error {
return func(c Context) error {
// Middleware logic
return next(c)
}
} Integrate with Fiber Application: app := fiber.New()
app.Use(func(c *fiber.Ctx) error {
var ctx Context
// Detect Fiber version or configure accordingly
ctx = &FiberV2Context{ctx: c} // or FiberV3Context, etc.
return YourMiddleware(func(c Context) error {
return c.(*FiberV2Context).ctx.Next()
})(ctx)
}) This approach decouples your middleware from the Fiber framework entirely, achieving true version independence. Let me know if this aligns with your goals or if you'd like to explore this further. ✏️ Learnings added
|
@coderabbitai we only want to support v3 and higher versions as v3 added the ctx interface for the first time |
Tip For best results, initiate chat on the files or code changes. Understood. Since you're targeting Fiber v3 and higher, which introduced the Here's how you might proceed:
Considerations:
Example Middleware Function: func CompressionMiddleware(next fiber.Handler) fiber.Handler {
return func(c fiber.Ctx) error {
// Perform compression logic
c.Response().Header.Set("Content-Encoding", "gzip")
// Continue with the next handler
return next(c)
}
} Usage: app := fiber.New()
app.Use(CompressionMiddleware)
app.Get("/", func(c fiber.Ctx) error {
return c.SendString("Hello, World!")
})
app.Listen(":3000") By using the Let me know if this approach works for you or if you have further questions. 🧠 Learnings used
|
This PR introduces an abstraction for the Fiber context to make Fiber contrib libraries independent of the Fiber version being used.
Key Changes:
Known Issue:
Currently, the fiber.Ctx interface presents a limitation for complete version independence. Methods such as Status() return fiber.Ctx itself, which tightly couples the interface to the original Fiber implementation. This behavior makes the abstraction less flexible and middleware dependent on a specific Fiber version.
A fix for this issue is planned by modifying the Context interface to return the abstracted Context instead of fiber.Ctx.
This PR is an important step towards making Fiber contrib libraries more flexible, reusable, and compatible across different Fiber versions. Further refinements will follow to address the self-referential return issue and fully decouple the middleware from the Fiber framework.