-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #458 from Fenny/master
Update middleware docs
- Loading branch information
Showing
7 changed files
with
190 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Compress | ||
|
||
Compression middleware for Fiber, it supports `deflate`, `gzip` and `brotli` by default. | ||
|
||
### Example | ||
```go | ||
package main | ||
|
||
import ( | ||
"github.com/gofiber/fiber" | ||
"github.com/gofiber/fiber/middleware" | ||
) | ||
|
||
func main() { | ||
app := fiber.New() | ||
|
||
// Default | ||
app.Use(middleware.Compress()) | ||
|
||
// Custom compression level | ||
app.Use(middleware.Compress(middleware.CompressLevelBestSpeed)) | ||
|
||
// Custom Config | ||
app.Use(middleware.CompressWithConfig(middleware.LoggerConfig{ | ||
Next: func(ctx *fiber.Ctx) bool { | ||
return strings.HasPrefix(ctx.Path(), "/static") | ||
}, | ||
Level: middleware.CompressLevelBestCompression, | ||
})) | ||
|
||
app.Listen(3000) | ||
} | ||
``` | ||
|
||
### Signatures | ||
```go | ||
func Compress(level ...int) fiber.Handler {} | ||
func CompressWithConfig(config CompressConfig) fiber.Handler {} | ||
``` | ||
|
||
### Config | ||
```go | ||
type CompressConfig struct { | ||
// Next defines a function to skip this middleware. | ||
Next func(ctx *fiber.Ctx) bool | ||
// Compression level for brotli, gzip and deflate | ||
Level int | ||
} | ||
``` | ||
### Compression Levels | ||
```go | ||
const ( | ||
CompressLevelDisabled = -1 | ||
CompressLevelDefault = 0 | ||
CompressLevelBestSpeed = 1 | ||
CompressLevelBestCompression = 2 | ||
) | ||
``` | ||
|
||
### Default Config | ||
```go | ||
var CompressConfigDefault = CompressConfig{ | ||
Next: nil, | ||
Level: CompressLevelDefault, | ||
} | ||
``` |
This file was deleted.
Oops, something went wrong.
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# Logger | ||
|
||
HTTP request/response logger for Fiber | ||
|
||
### Example | ||
```go | ||
package main | ||
|
||
import ( | ||
"github.com/gofiber/fiber" | ||
"github.com/gofiber/fiber/middleware" | ||
) | ||
|
||
func main() { | ||
app := fiber.New() | ||
|
||
// Default | ||
app.Use(middleware.Logger()) | ||
|
||
// Custom logging format | ||
app.Use(middleware.Logger("${method} - ${path}")) | ||
|
||
// Custom Config | ||
app.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{ | ||
Next: func(ctx *fiber.Ctx) bool { | ||
return ctx.Path() != "/private" | ||
}, | ||
Format: "${method} - ${path}", | ||
Output: io.Writer, | ||
})) | ||
|
||
app.Listen(3000) | ||
} | ||
``` | ||
|
||
### Signatures | ||
```go | ||
func Logger(format ...string) fiber.Handler {} | ||
func LoggerWithConfig(config LoggerConfig) fiber.Handler {} | ||
``` | ||
|
||
### Config | ||
```go | ||
type LoggerConfig struct { | ||
// Next defines a function to skip this middleware. | ||
Next func(ctx *fiber.Ctx) bool | ||
|
||
// Format defines the logging tags | ||
// | ||
// - time | ||
// - ip | ||
// - ips | ||
// - url | ||
// - host | ||
// - method | ||
// - path | ||
// - protocol | ||
// - route | ||
// - referer | ||
// - ua | ||
// - latency | ||
// - status | ||
// - body | ||
// - error | ||
// - bytesSent | ||
// - bytesReceived | ||
// - header:<key> | ||
// - query:<key> | ||
// - form:<key> | ||
// - cookie:<key> | ||
// | ||
// Optional. Default: ${time} ${method} ${path} - ${ip} - ${status} - ${latency}\n | ||
Format string | ||
|
||
// TimeFormat https://programming.guide/go/format-parse-string-time-date-example.html | ||
// | ||
// Optional. Default: 15:04:05 | ||
TimeFormat string | ||
|
||
// Output is a writter where logs are written | ||
// | ||
// Default: os.Stderr | ||
Output io.Writer | ||
} | ||
``` | ||
### Default Config | ||
```go | ||
var LoggerConfigDefault = LoggerConfig{ | ||
Next: nil, | ||
Format: "${time} ${method} ${path} - ${ip} - ${status} - ${latency}\n", | ||
TimeFormat: "15:04:05", | ||
Output: os.Stderr, | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Recover | ||
|
||
Recover middleware recovers from panics anywhere in the stack chain and handles the control to the centralized [ErrorHandler](https://docs.gofiber.io/error-handling). | ||
|
||
### Example | ||
```go | ||
package main | ||
|
||
import ( | ||
"github.com/gofiber/fiber" | ||
"github.com/gofiber/fiber/middleware" | ||
) | ||
|
||
func main() { | ||
app := fiber.New() | ||
|
||
app.Use(middleware.Recover()) | ||
|
||
app.Get("/", func(c *fiber.Ctx) { | ||
panic("normally this would crash your app") | ||
}) | ||
|
||
app.Listen(3000) | ||
} | ||
``` | ||
|
||
### Signatures | ||
```go | ||
func Recover() fiber.Handler {} | ||
``` |