Fiber Hooks
Through the hooks provided by the Fiber framework, we can intercept the behavior of the framework and customize logic at crucial points.
Starting from Fiber v2.30.0, you can execute custom user functions when running certain methods. Here is a list of these hooks:
- OnRoute (Route Hook)
- OnName (Name Hook)
- OnGroup (Group Hook)
- OnGroupName (Group Name Hook)
- OnListen (Listen Hook)
- OnFork (Fork Hook)
- OnShutdown (Shutdown Hook)
- OnMount (Mount Hook)
Constants
// Handlers define a function to create hooks for Fiber.
type OnRouteHandler = func(Route) error
type OnNameHandler = OnRouteHandler
type OnGroupHandler = func(Group) error
type OnGroupNameHandler = OnGroupHandler
type OnListenHandler = func(ListenData) error
type OnForkHandler = func(int) error
type OnShutdownHandler = func() error
type OnMountHandler = func(*App) error
OnRoute (Route Hook)
OnRoute is a hook that executes the user function every time a route is registered. You can also access the route properties through the route parameter.
Signature
func (app *App) OnRoute(handler ...OnRouteHandler)
OnName (Name Hook)
OnName is a hook that executes the user function every time a route is named. You can also access the route properties through the route parameter.
Note: OnName only applies to named routes, not to groups.
Signature
func (app *App) OnName(handler ...OnNameHandler)
- OnName Example
package main
import (
"fmt"
"github.com/gofiber/fiber/v2"
)
func main() {
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString(c.Route().Name)
}).Name("index")
app.Hooks().OnName(func(r fiber.Route) error {
fmt.Print("Name: " + r.Name + ", ")
return nil
})
app.Hooks().OnName(func(r fiber.Route) error {
fmt.Print("Method: " + r.Method + "\n")
return nil
})
app.Get("/add/user", func(c *fiber.Ctx) error {
return c.SendString(c.Route().Name)
}).Name("addUser")
app.Delete("/destroy/user", func(c *fiber.Ctx) error {
return c.SendString(c.Route().Name)
}).Name("destroyUser")
app.Listen(":5000")
}
// Result:
// Name: addUser, Method: GET
// Name: destroyUser, Method: DELETE
OnGroup (Group Hook)
OnGroup is a hook that executes the user function every time a group is registered. You can also access the group properties through the group parameter.
Signature
func (app *App) OnGroup(handler ...OnGroupHandler)
OnGroupName (Group Name Hook)
OnGroupName is a hook that executes the user function every time a group is named. You can also access the group properties through the group parameter.
Note: OnGroupName only applies to named groups, not to routes.
Signature
func (app *App) OnGroupName(handler ...OnGroupNameHandler)
OnListen (Listen Hook)
The OnListen
hook is used to execute user-defined functions when performing the Listen, ListenTLS, or Listener operations.
Signature
func (app *App) OnListen(handler ...OnListenHandler)
- Example of OnListen
app := fiber.New(fiber.Config{
DisableStartupMessage: true,
})
app.Hooks().OnListen(func(listenData fiber.ListenData) error {
if fiber.IsChild() {
return nil
}
scheme := "http"
if listenData.TLS {
scheme = "https"
}
log.Println(scheme + "://" + listenData.Host + ":" + listenData.Port)
return nil
})
app.Listen(":5000")
OnFork (Fork Hook)
The OnFork
hook is used to execute user-defined functions when performing the Fork operation.
Signature
func (app *App) OnFork(handler ...OnForkHandler)
OnShutdown (Shutdown Hook)
The OnShutdown
hook is used to execute user-defined functions after performing the Shutdown operation.
Signature
func (app *App) OnShutdown(handler ...OnShutdownHandler)
OnMount
OnMount
is a hook that executes user-defined functions after the mounting process. When a sub-application is mounted to a parent application, the mounting event is triggered, with the parent application being passed as a parameter. It is suitable for mounting applications and groups.
Signature
func (h *Hooks) OnMount(handler ...OnMountHandler)
- Example of OnMount
package main
import (
"fmt"
"github.com/gofiber/fiber/v2"
)
func main() {
app := New()
app.Get("/", testSimpleHandler).Name("x")
subApp := New()
subApp.Get("/test", testSimpleHandler)
subApp.Hooks().OnMount(func(parent *fiber.App) error {
fmt.Print("Parent application's mount path: "+parent.MountPath())
// ...
return nil
})
app.Mount("/sub", subApp)
}
// Result:
// Parent application's mount path:
Note: The OnName/OnRoute/OnGroup/OnGroupName hooks are sensitive to mounting. If you use these routes on a sub-application and mount it, the paths of the routes and groups will start with the mount prefix.