Skip to content

Commit 8a9167a

Browse files
committed
update docs
1 parent 0429b23 commit 8a9167a

6 files changed

Lines changed: 96 additions & 44 deletions

File tree

docs/api/hooks.md

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,31 @@ func main() {
186186
| `ChildPIDs` | `[]int` | Child process identifiers when preforking. |
187187
| `ColorScheme` | [`Colors`](https://github.com/gofiber/fiber/blob/main/color.go) | Active color scheme for the startup message. |
188188

189-
### Startup message customization
189+
### Startup Message Customization
190190

191-
Use `OnPreStartupMessage` to tweak the banner before Fiber prints it, and `OnPostStartupMessage` to run logic after the banner is printed (or skipped):
191+
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.
192192

193-
- Assign `sm.Header` to override the ASCII art banner. Leave it empty to use the default.
194-
- Provide `sm.PrimaryInfo` and/or `sm.SecondaryInfo` maps to replace the primary (server URL, handler counts, etc.) and secondary (prefork status, PID, process count) sections.
193+
```go title="Signatures"
194+
// AddInfo adds an informational entry to the startup message with "INFO" label.
195+
func (sm *PreStartupMessageData) AddInfo(key, title, value string, priority ...int)
196+
197+
// AddWarning adds a warning entry to the startup message with "WARNING" label.
198+
func (sm *PreStartupMessageData) AddWarning(key, title, value string, priority ...int)
199+
200+
// AddError adds an error entry to the startup message with "ERROR" label.
201+
func (sm *PreStartupMessageData) AddError(key, title, value string, priority ...int)
202+
203+
// EntryKeys returns all entry keys currently present in the startup message.
204+
func (sm *PreStartupMessageData) EntryKeys() []string
205+
206+
// ResetEntries removes all existing entries from the startup message.
207+
func (sm *PreStartupMessageData) ResetEntries()
208+
209+
// DeleteEntry removes a specific entry from the startup message by its key.
210+
func (sm *PreStartupMessageData) DeleteEntry(key string)
211+
```
212+
213+
- Assign `sm.BannerHeader` to override the ASCII art banner. Leave it empty to use the default banner provided by Fiber.
195214
- Set `sm.PreventDefault = true` to suppress the built-in banner without affecting other hooks.
196215
- `PostStartupMessageData` reports whether the banner was skipped via the `Disabled`, `IsChild`, and `Prevented` flags.
197216

@@ -210,8 +229,12 @@ func main() {
210229

211230
app.Hooks().OnPreStartupMessage(func(sm *fiber.PreStartupMessageData) error {
212231
sm.Header = "FOOBER " + sm.Version + "\n-------"
213-
sm.PrimaryInfo = fiber.Map{"Git hash": os.Getenv("GIT_HASH")}
214-
sm.SecondaryInfo = fiber.Map{"Prefork": sm.Prefork}
232+
233+
// Optional: you can also remove old entries
234+
// sm.ResetEntries()
235+
236+
sm.AddInfo("git-hash", "Git hash", os.Getenv("GIT_HASH"))
237+
sm.AddInfo("prefork", "Prefork", fmt.Sprintf("%v", sm.Prefork), 15)
215238
return nil
216239
})
217240

docs/whats_new.md

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -295,27 +295,41 @@ app.Listen("app.sock", fiber.ListenerConfig{
295295
})
296296
```
297297

298-
- Expanded `ListenData` with versioning, handler, process, and PID metadata, plus dedicated startup message hooks for customization.
298+
- 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.
299299

300-
```go
301-
app := fiber.New()
300+
```go title="Customize the startup message"
301+
package main
302302

303-
app.Hooks().OnPreStartupMessage(func(sm *fiber.PreStartupMessageData) error {
304-
sm.Header = "FOOBER " + sm.Version + "\n-------"
305-
sm.PrimaryInfo = fiber.Map{"Git hash": os.Getenv("GIT_HASH")}
306-
sm.SecondaryInfo = fiber.Map{"Process count": sm.ProcessCount}
307-
// Set sm.PreventDefault = true to suppress the default banner entirely.
308-
return nil
309-
})
303+
import (
304+
"fmt"
305+
"os"
310306

311-
app.Hooks().OnPostStartupMessage(func(sm fiber.PostStartupMessageData) error {
312-
if !sm.Disabled && !sm.IsChild && !sm.Prevented {
313-
log.Println("startup completed")
314-
}
315-
return nil
316-
})
307+
"github.com/gofiber/fiber/v3"
308+
)
317309

318-
go app.Listen(":3000")
310+
func main() {
311+
app := fiber.New()
312+
313+
app.Hooks().OnPreStartupMessage(func(sm *fiber.PreStartupMessageData) error {
314+
sm.Header = "FOOBER " + sm.Version + "\n-------"
315+
316+
// Optional: you can also remove old entries
317+
// sm.ResetEntries()
318+
319+
sm.AddInfo("git-hash", "Git hash", os.Getenv("GIT_HASH"))
320+
sm.AddInfo("prefork", "Prefork", fmt.Sprintf("%v", sm.Prefork), 15)
321+
return nil
322+
})
323+
324+
app.Hooks().OnPostStartupMessage(func(sm fiber.PostStartupMessageData) error {
325+
if !sm.Disabled && !sm.IsChild && !sm.Prevented {
326+
fmt.Println("startup completed")
327+
}
328+
return nil
329+
})
330+
331+
app.Listen(":5000")
332+
}
319333
```
320334

321335
## 🗺 Router

hooks.go

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ const (
5959
StartupMessageLevelError
6060
)
6161

62+
const errString = "ERROR"
63+
6264
// startupMessageEntry represents a single line of startup message information.
6365
type startupMessageEntry struct {
6466
key string
@@ -90,13 +92,16 @@ type ListenData struct {
9092
type PreStartupMessageData struct {
9193
*ListenData
9294

93-
Header string
95+
// BannerHeader allows overriding the ASCII art banner displayed at startup.
96+
BannerHeader string
9497

9598
entries []startupMessageEntry
9699

100+
// PreventDefault, when set to true, suppresses the default startup message.
97101
PreventDefault bool
98102
}
99103

104+
// AddInfo adds an informational entry to the startup message with "INFO" label.
100105
func (sm *PreStartupMessageData) AddInfo(key, title, value string, priority ...int) {
101106
pri := -1
102107
if len(priority) > 0 {
@@ -106,6 +111,7 @@ func (sm *PreStartupMessageData) AddInfo(key, title, value string, priority ...i
106111
sm.addEntry(key, title, value, pri, StartupMessageLevelInfo)
107112
}
108113

114+
// AddWarning adds a warning entry to the startup message with "WARNING" label.
109115
func (sm *PreStartupMessageData) AddWarning(key, title, value string, priority ...int) {
110116
pri := -1
111117
if len(priority) > 0 {
@@ -115,6 +121,7 @@ func (sm *PreStartupMessageData) AddWarning(key, title, value string, priority .
115121
sm.addEntry(key, title, value, pri, StartupMessageLevelWarning)
116122
}
117123

124+
// AddError adds an error entry to the startup message with "ERROR" label.
118125
func (sm *PreStartupMessageData) AddError(key, title, value string, priority ...int) {
119126
pri := -1
120127
if len(priority) > 0 {
@@ -124,6 +131,7 @@ func (sm *PreStartupMessageData) AddError(key, title, value string, priority ...
124131
sm.addEntry(key, title, value, pri, StartupMessageLevelError)
125132
}
126133

134+
// EntryKeys returns all entry keys currently present in the startup message.
127135
func (sm *PreStartupMessageData) EntryKeys() []string {
128136
keys := make([]string, 0, len(sm.entries))
129137
for _, entry := range sm.entries {
@@ -132,10 +140,25 @@ func (sm *PreStartupMessageData) EntryKeys() []string {
132140
return keys
133141
}
134142

143+
// ResetEntries removes all existing entries from the startup message.
135144
func (sm *PreStartupMessageData) ResetEntries() {
136145
sm.entries = sm.entries[:0]
137146
}
138147

148+
// DeleteEntry removes a specific entry from the startup message by its key.
149+
func (sm *PreStartupMessageData) DeleteEntry(key string) {
150+
if sm.entries == nil {
151+
return
152+
}
153+
154+
for i, entry := range sm.entries {
155+
if entry.key == key {
156+
sm.entries = append(sm.entries[:i], sm.entries[i+1:]...)
157+
return
158+
}
159+
}
160+
}
161+
139162
func (sm *PreStartupMessageData) addEntry(key, title, value string, priority int, level StartupMessageLevel) {
140163
if sm.entries == nil {
141164
sm.entries = make([]startupMessageEntry, 0)
@@ -155,19 +178,6 @@ func (sm *PreStartupMessageData) addEntry(key, title, value string, priority int
155178
sm.entries = append(sm.entries, startupMessageEntry{key: key, title: title, value: value, level: level, priority: priority})
156179
}
157180

158-
func (sm *PreStartupMessageData) DeleteEntry(key string) {
159-
if sm.entries == nil {
160-
return
161-
}
162-
163-
for i, entry := range sm.entries {
164-
if entry.key == key {
165-
sm.entries = append(sm.entries[:i], sm.entries[i+1:]...)
166-
return
167-
}
168-
}
169-
}
170-
171181
func newPreStartupMessageData(listenData *ListenData) *PreStartupMessageData {
172182
clone := listenData
173183
if len(listenData.ChildPIDs) > 0 {
@@ -181,8 +191,13 @@ func newPreStartupMessageData(listenData *ListenData) *PreStartupMessageData {
181191
type PostStartupMessageData struct {
182192
*ListenData
183193

184-
Disabled bool
185-
IsChild bool
194+
// Disabled indicates whether the startup message was disabled via configuration.
195+
Disabled bool
196+
197+
// IsChild indicates whether the current process is a child in prefork mode.
198+
IsChild bool
199+
200+
// Prevented indicates whether the startup message was suppressed by a pre-startup hook using PreventDefault property.
186201
Prevented bool
187202
}
188203

listen.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,8 @@ func (app *App) startupMessage(listenData *ListenData, cfg *ListenConfig) {
430430
return
431431
}
432432

433-
if preData.Header != "" {
434-
header := preData.Header
433+
if preData.BannerHeader != "" {
434+
header := preData.BannerHeader
435435
fmt.Fprint(out, header)
436436
if !strings.HasSuffix(header, "\n") {
437437
fmt.Fprintln(out)
@@ -481,7 +481,7 @@ func printStartupEntries(out io.Writer, colors *Colors, entries []startupMessage
481481
case StartupMessageLevelWarning:
482482
label, color = "WARN", colors.Yellow
483483
case StartupMessageLevelError:
484-
label, color = "ERROR", colors.Red
484+
label, color = errString, colors.Red
485485
default:
486486
label, color = "INFO", colors.Green
487487
}

listen_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ func Test_StartupMessageCustomization(t *testing.T) {
630630
listenData := app.prepareListenData(":8080", false, &cfg, nil)
631631

632632
app.Hooks().OnPreStartupMessage(func(data *PreStartupMessageData) error {
633-
data.Header = "FOOBER v98\n-------"
633+
data.BannerHeader = "FOOBER v98\n-------"
634634

635635
data.ResetEntries()
636636
data.AddInfo("git_hash", "Git hash", "abc123", 3)

services.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func (app *App) logServices(ctx context.Context, out io.Writer, colors *Colors)
141141
var stateColor string
142142
state, err := srv.State(ctx)
143143
if err != nil {
144-
state = "ERROR"
144+
state = errString
145145
stateColor = scheme.Red
146146
} else {
147147
stateColor = scheme.Blue

0 commit comments

Comments
 (0)