Add readmes

pull/453/head
Fenny 2020-06-07 10:13:40 +02:00
parent 6fd165e05a
commit 15b0630ede
3 changed files with 102 additions and 10 deletions

View File

@ -21,17 +21,41 @@ type (
LoggerConfig struct {
// Next defines a function to skip this middleware.
Next func(ctx *fiber.Ctx) bool
// Format defines the logging format with defined variables
// Optional. Default: "${time} ${method} ${path} - ${ip} - ${status} - ${latency}\n"
// Possible values:
// 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>
// 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
}
@ -104,7 +128,7 @@ func LoggerWithConfig(config LoggerConfig) fiber.Handler {
}
// Return handler
return func(c *fiber.Ctx) {
// Don't execute the middleware if Next returns false
// Don't execute the middleware if Next returns true
if config.Next != nil && config.Next(c) {
c.Next()
return

View File

@ -6,6 +6,20 @@ 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 config
var RecoverConfigDefault = RecoverConfig{
Next: nil,
}
// Recover will recover from panics and calls the ErrorHandler
func Recover() fiber.Handler {
return func(ctx *fiber.Ctx) {

View File

@ -5,16 +5,70 @@ import (
"github.com/gofiber/utils"
)
// Middleware types
type (
// RequestIDConfig defines the config for Logger middleware.
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
}
)
// Default config
var RequestIDConfigDefault = RequestIDConfig{
Next: nil,
Header: fiber.HeaderXRequestID,
Generator: func() string {
return utils.UUID()
},
}
// RequestID adds an UUID indentifier to the request
func RequestID() fiber.Handler {
func RequestID(header ...string) fiber.Handler {
// Create default config
var config = RequestIDConfigDefault
// Set lookup if provided
if len(header) > 0 {
config.Header = header[0]
}
// Return LoggerWithConfig
return RequestIDWithConfig(config)
}
// RequestID adds an UUID indentifier to the request
func RequestIDWithConfig(config RequestIDConfig) fiber.Handler {
// Set default values
if config.Header == "" {
config.Header = RequestIDConfigDefault.Header
}
if config.Generator == nil {
config.Generator = RequestIDConfigDefault.Generator
}
// Return handler
return func(ctx *fiber.Ctx) {
// Don't execute the middleware if Next returns true
if config.Next != nil && config.Next(ctx) {
ctx.Next()
return
}
// Get id from request
rid := ctx.Get(fiber.HeaderXRequestID)
rid := ctx.Get(config.Header)
// Create new UUID if empty
if len(rid) <= 0 {
rid = utils.UUID()
}
// Set new id to response
// Set new id to response header
ctx.Set(fiber.HeaderXRequestID, rid)
// Continue stack
ctx.Next()