Skip to content

Commit 89bcc76

Browse files
authored
Merge pull request #458 from Fenny/master
Update middleware docs
2 parents 8bc6044 + 1174b9f commit 89bcc76

File tree

7 files changed

+190
-86
lines changed

7 files changed

+190
-86
lines changed

middleware/compress.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Compress
2+
3+
Compression middleware for Fiber, it supports `deflate`, `gzip` and `brotli` by default.
4+
5+
### Example
6+
```go
7+
package main
8+
9+
import (
10+
"github.com/gofiber/fiber"
11+
"github.com/gofiber/fiber/middleware"
12+
)
13+
14+
func main() {
15+
app := fiber.New()
16+
17+
// Default
18+
app.Use(middleware.Compress())
19+
20+
// Custom compression level
21+
app.Use(middleware.Compress(middleware.CompressLevelBestSpeed))
22+
23+
// Custom Config
24+
app.Use(middleware.CompressWithConfig(middleware.LoggerConfig{
25+
Next: func(ctx *fiber.Ctx) bool {
26+
return strings.HasPrefix(ctx.Path(), "/static")
27+
},
28+
Level: middleware.CompressLevelBestCompression,
29+
}))
30+
31+
app.Listen(3000)
32+
}
33+
```
34+
35+
### Signatures
36+
```go
37+
func Compress(level ...int) fiber.Handler {}
38+
func CompressWithConfig(config CompressConfig) fiber.Handler {}
39+
```
40+
41+
### Config
42+
```go
43+
type CompressConfig struct {
44+
// Next defines a function to skip this middleware.
45+
Next func(ctx *fiber.Ctx) bool
46+
// Compression level for brotli, gzip and deflate
47+
Level int
48+
}
49+
```
50+
### Compression Levels
51+
```go
52+
const (
53+
CompressLevelDisabled = -1
54+
CompressLevelDefault = 0
55+
CompressLevelBestSpeed = 1
56+
CompressLevelBestCompression = 2
57+
)
58+
```
59+
60+
### Default Config
61+
```go
62+
var CompressConfigDefault = CompressConfig{
63+
Next: nil,
64+
Level: CompressLevelDefault,
65+
}
66+
```

middleware/helmet.go

Lines changed: 0 additions & 38 deletions
This file was deleted.

middleware/helmet.md

Whitespace-only changes.

middleware/helmet_test.go

Lines changed: 0 additions & 34 deletions
This file was deleted.

middleware/logger.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Logger
2+
3+
HTTP request/response logger for Fiber
4+
5+
### Example
6+
```go
7+
package main
8+
9+
import (
10+
"github.com/gofiber/fiber"
11+
"github.com/gofiber/fiber/middleware"
12+
)
13+
14+
func main() {
15+
app := fiber.New()
16+
17+
// Default
18+
app.Use(middleware.Logger())
19+
20+
// Custom logging format
21+
app.Use(middleware.Logger("${method} - ${path}"))
22+
23+
// Custom Config
24+
app.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
25+
Next: func(ctx *fiber.Ctx) bool {
26+
return ctx.Path() != "/private"
27+
},
28+
Format: "${method} - ${path}",
29+
Output: io.Writer,
30+
}))
31+
32+
app.Listen(3000)
33+
}
34+
```
35+
36+
### Signatures
37+
```go
38+
func Logger(format ...string) fiber.Handler {}
39+
func LoggerWithConfig(config LoggerConfig) fiber.Handler {}
40+
```
41+
42+
### Config
43+
```go
44+
type LoggerConfig struct {
45+
// Next defines a function to skip this middleware.
46+
Next func(ctx *fiber.Ctx) bool
47+
48+
// Format defines the logging tags
49+
//
50+
// - time
51+
// - ip
52+
// - ips
53+
// - url
54+
// - host
55+
// - method
56+
// - path
57+
// - protocol
58+
// - route
59+
// - referer
60+
// - ua
61+
// - latency
62+
// - status
63+
// - body
64+
// - error
65+
// - bytesSent
66+
// - bytesReceived
67+
// - header:<key>
68+
// - query:<key>
69+
// - form:<key>
70+
// - cookie:<key>
71+
//
72+
// Optional. Default: ${time} ${method} ${path} - ${ip} - ${status} - ${latency}\n
73+
Format string
74+
75+
// TimeFormat https://programming.guide/go/format-parse-string-time-date-example.html
76+
//
77+
// Optional. Default: 15:04:05
78+
TimeFormat string
79+
80+
// Output is a writter where logs are written
81+
//
82+
// Default: os.Stderr
83+
Output io.Writer
84+
}
85+
```
86+
### Default Config
87+
```go
88+
var LoggerConfigDefault = LoggerConfig{
89+
Next: nil,
90+
Format: "${time} ${method} ${path} - ${ip} - ${status} - ${latency}\n",
91+
TimeFormat: "15:04:05",
92+
Output: os.Stderr,
93+
}
94+
```

middleware/recover.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,6 @@ import (
66
"github.com/gofiber/fiber"
77
)
88

9-
// Middleware types
10-
type (
11-
// RecoverConfig defines the config for Logger middleware.
12-
RecoverConfig struct {
13-
// Next defines a function to skip this middleware.
14-
Next func(ctx *fiber.Ctx) bool
15-
}
16-
)
17-
18-
// RecoverConfigDefault is the default config
19-
var RecoverConfigDefault = RecoverConfig{
20-
Next: nil,
21-
}
22-
239
// Recover will recover from panics and calls the ErrorHandler
2410
func Recover() fiber.Handler {
2511
return func(ctx *fiber.Ctx) {

middleware/recover.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Recover
2+
3+
Recover middleware recovers from panics anywhere in the stack chain and handles the control to the centralized [ErrorHandler](https://docs.gofiber.io/error-handling).
4+
5+
### Example
6+
```go
7+
package main
8+
9+
import (
10+
"github.com/gofiber/fiber"
11+
"github.com/gofiber/fiber/middleware"
12+
)
13+
14+
func main() {
15+
app := fiber.New()
16+
17+
app.Use(middleware.Recover())
18+
19+
app.Get("/", func(c *fiber.Ctx) {
20+
panic("normally this would crash your app")
21+
})
22+
23+
app.Listen(3000)
24+
}
25+
```
26+
27+
### Signatures
28+
```go
29+
func Recover() fiber.Handler {}
30+
```

0 commit comments

Comments
 (0)