Skip to content

Commit

Permalink
Merge pull request #458 from Fenny/master
Browse files Browse the repository at this point in the history
Update middleware docs
  • Loading branch information
Fenny authored Jun 8, 2020
2 parents 8bc6044 + 1174b9f commit 89bcc76
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 86 deletions.
66 changes: 66 additions & 0 deletions middleware/compress.md
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,
}
```
38 changes: 0 additions & 38 deletions middleware/helmet.go

This file was deleted.

Empty file removed middleware/helmet.md
Empty file.
34 changes: 0 additions & 34 deletions middleware/helmet_test.go

This file was deleted.

94 changes: 94 additions & 0 deletions middleware/logger.md
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,
}
```
14 changes: 0 additions & 14 deletions middleware/recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,6 @@ import (
"github.com/gofiber/fiber"
)

// Middleware types
type (
// RecoverConfig defines the config for Logger middleware.
RecoverConfig struct {
// Next defines a function to skip this middleware.
Next func(ctx *fiber.Ctx) bool
}
)

// RecoverConfigDefault is the default config
var RecoverConfigDefault = RecoverConfig{
Next: nil,
}

// Recover will recover from panics and calls the ErrorHandler
func Recover() fiber.Handler {
return func(ctx *fiber.Ctx) {
Expand Down
30 changes: 30 additions & 0 deletions middleware/recover.md
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 {}
```

0 comments on commit 89bcc76

Please sign in to comment.