middleware

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2024 License: MIT Imports: 18 Imported by: 0

README

middleware

├── logger.go
├── powered-by.go
├── rate-limiting.go
└── set-lang.go

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultLoggerConfig is the default Logger middleware config.
	DefaultLoggerConfig = LoggerConfig{
		Skipper:     middleware.DefaultSkipper,
		MinBodySize: 0,
		Timeout:     200 * time.Millisecond,
		Format: `{"time":"${time_rfc3339_nano}","id":"${id}","remote_ip":"${remote_ip}",` +
			`"host":"${host}","method":"${method}","uri":"${uri}","user_agent":"${user_agent}",` +
			`"status":${status},"error":"${error}","latency":${latency},"latency_human":"${latency_human}"` +
			`,"bytes_in":${bytes_in},"bytes_out":${bytes_out},"req_body":${req_body},"res_body":${res_body}}` + "\n",
		CustomTimeFormat: "2006-01-02 15:04:05.00000",
		// contains filtered or unexported fields
	}
)

Functions

func Logger

func Logger() echo.MiddlewareFunc

Logger returns a middleware that logs HTTP requests.

func LoggerWithConfig

func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc

LoggerWithConfig returns a Logger middleware with config. See: `Logger()`.

func PoweredBy

func PoweredBy(config PoweredByConfig) echo.MiddlewareFunc

PoweredBy header add Powered-By

func RateLimiting

func RateLimiting(config *RateLimitingConfig) echo.MiddlewareFunc

RateLimiting is rate limit

func SetLang

func SetLang(config SetLangConfig) echo.MiddlewareFunc

SetLang set language

Types

type LoggerConfig

type LoggerConfig struct {
	// Skipper defines a function to skip middleware.
	Skipper middleware.Skipper

	// Timeout in seconds.
	Timeout time.Duration

	// Min body
	MinBodySize int

	// Tags to construct the logger format.
	//
	// - time_unix
	// - time_unix_nano
	// - time_rfc3339
	// - time_rfc3339_nano
	// - time_custom
	// - id (Request ID)
	// - remote_ip
	// - uri
	// - host
	// - method
	// - path
	// - protocol
	// - referer
	// - user_agent
	// - status
	// - error
	// - latency (In nanoseconds)
	// - latency_human (Human readable)
	// - bytes_in (Bytes received)
	// - bytes_out (Bytes sent)
	// - req_body (Request body)
	// - header:<NAME>
	// - query:<NAME>
	// - form:<NAME>
	//
	// Example "${remote_ip} ${status}"
	//
	// Optional. Default value DefaultLoggerConfig.Format.
	Format string `yaml:"format"`

	// Optional. Default value DefaultLoggerConfig.CustomTimeFormat.
	CustomTimeFormat string `yaml:"custom_time_format"`

	// Output is a writer where logs in JSON format are written.
	// Optional. Default value os.Stdout.
	Output io.Writer
	// contains filtered or unexported fields
}

LoggerConfig defines the config for Logger middleware.

type PoweredByConfig

type PoweredByConfig struct {
	// Skipper defines a function to skip middleware.
	Skipper middleware.Skipper

	Name string `yaml:"name"`

	Version string `yaml:"version"`
}

PoweredByConfig is config

type RateLimitingCache

type RateLimitingCache struct {
	Value   int
	Expired time.Time
}

RateLimitingCache is cache

type RateLimitingConfig

type RateLimitingConfig struct {
	// Skipper defines a function to skip middleware.
	Skipper middleware.Skipper
	// 生成ID
	Generator func(req *http.Request, res *echo.Response, c echo.Context) string
	// 窗口时间,单位:毫秒
	Window time.Duration `yaml:"window"`
	// 最大请求次数
	Limit int `yaml:"limit"`
	// Cache
	Cache map[string]*RateLimitingCache
	// 回调函数
	Callback func(req *http.Request, res *echo.Response, c echo.Context) error
	// lock
	sync.RWMutex
}

RateLimitingConfig is config

type SetLangConfig

type SetLangConfig struct {
	// Skipper defines a function to skip middleware.
	Skipper middleware.Skipper
	// 获取语言参数
	Language func(req *http.Request, res *echo.Response, c echo.Context) string
	// support languages
	Languages []language.Tag
}

SetLangConfig is config

Jump to

Keyboard shortcuts

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