πŸͺ
Hooks
With Fiber v2.30.0, you can execute custom user functions when to run some methods. Here is a list of this hooks:

Constants

1
// Handlers define a function to create hooks for Fiber.
2
type OnRouteHandler = func(Route) error
3
type OnNameHandler = OnRouteHandler
4
type OnGroupHandler = func(Group) error
5
type OnGroupNameHandler = OnGroupHandler
6
type OnListenHandler = func() error
7
type OnShutdownHandler = OnListenHandler
Copied!

OnRoute

OnRoute is a hook to execute user functions on each route registeration. Also you can get route properties by route parameter.
Signature
1
func (app *App) OnRoute(handler ...OnRouteHandler)
Copied!

OnName

OnName is a hook to execute user functions on each route naming. Also you can get route properties by route parameter.
WARN: OnName only works with naming routes, not groups.
Signature
1
func (app *App) OnName(handler ...OnNameHandler)
Copied!

OnGroup

OnGroup is a hook to execute user functions on each group registeration. Also you can get group properties by group parameter.
Signature
1
func (app *App) OnGroup(handler ...OnGroupHandler)
Copied!

OnGroupName

OnGroupName is a hook to execute user functions on each group naming. Also you can get group properties by group parameter.
WARN: OnGroupName only works with naming groups, not routes.
Signature
1
func (app *App) OnGroupName(handler ...OnGroupNameHandler)
Copied!

OnListen

OnListen is a hook to execute user functions on Listen, ListenTLS, Listener.
Signature
1
func (app *App) OnListen(handler ...OnListenHandler)
Copied!

OnShutdown

OnShutdown is a hook to execute user functions after Shutdown.
Signature
1
func (app *App) OnShutdown(handler ...OnShutdownHandler)
Copied!
OnName Example
1
package main
2
​
3
import (
4
"fmt"
5
​
6
"github.com/gofiber/fiber/v2"
7
)
8
​
9
func main() {
10
app := fiber.New()
11
​
12
app.Get("/", func(c *fiber.Ctx) error {
13
return c.SendString(c.Route().Name)
14
}).Name("index")
15
​
16
app.Hooks().OnName(func(r fiber.Route) error {
17
fmt.Print("Name: " + r.Name + ", ")
18
​
19
return nil
20
})
21
​
22
app.Hooks().OnName(func(r fiber.Route) error {
23
fmt.Print("Method: " + r.Method + "\n")
24
​
25
return nil
26
})
27
​
28
app.Get("/add/user", func(c *fiber.Ctx) error {
29
return c.SendString(c.Route().Name)
30
}).Name("addUser")
31
​
32
app.Delete("/destroy/user", func(c *fiber.Ctx) error {
33
return c.SendString(c.Route().Name)
34
}).Name("destroyUser")
35
​
36
app.Listen(":5000")
37
}
38
​
39
// Results:
40
// Name: addUser, Method: GET
41
// Name: destroyUser, Method: DELETE
Copied!