Fiber provides a powerful hooks system that allows you to run custom code at specific points in the application lifecycle.
Hook Types
All hook handler type definitions:
type OnRouteHandler = func(Route) error
type OnNameHandler = OnRouteHandler
type OnGroupHandler = func(Group) error
type OnGroupNameHandler = OnGroupHandler
type OnListenHandler = func(ListenData) error
type OnPreStartupMessageHandler = func(*PreStartupMessageData) error
type OnPostStartupMessageHandler = func(*PostStartupMessageData) error
type OnPreShutdownHandler = func() error
type OnPostShutdownHandler = func(error) error
type OnForkHandler = func(int) error
type OnMountHandler = func(*App) error
Routing Hooks
OnRoute
Executed after each route is registered.
func (h *Hooks) OnRoute(handler ...OnRouteHandler)
One or more handlers that receive the registered route.
app.Hooks().OnRoute(func(r fiber.Route) error {
fmt.Printf("Route registered: %s %s\n", r.Method, r.Path)
return nil
})
OnName
Executed when a route is named.
func (h *Hooks) OnName(handler ...OnNameHandler)
One or more handlers that receive the named route.
app.Hooks().OnName(func(r fiber.Route) error {
fmt.Printf("Route named: %s -> %s %s\n", r.Name, r.Method, r.Path)
return nil
})
app.Get("/users/:id", handler).Name("user.show")
OnName only triggers for routes that are explicitly named using .Name().
OnGroup
Executed after each route group is created.
func (h *Hooks) OnGroup(handler ...OnGroupHandler)
One or more handlers that receive the created group.
app.Hooks().OnGroup(func(g fiber.Group) error {
fmt.Printf("Group created: %s\n", g.Prefix)
return nil
})
api := app.Group("/api")
OnGroupName
Executed when a group is named.
func (h *Hooks) OnGroupName(handler ...OnGroupNameHandler)
One or more handlers that receive the named group.
OnGroupName only triggers for groups that are explicitly named.
Server Lifecycle Hooks
OnListen
Executed when the server starts listening.
func (h *Hooks) OnListen(handler ...OnListenHandler)
One or more handlers that receive ListenData.
app.Hooks().OnListen(func(data fiber.ListenData) error {
scheme := "http"
if data.TLS {
scheme = "https"
}
fmt.Printf("Server listening on %s://%s:%s\n", scheme, data.Host, data.Port)
return nil
})
ListenData
Contains information about the listening server:
The hostname or IP address the server is bound to.
The port number the server is listening on.
The application name from Config.
Total number of registered handlers.
Number of processes (relevant in prefork mode).
Whether prefork mode is enabled.
Child process IDs when using prefork.
Active color scheme for startup message.
OnPreStartupMessage
Executed before printing the startup banner.
func (h *Hooks) OnPreStartupMessage(handler ...OnPreStartupMessageHandler)
handler
...OnPreStartupMessageHandler
One or more handlers that receive PreStartupMessageData.
app.Hooks().OnPreStartupMessage(func(sm *fiber.PreStartupMessageData) error {
sm.BannerHeader = "MY APP " + sm.Version
sm.AddInfo("environment", "Environment", os.Getenv("ENV"))
sm.AddWarning("debug", "Debug Mode", "Enabled", 10)
return nil
})
PreStartupMessageData Methods
AddInfo
Add an informational entry to the startup banner.
func (sm *PreStartupMessageData) AddInfo(key, title, value string, priority ...int)
Unique identifier for this entry.
Display label for the entry.
Display priority (higher numbers appear first).
AddWarning
Add a warning entry to the startup banner.
func (sm *PreStartupMessageData) AddWarning(key, title, value string, priority ...int)
Unique identifier for this entry.
Display label for the entry.
Display priority (higher numbers appear first).
AddError
Add an error entry to the startup banner.
func (sm *PreStartupMessageData) AddError(key, title, value string, priority ...int)
Unique identifier for this entry.
Display label for the entry.
Display priority (higher numbers appear first).
EntryKeys
Get all entry keys currently in the startup message.
func (sm *PreStartupMessageData) EntryKeys() []string
DeleteEntry
Remove an entry from the startup message.
func (sm *PreStartupMessageData) DeleteEntry(key string)
The key of the entry to remove.
ResetEntries
Remove all entries from the startup message.
func (sm *PreStartupMessageData) ResetEntries()
PreStartupMessageData Fields
Custom ASCII art or header text. Leave empty for default Fiber banner.
Set to true to suppress the default startup message entirely.
OnPostStartupMessage
Executed after the startup banner is printed (or skipped).
func (h *Hooks) OnPostStartupMessage(handler ...OnPostStartupMessageHandler)
handler
...OnPostStartupMessageHandler
One or more handlers that receive PostStartupMessageData.
app.Hooks().OnPostStartupMessage(func(sm *fiber.PostStartupMessageData) error {
if !sm.Disabled && !sm.IsChild {
fmt.Println("Server started successfully!")
}
return nil
})
PostStartupMessageData Fields
True if startup message was disabled via DisableStartupMessage config.
True if this is a child process (prefork mode).
True if PreventDefault was set in OnPreStartupMessage.
OnPreShutdown
Executed before the server begins shutdown.
func (h *Hooks) OnPreShutdown(handler ...OnPreShutdownHandler)
One or more handlers executed before shutdown.
app.Hooks().OnPreShutdown(func() error {
fmt.Println("Server shutting down...")
// Close database connections, etc.
return nil
})
OnPostShutdown
Executed after the server has shut down.
func (h *Hooks) OnPostShutdown(handler ...OnPostShutdownHandler)
One or more handlers that receive the shutdown error (if any).
app.Hooks().OnPostShutdown(func(err error) error {
if err != nil {
fmt.Printf("Shutdown error: %v\n", err)
} else {
fmt.Println("Server shut down cleanly")
}
return nil
})
Process Hooks
OnFork
Executed in child processes when using prefork mode.
func (h *Hooks) OnFork(handler ...OnForkHandler)
One or more handlers that receive the worker ID.
app.Hooks().OnFork(func(workerID int) error {
fmt.Printf("Worker %d started\n", workerID)
return nil
})
OnMount
Executed when a sub-application is mounted.
func (h *Hooks) OnMount(handler ...OnMountHandler)
One or more handlers that receive the parent App.
subApp := fiber.New()
subApp.Hooks().OnMount(func(parent *fiber.App) error {
fmt.Printf("Mounted at: %s\n", parent.MountPath())
return nil
})
app.Use("/api", subApp)
OnName, OnRoute, OnGroup, and OnGroupName hooks are mount-aware. When a sub-app with these hooks is mounted, route and group paths include the mount prefix.
Example: Complete Hook Setup
app := fiber.New()
// Route registration hooks
app.Hooks().OnRoute(func(r fiber.Route) error {
fmt.Printf("[Route] %s %s\n", r.Method, r.Path)
return nil
})
app.Hooks().OnName(func(r fiber.Route) error {
fmt.Printf("[Named] %s -> %s\n", r.Name, r.Path)
return nil
})
// Startup hooks
app.Hooks().OnPreStartupMessage(func(sm *fiber.PreStartupMessageData) error {
sm.AddInfo("env", "Environment", os.Getenv("ENV"))
return nil
})
app.Hooks().OnListen(func(data fiber.ListenData) error {
fmt.Printf("Listening on %s:%s\n", data.Host, data.Port)
return nil
})
// Shutdown hooks
app.Hooks().OnPreShutdown(func() error {
fmt.Println("Cleaning up resources...")
return nil
})
app.Hooks().OnPostShutdown(func(err error) error {
fmt.Println("Shutdown complete")
return nil
})
// Mount hooks
subApp := fiber.New()
subApp.Hooks().OnMount(func(parent *fiber.App) error {
fmt.Printf("Sub-app mounted at %s\n", parent.MountPath())
return nil
})
app.Use("/v1", subApp)