middleware

package
v3.0.5 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2025 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DetectUserAgentLanguage added in v3.0.1

func DetectUserAgentLanguage() gin.HandlerFunc

DetectUserAgentLanguage 检测客户端语言

通过解析请求头中的`Accept-Language`字段得到

默认为`en`

func LogTrace added in v3.0.3

func LogTrace() gin.HandlerFunc

LogTrace 日志追踪

作用是在请求入口注入必要的内容到上下文,供后续的请求调用链使用,一般用于日志追踪、链路追踪

注入内容:

1.请求id

2.请求到来那一刻的纳秒时间戳

3.包装了请求id的日志组件实例

Example:

requestId, ok := ginContext.Get("requestId").(string)

spanId, ok := ginContext.Get("spanId").(string)

entryAt, ok := ginContext.Get("entryAt").(int64)

logger, ok := ginContext.Get("logger").(*zap.Logger)

func PrintRequestPayload

func PrintRequestPayload() gin.HandlerFunc

PrintRequestPayload 打印请求载荷

func PrometheusExporter

func PrometheusExporter() gin.HandlerFunc

PrometheusExporter 导出Prometheus指标

func RateLimiter added in v3.0.5

func RateLimiter(limiter *Limiter) gin.HandlerFunc

RateLimiter 是一个限流器中间件,用于对HTTP请求进行IP限流,并添加X-Rate-Limit相关的响应头

func SetDiskPath added in v3.0.5

func SetDiskPath(path string)

SetDiskPath 设置磁盘监控路径

func SetSampleInterval added in v3.0.5

func SetSampleInterval(interval string)

SetSampleInterval 设置采样间隔(频率)

func SystemMetricsSample added in v3.0.5

func SystemMetricsSample()

SystemMetricsSample 系统指标采样

采样周期为每分钟一次,覆盖上一次记录值

采集对象:

- cpu,多核 -> 使用率

- 内存 -> 使用数值和使用率

- 硬盘使用率,根路径 -> 使用数值和使用率

- 硬盘io,遍历设备 -> 读写次数、字节数、时间

- 网卡,多网卡 -> 出入站数值

func WithCors

func WithCors(headers map[string]string) gin.HandlerFunc

WithCors 允许浏览器跨域请求

func WithCorsOnlyOptions added in v3.0.3

func WithCorsOnlyOptions(headers map[string]string) gin.HandlerFunc

WithCorsOnlyOptions 允许浏览器跨域请求

仅对options探测请求注入放行headers

Types

type Limiter added in v3.0.5

type Limiter struct {
	// contains filtered or unexported fields
}

Limiter 是一个基于滑动时间窗口和IP限制的限流器

func NewLimiter added in v3.0.5

func NewLimiter(reqs int, window time.Duration) *Limiter

NewLimiter 返回一个新的基于滑动时间窗口和IP限制的Limiter实例

func (*Limiter) Allow added in v3.0.5

func (l *Limiter) Allow(ip string) (bool, int, time.Time)

Allow 方法返回指定IP的请求是否被允许,同时返回剩余的请求数和窗口重置时间

Jump to

Keyboard shortcuts

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