-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
✨ feature: add initial support for hooks #1777
Conversation
gave him some tasks to move all the code into a separate struct called hooks.go(+tests) and thus reduce the size of the file for the app @hi019 what do you think about the different hooks |
Will look tomorrow |
Looks good, I'll test it out tomorrow. But why do we need OnResponse/OnRequest when the same thing can be done with a middleware? Also what about changing the hook signature to not return anything, so that the hook handles any errors itself? |
OnX methods don't return anything already. |
I mean HookHandler: type HookHandler = func(*Ctx, Map) error |
i thought handling OnRoute and OnListen errors would be useful but if you're not OK, i can remove them. |
Actually, I think the current implementation is fine. I'll finish the review tomorrow |
What about using a struct instead of a map for the hook function params, to provide better type safety? It would be nice if each hook had its own struct. Also this print statement seems to be executed twice: package main
import (
"fmt"
"log"
"github.com/gofiber/fiber/v2"
)
func main() {
app := fiber.New(fiber.Config{DisableStartupMessage: true})
app.Hooks().OnRoute(func(c *fiber.Ctx, m fiber.Map) error {
fmt.Println(m["route"].(fiber.Route).Path)
return nil
})
app.Get("/hello", h)
log.Fatal(app.Listen(":3001"))
}
func h(c *fiber.Ctx) error {
return c.Status(fiber.StatusOK).SendString("Hello")
} It's because app.register is being called twice somehow. Still looking into that |
I think it's not bug because of Get creates GET and HEAD methods. |
done |
I meant changing the hook handlers to be something like this: // Old
type HookHandler = func(*Ctx, Map) error
// New
// naming could be improved
type OnRouteHandler = func(*Ctx OnRouteParams) This will prevent typecasting: app.Hooks().OnRoute(func(c *fiber.Ctx, m fiber.OnRouteParams) error {
// Old
fmt.Println(m["route"].(fiber.Route).Path)
// New
fmt.Println(m.Route.Path)
return nil
}) |
Good idea. I'll change it. Also i plan to add OnGroup. Thanks for suggestions. |
Also we'll need to update your docs pr with the struct changes |
I'll update docs tomorrow. |
done |
Simple Usage:
Closes: #1758
To-Do List: