middleware

package
v1.14.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 11, 2020 License: MIT Imports: 22 Imported by: 50

README

Fiber Core Middleware

Documentation

Index

Constants

View Source
const (
	CompressLevelDisabled        = -1
	CompressLevelDefault         = 0
	CompressLevelBestSpeed       = 1
	CompressLevelBestCompression = 2
)

Compression levels determine the compression complexity

View Source
const (
	LoggerTagPid           = "pid"
	LoggerTagTime          = "time"
	LoggerTagReferer       = "referer"
	LoggerTagProtocol      = "protocol"
	LoggerTagIP            = "ip"
	LoggerTagIPs           = "ips"
	LoggerTagHost          = "host"
	LoggerTagMethod        = "method"
	LoggerTagPath          = "path"
	LoggerTagURL           = "url"
	LoggerTagUA            = "ua"
	LoggerTagLatency       = "latency"
	LoggerTagStatus        = "status"
	LoggerTagBody          = "body"
	LoggerTagBytesSent     = "bytesSent"
	LoggerTagBytesReceived = "bytesReceived"
	LoggerTagRoute         = "route"
	LoggerTagError         = "error"
	LoggerTagHeader        = "header:"
	LoggerTagQuery         = "query:"
	LoggerTagForm          = "form:"
	LoggerTagCookie        = "cookie:"
	LoggerTagColorBlack    = "black"
	LoggerTagColorRed      = "red"
	LoggerTagColorGreen    = "green"
	LoggerTagColorYellow   = "yellow"
	LoggerTagColorBlue     = "blue"
	LoggerTagColorMagenta  = "magenta"
	LoggerTagColorCyan     = "cyan"
	LoggerTagColorWhite    = "white"
	LoggerTagColorReset    = "resetColor"
)

Logger variables

Variables

View Source
var CompressConfigDefault = CompressConfig{
	Next:  nil,
	Level: CompressLevelDefault,
}

CompressConfigDefault is the default config

View Source
var FileSystemConfigDefault = FileSystemConfig{
	Next:   nil,
	Root:   nil,
	Index:  "/index.html",
	Browse: false,
}

FileSystemConfigDefault is the default config

View Source
var LoggerConfigDefault = LoggerConfig{
	Next:       nil,
	Format:     "#${pid} - ${time} ${status} - ${latency} ${method} ${path}\n",
	TimeFormat: "2006/01/02 15:04:05",
	TimeZone:   "Local",
	Output:     os.Stderr,
}

LoggerConfigDefault is the default config

View Source
var RequestIDConfigDefault = RequestIDConfig{
	Next:   nil,
	Header: fiber.HeaderXRequestID,
	Generator: func() string {
		return utils.UUID()
	},
}

RequestIDConfigDefault is the default config

Functions

func Compress added in v1.11.0

func Compress(options ...interface{}) fiber.Handler

Compress allows the following config arguments in any order:

  • Compress()
  • Compress(next func(*fiber.Ctx) bool)
  • Compress(level int)
  • Compress(config CompressConfig)

func Favicon added in v1.11.0

func Favicon(file ...string) fiber.Handler

Favicon adds an UUID indentifier to the request

func FileSystem added in v1.12.3

func FileSystem(options ...interface{}) fiber.Handler

FileSystem allows the following config arguments in any order:

  • FileSystem(root http.FileSystem)
  • FileSystem(index string)
  • FileSystem(browse bool)
  • FileSystem(config FileSystem)
  • FileSystem(next func(*fiber.Ctx) bool)

func Logger added in v1.3.3

func Logger(options ...interface{}) fiber.Handler

Logger allows the following config arguments in any order:

  • Logger()
  • Logger(next func(*fiber.Ctx) bool)
  • Logger(output io.Writer)
  • Logger(format string)
  • Logger(timeZone string)
  • Logger(timeFormat string)
  • Logger(config LoggerConfig)

func Pprof added in v1.14.1

func Pprof() fiber.Handler

Pprof will enabling profiling

func Recover added in v1.8.2

func Recover() fiber.Handler

Recover will recover from panics and calls the ErrorHandler

func RequestID added in v1.8.1

func RequestID(options ...interface{}) fiber.Handler

RequestID adds an UUID indentifier to the request

RequestID adds an UUID indentifier to the request, the following config arguments in any order:

  • RequestID()
  • RequestID(next func(*fiber.Ctx) bool)
  • RequestID(header string)
  • RequestID(generator func() string)
  • RequestID(config RequestIDConfig)

func Timeout added in v1.12.1

func Timeout(handler fiber.Handler, timeout time.Duration) fiber.Handler

Timeout wraps a handler and aborts the process of the handler if the timeout is reached

Types

type CompressConfig added in v1.11.0

type CompressConfig struct {
	// Next defines a function to skip this middleware if returned true.
	Next func(ctx *fiber.Ctx) bool
	// Compression level for brotli, gzip and deflate
	Level int
}

CompressConfig defines the config for Compress middleware.

type FileSystemConfig added in v1.12.3

type FileSystemConfig struct {
	// Next defines a function to skip this middleware if returned true.
	Next func(ctx *fiber.Ctx) bool

	// Root is a FileSystem that provides access
	// to a collection of files and directories.
	// Required. Default: nil
	Root http.FileSystem

	// Index file for serving a directory.
	// Optional. Default: "index.html"
	Index string

	// Enable directory browsing.
	// Optional. Default: false
	Browse bool
}

FileSystemConfig defines the config for FileSystem middleware.

type LoggerConfig added in v1.8.1

type LoggerConfig struct {
	// Next defines a function to skip this middleware if returned true.
	Next func(*fiber.Ctx) bool

	// Format defines the logging tags
	//
	// - pid
	// - time
	// - ip
	// - ips
	// - url
	// - host
	// - method
	// - methodColored
	// - path
	// - protocol
	// - route
	// - referer
	// - ua
	// - latency
	// - status
	// - statusColored
	// - body
	// - error
	// - bytesSent
	// - bytesReceived
	// - header:<key>
	// - query:<key>
	// - form:<key>
	// - cookie:<key>
	//
	// Optional. Default: #${pid} - ${time} ${status} - ${latency} ${method} ${path}\n
	Format string

	// TimeFormat https://programming.guide/go/format-parse-string-time-date-example.html
	//
	// Optional. Default: 2006/01/02 15:04:05
	TimeFormat string

	// TimeZone can be specified, such as "UTC" and "America/New_York" and "Asia/Chongqing", etc
	//
	// Optional. Default: Local
	TimeZone string

	// Output is a writter where logs are written
	//
	// Default: os.Stderr
	Output io.Writer
	// contains filtered or unexported fields
}

LoggerConfig defines the config for Logger middleware.

type RequestIDConfig added in v1.8.1

type RequestIDConfig struct {
	// Next defines a function to skip this middleware.
	Next func(ctx *fiber.Ctx) bool

	// Header is the header key where to get/set the unique ID
	// Optional. Default: X-Request-ID
	Header string

	// Generator defines a function to generate the unique identifier.
	// Optional. Default: func() string {
	//   return utils.UUID()
	// }
	Generator func() string
}

RequestIDConfig defines the config for Logger middleware.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL