Skip to content

Commit c03abe9

Browse files
Add docs from gofiber/fiber@e686668
1 parent 85b2ace commit c03abe9

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

docs/core/api/hooks.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Fiber lets you run custom callbacks at specific points in the routing lifecycle.
1414
- [OnGroup](#ongroup)
1515
- [OnGroupName](#ongroupname)
1616
- [OnListen](#onlisten)
17+
- [OnPreStartupMessage/OnPostStartupMessage](#onprestartupmessageonpoststartupmessage)
18+
- [ListenData](#listendata)
1719
- [OnFork](#onfork)
1820
- [OnPreShutdown](#onpreshutdown)
1921
- [OnPostShutdown](#onpostshutdown)
@@ -29,6 +31,8 @@ type OnGroupHandler = func(Group) error
2931
type OnGroupNameHandler = OnGroupHandler
3032
type OnListenHandler = func(ListenData) error
3133
type OnForkHandler = func(int) error
34+
type OnPreStartupMessageHandler = func(*PreStartupMessageData) error
35+
type OnPostStartupMessageHandler = func(*PostStartupMessageData) error
3236
type OnPreShutdownHandler = func() error
3337
type OnPostShutdownHandler = func(error) error
3438
type OnMountHandler = func(*App) error
@@ -168,6 +172,87 @@ func main() {
168172
</TabItem>
169173
</Tabs>
170174

175+
## OnPreStartupMessage/OnPostStartupMessage
176+
177+
Use `OnPreStartupMessage` to tweak the banner before Fiber prints it, and `OnPostStartupMessage` to run logic after the banner is printed (or skipped). You can use some helper functions to customize the banner inside the `OnPreStartupMessage` hook.
178+
179+
```go title="Signatures"
180+
// AddInfo adds an informational entry to the startup message with "INFO" label.
181+
func (sm *PreStartupMessageData) AddInfo(key, title, value string, priority ...int)
182+
183+
// AddWarning adds a warning entry to the startup message with "WARNING" label.
184+
func (sm *PreStartupMessageData) AddWarning(key, title, value string, priority ...int)
185+
186+
// AddError adds an error entry to the startup message with "ERROR" label.
187+
func (sm *PreStartupMessageData) AddError(key, title, value string, priority ...int)
188+
189+
// EntryKeys returns all entry keys currently present in the startup message.
190+
func (sm *PreStartupMessageData) EntryKeys() []string
191+
192+
// ResetEntries removes all existing entries from the startup message.
193+
func (sm *PreStartupMessageData) ResetEntries()
194+
195+
// DeleteEntry removes a specific entry from the startup message by its key.
196+
func (sm *PreStartupMessageData) DeleteEntry(key string)
197+
```
198+
199+
- Assign `sm.BannerHeader` to override the ASCII art banner. Leave it empty to use the default banner provided by Fiber.
200+
- Set `sm.PreventDefault = true` to suppress the built-in banner without affecting other hooks.
201+
- `PostStartupMessageData` reports whether the banner was skipped via the `Disabled`, `IsChild`, and `Prevented` flags.
202+
203+
```go title="Customize the startup message"
204+
package main
205+
206+
import (
207+
"fmt"
208+
"os"
209+
210+
"github.com/gofiber/fiber/v3"
211+
)
212+
213+
func main() {
214+
app := fiber.New()
215+
216+
app.Hooks().OnPreStartupMessage(func(sm *fiber.PreStartupMessageData) error {
217+
sm.BannerHeader = "FOOBER " + sm.Version + "\n-------"
218+
219+
// Optional: you can also remove old entries
220+
// sm.ResetEntries()
221+
222+
sm.AddInfo("git-hash", "Git hash", os.Getenv("GIT_HASH"))
223+
sm.AddInfo("prefork", "Prefork", fmt.Sprintf("%v", sm.Prefork), 15)
224+
return nil
225+
})
226+
227+
app.Hooks().OnPostStartupMessage(func(sm fiber.PostStartupMessageData) error {
228+
if !sm.Disabled && !sm.IsChild && !sm.Prevented {
229+
fmt.Println("startup completed")
230+
}
231+
return nil
232+
})
233+
234+
app.Listen(":5000")
235+
}
236+
```
237+
238+
### ListenData
239+
240+
`ListenData` exposes runtime metadata about the listener:
241+
242+
| Field | Type | Description |
243+
| --- | --- | --- |
244+
| `Host` | `string` | Resolved hostname or IP address. |
245+
| `Port` | `string` | The bound port. |
246+
| `TLS` | `bool` | Indicates whether TLS is enabled. |
247+
| `Version` | `string` | Fiber version reported in the startup banner. |
248+
| `AppName` | `string` | Application name from the configuration. |
249+
| `HandlerCount` | `int` | Total registered handler count. |
250+
| `ProcessCount` | `int` | Number of processes Fiber will use. |
251+
| `PID` | `int` | Current process identifier. |
252+
| `Prefork` | `bool` | Whether prefork is enabled. |
253+
| `ChildPIDs` | `[]int` | Child process identifiers when preforking. |
254+
| `ColorScheme` | [`Colors`](https://github.com/gofiber/fiber/blob/main/color.go) | Active color scheme for the startup message. |
255+
171256
## OnFork
172257

173258
Runs in the child process after a fork.

docs/core/whats_new.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ We have made several changes to the Fiber hooks, including:
224224
- Added new shutdown hooks to provide better control over the shutdown process:
225225
- `OnPreShutdown` - Executes before the server starts shutting down
226226
- `OnPostShutdown` - Executes after the server has shut down, receives any shutdown error
227+
- `OnPreStartupMessage` - Executes before the startup message is printed, allowing customization of the banner and info entries
228+
- `OnPostStartupMessage` - Executes after the startup message is printed, allowing post-startup logic
227229
- Deprecated `OnShutdown` in favor of the new pre/post shutdown hooks
228230
- Improved shutdown hook execution order and reliability
229231
- Added mutex protection for hook registration and execution
@@ -296,6 +298,43 @@ app.Listen("app.sock", fiber.ListenerConfig{
296298
})
297299
```
298300

301+
- Expanded `ListenData` with versioning, handler, process, and PID metadata, plus dedicated startup message hooks for customization. Check out the [Hooks](./api/hooks#startup-message-customization) documentation for further details.
302+
303+
```go title="Customize the startup message"
304+
package main
305+
306+
import (
307+
"fmt"
308+
"os"
309+
310+
"github.com/gofiber/fiber/v3"
311+
)
312+
313+
func main() {
314+
app := fiber.New()
315+
316+
app.Hooks().OnPreStartupMessage(func(sm *fiber.PreStartupMessageData) error {
317+
sm.BannerHeader = "FOOBER " + sm.Version + "\n-------"
318+
319+
// Optional: you can also remove old entries
320+
// sm.ResetEntries()
321+
322+
sm.AddInfo("git-hash", "Git hash", os.Getenv("GIT_HASH"))
323+
sm.AddInfo("prefork", "Prefork", fmt.Sprintf("%v", sm.Prefork), 15)
324+
return nil
325+
})
326+
327+
app.Hooks().OnPostStartupMessage(func(sm fiber.PostStartupMessageData) error {
328+
if !sm.Disabled && !sm.IsChild && !sm.Prevented {
329+
fmt.Println("startup completed")
330+
}
331+
return nil
332+
})
333+
334+
app.Listen(":5000")
335+
}
336+
```
337+
299338
## 🗺 Router
300339

301340
We have slightly adapted our router interface

0 commit comments

Comments
 (0)