litemiddleware

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2026 License: BSD-2-Clause Imports: 12 Imported by: 0

README

中间件配置指南

本目录提供了开箱即用的内置中间件组件,所有中间件都支持灵活的配置方式。配置属性使用指针类型,支持可选配置,未配置的属性自动使用默认值。

目录变更说明

v1.0.0 (2026-01-24):中间件目录从 component/middleware 重构为 component/litemiddleware,包名从 middleware 变更为 litemiddleware

配置设计

1. 可选配置(指针类型)

所有配置属性都使用指针类型,这意味着:

  • 可选配置:可以只配置需要修改的属性
  • 自动默认值:未配置的属性自动使用默认值
  • 灵活组合:支持任意属性组合
2. 默认值覆盖机制

每个 NewXxxMiddleware 函数内部都会:

  1. 接收 *XxxConfig(可以为 nil)
  2. 获取默认配置
  3. 用用户配置覆盖默认配置中的对应字段
func NewCorsMiddleware(config *CorsConfig) common.IBaseMiddleware {
    cfg := config
    if cfg == nil {
        cfg = &CorsConfig{}
    }
    
    defaultCfg := DefaultCorsConfig()
    
    // 只覆盖用户配置的字段
    if cfg.Name == nil {
        cfg.Name = defaultCfg.Name
    }
    if cfg.AllowOrigins == nil {
        cfg.AllowOrigins = defaultCfg.AllowOrigins
    }
    // ...
    
    return &corsMiddleware{cfg: cfg}
}

支持的中间件

1. CORS 中间件 (cors_middleware.go)

配置结构:

type CorsConfig struct {
    Name             *string       // 中间件名称
    Order            *int          // 执行顺序
    AllowOrigins     *[]string     // 允许的源
    AllowMethods     *[]string     // 允许的 HTTP 方法
    AllowHeaders     *[]string     // 允许的请求头
    ExposeHeaders    *[]string     // 暴露的响应头
    AllowCredentials *bool         // 是否允许携带凭证
    MaxAge           *time.Duration // 预检请求缓存时间
}

默认配置:

  • AllowOrigins: []string{"*"}
  • AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"}
  • AllowHeaders: []string{"Origin", "Content-Type", "Authorization", ...}
  • AllowCredentials: true
  • MaxAge: 12 * time.Hour

使用示例:

// 使用默认配置(允许所有源)
devCors := litemiddleware.NewCorsMiddleware(nil)

// 只修改允许的源(其他使用默认值)
allowOrigins := []string{"https://example.com"}
prodCors := litemiddleware.NewCorsMiddleware(&litemiddleware.CorsConfig{
    AllowOrigins: &allowOrigins,
})

// 修改多个属性
allowOrigins := []string{"https://example.com", "https://app.example.com"}
allowMethods := []string{"GET", "POST", "PUT"}
allowCredentials := true
customCors := litemiddleware.NewCorsMiddleware(&litemiddleware.CorsConfig{
    AllowOrigins:     &allowOrigins,
    AllowMethods:     &allowMethods,
    AllowCredentials: &allowCredentials,
})

2. RequestLogger 中间件 (request_logger_middleware.go)

配置结构:

type RequestLoggerConfig struct {
    Name            *string  // 中间件名称
    Order           *int     // 执行顺序
    Enable          *bool    // 是否启用请求日志
    LogBody         *bool    // 是否记录请求 Body
    MaxBodySize     *int     // 最大记录 Body 大小(字节)
    SkipPaths       *[]string // 跳过日志记录的路径
    LogHeaders      *[]string // 需要记录的请求头
    SuccessLogLevel *string  // 成功请求日志级别(debug/info)
}

默认配置:

  • Enable: true
  • LogBody: true
  • MaxBodySize: 4096
  • SkipPaths: []string{"/health", "/metrics"}
  • LogHeaders: []string{"User-Agent", "Content-Type"}
  • SuccessLogLevel: "info"

使用示例:

// 使用默认配置
reqLogger := litemiddleware.NewRequestLoggerMiddleware(nil)

// 只禁用 Body 记录
logBody := false
prodLogger := litemiddleware.NewRequestLoggerMiddleware(&litemiddleware.RequestLoggerConfig{
    LogBody: &logBody,
})

// 完全自定义
enable := true
logBody := false
maxBodySize := 2048
skipPaths := []string{"/health", "/metrics", "/ping"}
logHeaders := []string{"User-Agent", "X-Request-ID"}
successLogLevel := "debug"
customLogger := litemiddleware.NewRequestLoggerMiddleware(&litemiddleware.RequestLoggerConfig{
    Enable:          &enable,
    LogBody:         &logBody,
    MaxBodySize:     &maxBodySize,
    SkipPaths:       &skipPaths,
    LogHeaders:      &logHeaders,
    SuccessLogLevel: &successLogLevel,
})

3. SecurityHeaders 中间件 (security_headers_middleware.go)

配置结构:

type SecurityHeadersConfig struct {
    Name                    *string // 中间件名称
    Order                   *int    // 执行顺序
    FrameOptions            *string // X-Frame-Options
    ContentTypeOptions      *string // X-Content-Type-Options
    XSSProtection           *string // X-XSS-Protection
    ReferrerPolicy          *string // Referrer-Policy
    ContentSecurityPolicy   *string // Content-Security-Policy
    StrictTransportSecurity *string // Strict-Transport-Security
}

默认配置:

  • FrameOptions: "DENY"
  • ContentTypeOptions: "nosniff"
  • XSSProtection: "1; mode=block"
  • ReferrerPolicy: "strict-origin-when-cross-origin"

使用示例:

// 使用默认配置
security := litemiddleware.NewSecurityHeadersMiddleware(nil)

// 添加 CSP
csp := "default-src 'self'; script-src 'self'"
customSecurity := litemiddleware.NewSecurityHeadersMiddleware(&litemiddleware.SecurityHeadersConfig{
    ContentSecurityPolicy: &csp,
})

// 完全自定义
frameOptions := "SAMEORIGIN"
contentTypeOptions := "nosniff"
xssProtection := "1; mode=block"
referrerPolicy := "no-referrer"
csp := "default-src 'self'"
hsts := "max-age=31536000; includeSubDomains"
prodSecurity := litemiddleware.NewSecurityHeadersMiddleware(&litemiddleware.SecurityHeadersConfig{
    FrameOptions:            &frameOptions,
    ContentTypeOptions:      &contentTypeOptions,
    XSSProtection:           &xssProtection,
    ReferrerPolicy:          &referrerPolicy,
    ContentSecurityPolicy:   &csp,
    StrictTransportSecurity: &hsts,
})

4. Recovery 中间件 (recovery_middleware.go)

配置结构:

type RecoveryConfig struct {
    Name            *string // 中间件名称
    Order           *int    // 执行顺序
    PrintStack      *bool   // 是否打印堆栈信息
    CustomErrorBody *bool   // 是否使用自定义错误响应
    ErrorMessage    *string // 自定义错误消息
    ErrorCode       *string // 自定义错误代码
}

默认配置:

  • PrintStack: true
  • CustomErrorBody: true
  • ErrorMessage: "内部服务器错误"
  • ErrorCode: "INTERNAL_SERVER_ERROR"

使用示例:

// 使用默认配置(打印堆栈)
recovery := litemiddleware.NewRecoveryMiddleware(nil)

// 生产环境(不打印堆栈)
printStack := false
customErrorBody := true
errorMessage := "系统繁忙,请稍后重试"
errorCode := "SYSTEM_BUSY"
prodRecovery := litemiddleware.NewRecoveryMiddleware(&litemiddleware.RecoveryConfig{
    PrintStack:      &printStack,
    CustomErrorBody: &customErrorBody,
    ErrorMessage:    &errorMessage,
    ErrorCode:       &errorCode,
})

5. RateLimiter 中间件 (rate_limiter_middleware.go)

配置结构:

type RateLimiterConfig struct {
    Name      *string       // 中间件名称
    Order     *int          // 执行顺序
    Limit     *int          // 时间窗口内最大请求数
    Window    *time.Duration // 时间窗口大小
    KeyFunc   KeyFunc       // 自定义key生成函数
    SkipFunc  SkipFunc      // 跳过限流的条件
    KeyPrefix *string       // key前缀
}

默认配置:

  • Limit: 100
  • Window: time.Minute
  • KeyPrefix: "rate_limit"
  • KeyFunc: 按IP生成key

使用示例:

// 使用默认配置(按IP限流)
limiter := litemiddleware.NewRateLimiterMiddleware(nil)

// 自定义限流规则
limit := 200
window := time.Minute
keyPrefix := "api"
customLimiter := litemiddleware.NewRateLimiterMiddleware(&litemiddleware.RateLimiterConfig{
    Limit:     &limit,
    Window:    &window,
    KeyPrefix: &keyPrefix,
    KeyFunc: func(c *gin.Context) string {
        return c.GetHeader("X-User-ID")
    },
    SkipFunc: func(c *gin.Context) bool {
        return c.GetHeader("X-Internal") == "true"
    },
})

6. Telemetry 中间件 (telemetry_middleware.go)

配置结构:

type TelemetryConfig struct {
    Name  *string // 中间件名称
    Order *int    // 执行顺序
}

默认配置:

  • 无需额外配置,通过 DI 注入 TelemetryManager

使用示例:

// 使用默认配置
telemetry := litemiddleware.NewTelemetryMiddleware(nil)

依赖注入

所有中间件的依赖注入使用 inject:"" 标签注入 Manager:

type requestLoggerMiddleware struct {
    LoggerMgr loggermgr.ILoggerManager `inject:""`
    cfg       *RequestLoggerConfig
}

type rateLimiterMiddleware struct {
    LimiterMgr limitermgr.ILimiterManager `inject:""`
    LoggerMgr  loggermgr.ILoggerManager   `inject:""`
    config     *RateLimiterConfig
}

依赖注入会在容器初始化时自动完成。


业务系统使用方式

方式 1:使用默认配置(快速开发)
// internal/middlewares/cors_middleware.go
func NewCorsMiddleware() ICorsMiddleware {
    return litemiddleware.NewCorsMiddleware(nil)
}
方式 2:使用自定义配置(生产环境)
// internal/middlewares/cors_middleware.go
func NewProductionCorsMiddleware() ICorsMiddleware {
    allowOrigins := []string{"https://example.com", "https://www.example.com"}
    allowCredentials := true
    maxAge := 12 * time.Hour
    
    return litemiddleware.NewCorsMiddleware(&litemiddleware.CorsConfig{
        AllowOrigins:     &allowOrigins,
        AllowCredentials: &allowCredentials,
        MaxAge:           &maxAge,
    })
}
方式 3:配置驱动(从配置文件读取)
// internal/middlewares/cors_middleware.go
func NewConfigurableCorsMiddleware(cfg config.CorsConfig) ICorsMiddleware {
    var allowOrigins []string
    if cfg.AllowOrigins != nil {
        allowOrigins = cfg.AllowOrigins
    }
    
    allowCredentials := true
    if cfg.AllowCredentials != nil {
        allowCredentials = *cfg.AllowCredentials
    }
    
    maxAge := 12 * time.Hour
    if cfg.MaxAge != nil {
        maxAge = *cfg.MaxAge
    }
    
    return litemiddleware.NewCorsMiddleware(&litemiddleware.CorsConfig{
        AllowOrigins:     &allowOrigins,
        AllowCredentials: &allowCredentials,
        MaxAge:           &maxAge,
    })
}

中间件执行顺序

预定义的中间件执行顺序(按 Order 值从小到大):

中间件 Order 说明
Recovery 0 panic 恢复(最先执行)
RequestLogger 50 请求日志
CORS 100 跨域处理
SecurityHeaders 150 安全头
RateLimiter 200 限流
Telemetry 250 遥测
Auth 300 认证(预留)

业务自定义中间件建议从 Order 350 开始。


测试

所有中间件都有完整的单元测试,测试包括:

  • 默认配置测试
  • 自定义配置测试
  • 功能正确性测试
  • 边界条件测试

运行测试:

go test ./component/litemiddleware/... -v

文件清单

中间件实现文件
  • cors_middleware.go - CORS 跨域中间件
  • recovery_middleware.go - Panic 恢复中间件
  • request_logger_middleware.go - 请求日志中间件
  • security_headers_middleware.go - 安全头中间件
  • rate_limiter_middleware.go - 限流中间件
  • telemetry_middleware.go - 遥测中间件
测试文件
  • cors_middleware_test.go - CORS 测试
  • rate_limiter_middleware_test.go - RateLimiter 测试
  • security_headers_middleware_test.go - SecurityHeaders 测试
  • telemetry_middleware_test.go - Telemetry 测试
  • example_test.go - 使用示例
配置文件
  • constants.go - 中间件执行顺序常量
  • README.md - 本文档
示例项目
  • samples/messageboard/internal/middlewares/ - 示例项目中间件封装

使用建议

开发环境

使用默认配置,快速开发:

container.RegisterMiddleware(middlewareContainer, litemiddleware.NewCorsMiddleware(nil))
生产环境

自定义配置,增强安全性:

allowOrigins := []string{"https://yourdomain.com"}
allowCredentials := true
container.RegisterMiddleware(middlewareContainer, litemiddleware.NewCorsMiddleware(&litemiddleware.CorsConfig{
    AllowOrigins:     &allowOrigins,
    AllowCredentials: &allowCredentials,
}))
配置文件驱动

建议将中间件配置纳入配置文件管理,支持不同环境使用不同配置:

cors:
  allow_origins:
    - https://example.com
    - https://app.example.com
  allow_credentials: true
  max_age: 12h

request_logger:
  enable: true
  log_body: false
  max_body_size: 2048
  skip_paths:
    - /health
    - /metrics
性能优化
  • 关闭 RequestLogger 的 Body 记录(大文件场景)
  • 关闭 Recovery 的堆栈打印(生产环境)
  • 合理设置 RateLimiter 限制

总结

中间件配置设计特性:

  1. ✅ 所有配置属性都使用指针类型(可选配置)
  2. ✅ 未配置的属性自动使用默认值
  3. ✅ 支持依赖注入机制
  4. ✅ 支持通过配置自定义 Name 和 Order
  5. ✅ CORS 支持灵活的跨域配置
  6. ✅ RequestLogger 支持性能优化配置
  7. ✅ RateLimiter 支持多种限流策略(按 IP/用户/路径等)
  8. ✅ 所有中间件都有完整的单元测试和示例

业务系统可以灵活地根据环境(开发/测试/生产)配置不同的中间件参数,支持任意属性组合。

版本历史

v1.0.0 (2026-01-24)
  • 目录重构component/middlewarecomponent/litemiddleware
  • 包名变更middlewarelitemiddleware
  • 配置增强:所有中间件支持通过配置自定义 Name 和 Order
  • 配置重构:配置属性改为指针类型,支持可选配置
  • 新增功能:RateLimiter 限流中间件
  • 示例完善:添加了完整的使用示例和测试用例

Documentation

Overview

Example (CustomCorsConfig)

示例 2: 自定义 CORS 配置

package main

import (
	"time"

	"github.com/lite-lake/litecore-go/component/litemiddleware"
)

func main() {
	// 自定义 CORS 配置
	allowOrigins := []string{"https://example.com", "https://app.example.com"}
	allowMethods := []string{"GET", "POST", "PUT", "DELETE"}
	allowHeaders := []string{"Content-Type", "Authorization"}
	exposeHeaders := []string{"Content-Length"}
	allowCredentials := true
	maxAge := 8 * time.Hour
	cfg := &litemiddleware.CorsConfig{
		AllowOrigins:     &allowOrigins,
		AllowMethods:     &allowMethods,
		AllowHeaders:     &allowHeaders,
		ExposeHeaders:    &exposeHeaders,
		AllowCredentials: &allowCredentials,
		MaxAge:           &maxAge,
	}
	cors := litemiddleware.NewCorsMiddleware(cfg)

	_ = cors
}
Example (CustomRateLimiterConfig)

示例 6: 自定义限流配置

package main

import (
	"time"

	"github.com/gin-gonic/gin"

	"github.com/lite-lake/litecore-go/component/litemiddleware"
)

func main() {
	// 按自定义配置创建限流中间件
	limit := 100
	window := time.Minute
	keyPrefix := "api"
	cfg := &litemiddleware.RateLimiterConfig{
		Limit:     &limit,
		Window:    &window,
		KeyPrefix: &keyPrefix,
		KeyFunc: func(c *gin.Context) string {
			// 自定义 key 生成逻辑
			// 可以基于请求路径、用户ID、IP等生成唯一key
			return c.ClientIP()
		},
		SkipFunc: func(c *gin.Context) bool {
			// 跳过某些请求的限流检查
			// 例如:内部IP、白名单用户等
			return false
		},
	}
	limiter := litemiddleware.NewRateLimiterMiddleware(cfg)

	_ = limiter
}
Example (CustomRecoveryConfig)

示例 5: 自定义 Recovery 配置

package main

import (
	"github.com/lite-lake/litecore-go/component/litemiddleware"
)

func main() {
	// 自定义 Recovery 配置
	printStack := true
	customErrorBody := true
	errorMessage := "服务器内部错误,请稍后重试"
	errorCode := "SERVER_ERROR"
	cfg := &litemiddleware.RecoveryConfig{
		PrintStack:      &printStack,
		CustomErrorBody: &customErrorBody,
		ErrorMessage:    &errorMessage,
		ErrorCode:       &errorCode,
	}
	recovery := litemiddleware.NewRecoveryMiddleware(cfg)

	_ = recovery
}
Example (CustomRequestLoggerConfig)

示例 3: 自定义请求日志配置

package main

import (
	"github.com/lite-lake/litecore-go/component/litemiddleware"
)

func main() {
	// 自定义请求日志配置
	enable := true
	logBody := true
	maxBodySize := 2048
	skipPaths := []string{"/health", "/metrics", "/ping"}
	logHeaders := []string{"User-Agent", "Content-Type", "X-Request-ID"}
	successLogLevel := "debug"
	cfg := &litemiddleware.RequestLoggerConfig{
		Enable:          &enable,
		LogBody:         &logBody,
		MaxBodySize:     &maxBodySize,
		SkipPaths:       &skipPaths,
		LogHeaders:      &logHeaders,
		SuccessLogLevel: &successLogLevel,
	}
	reqLogger := litemiddleware.NewRequestLoggerMiddleware(cfg)

	_ = reqLogger
}
Example (CustomSecurityHeadersConfig)

示例 4: 自定义安全头配置

package main

import (
	"github.com/lite-lake/litecore-go/component/litemiddleware"
)

func main() {
	// 自定义安全头配置
	frameOptions := "SAMEORIGIN"
	contentTypeOptions := "nosniff"
	xssProtection := "1; mode=block"
	referrerPolicy := "strict-origin-when-cross-origin"
	contentSecurityPolicy := "default-src 'self'; script-src 'self' 'unsafe-inline'"
	strictTransportSecurity := "max-age=31536000; includeSubDomains"
	cfg := &litemiddleware.SecurityHeadersConfig{
		FrameOptions:            &frameOptions,
		ContentTypeOptions:      &contentTypeOptions,
		XSSProtection:           &xssProtection,
		ReferrerPolicy:          &referrerPolicy,
		ContentSecurityPolicy:   &contentSecurityPolicy,
		StrictTransportSecurity: &strictTransportSecurity,
	}
	security := litemiddleware.NewSecurityHeadersMiddleware(cfg)

	_ = security
}
Example (DisableRequestLogger)

示例 9: 关闭请求日志

package main

import (
	"github.com/lite-lake/litecore-go/component/litemiddleware"
)

func main() {
	// 完全禁用请求日志
	enable := false
	cfg := &litemiddleware.RequestLoggerConfig{
		Enable: &enable,
	}
	reqLogger := litemiddleware.NewRequestLoggerMiddleware(cfg)

	_ = reqLogger
}
Example (NewMiddlewareWithDefaults)

示例 1: 使用默认配置创建中间件

package main

import (
	"github.com/lite-lake/litecore-go/component/litemiddleware"
)

func main() {
	// 使用默认配置
	cors := litemiddleware.NewCorsMiddlewareWithDefaults()
	recovery := litemiddleware.NewRecoveryMiddlewareWithDefaults()
	security := litemiddleware.NewSecurityHeadersMiddlewareWithDefaults()
	reqLogger := litemiddleware.NewRequestLoggerMiddlewareWithDefaults()

	_ = cors
	_ = recovery
	_ = security
	_ = reqLogger
}
Example (ProductionCorsConfig)

示例 8: 闭包配置生产环境 CORS

package main

import (
	"time"

	"github.com/lite-lake/litecore-go/component/litemiddleware"
)

func main() {
	// 生产环境 CORS 配置(仅允许特定域名)
	allowOrigins := []string{
		"https://example.com",
		"https://www.example.com",
	}
	allowMethods := []string{
		"GET",
		"POST",
		"PUT",
		"DELETE",
		"OPTIONS",
	}
	allowHeaders := []string{
		"Origin",
		"Content-Type",
		"Authorization",
		"Accept",
	}
	allowCredentials := true
	maxAge := 12 * time.Hour
	cfg := &litemiddleware.CorsConfig{
		AllowOrigins:     &allowOrigins,
		AllowMethods:     &allowMethods,
		AllowHeaders:     &allowHeaders,
		AllowCredentials: &allowCredentials,
		MaxAge:           &maxAge,
	}
	cors := litemiddleware.NewCorsMiddleware(cfg)

	_ = cors
}
Example (RateLimiterConfigurations)

示例 7: 使用配置创建不同类型的限流中间件

package main

import (
	"time"

	"github.com/gin-gonic/gin"

	"github.com/lite-lake/litecore-go/component/litemiddleware"
)

func main() {
	// 按IP限流
	limit1 := 100
	window1 := time.Minute
	keyPrefix1 := "ip"
	byIP := litemiddleware.NewRateLimiterMiddleware(&litemiddleware.RateLimiterConfig{
		Limit:     &limit1,
		Window:    &window1,
		KeyPrefix: &keyPrefix1,
	})
	// 按路径限流
	limit2 := 200
	window2 := time.Minute
	keyPrefix2 := "path"
	byPath := litemiddleware.NewRateLimiterMiddleware(&litemiddleware.RateLimiterConfig{
		Limit:     &limit2,
		Window:    &window2,
		KeyPrefix: &keyPrefix2,
		KeyFunc: func(c *gin.Context) string {
			return c.Request.URL.Path
		},
	})
	// 按请求头限流
	limit3 := 50
	window3 := time.Minute
	keyPrefix3 := "header"
	byHeader := litemiddleware.NewRateLimiterMiddleware(&litemiddleware.RateLimiterConfig{
		Limit:     &limit3,
		Window:    &window3,
		KeyPrefix: &keyPrefix3,
		KeyFunc: func(c *gin.Context) string {
			return c.GetHeader("X-User-ID")
		},
	})
	// 按用户ID限流
	limit4 := 10
	window4 := time.Minute
	keyPrefix4 := "user"
	byUserID := litemiddleware.NewRateLimiterMiddleware(&litemiddleware.RateLimiterConfig{
		Limit:     &limit4,
		Window:    &window4,
		KeyPrefix: &keyPrefix4,
		KeyFunc: func(c *gin.Context) string {
			if userID, exists := c.Get("user_id"); exists {
				if uid, ok := userID.(string); ok {
					return uid
				}
			}
			return c.ClientIP()
		},
	})

	_ = byIP
	_ = byPath
	_ = byHeader
	_ = byUserID
}
Example (RecoveryWithoutStack)

示例 10: 关闭 Recovery 堆栈打印(生产环境可能不需要)

package main

import (
	"github.com/lite-lake/litecore-go/component/litemiddleware"
)

func main() {
	// 不打印堆栈信息(生产环境可能为了性能)
	printStack := false
	customErrorBody := true
	errorMessage := "系统错误"
	errorCode := "SYSTEM_ERROR"
	cfg := &litemiddleware.RecoveryConfig{
		PrintStack:      &printStack,
		CustomErrorBody: &customErrorBody,
		ErrorMessage:    &errorMessage,
		ErrorCode:       &errorCode,
	}
	recovery := litemiddleware.NewRecoveryMiddleware(cfg)

	_ = recovery
}

Index

Examples

Constants

View Source
const (
	OrderRecovery        = 0   // panic 恢复中间件(最先执行)
	OrderRequestLogger   = 50  // 请求日志中间件
	OrderCORS            = 100 // CORS 跨域中间件
	OrderSecurityHeaders = 150 // 安全头中间件
	OrderRateLimiter     = 200 // 限流中间件(认证前执行)
	OrderTelemetry       = 250 // 遥测中间件
	OrderAuth            = 300 // 认证中间件

)
View Source
const (
	// 响应头
	RateLimitLimitHeader     = "X-RateLimit-Limit"
	RateLimitRemainingHeader = "X-RateLimit-Remaining"
	RateLimitResetHeader     = "X-RateLimit-Reset"
)

Variables

This section is empty.

Functions

func NewCorsMiddleware

func NewCorsMiddleware(config *CorsConfig) common.IBaseMiddleware

NewCorsMiddleware 创建 CORS 中间件

func NewCorsMiddlewareWithDefaults

func NewCorsMiddlewareWithDefaults() common.IBaseMiddleware

NewCorsMiddlewareWithDefaults 使用默认配置创建 CORS 中间件

func NewRateLimiterMiddleware

func NewRateLimiterMiddleware(config *RateLimiterConfig) common.IBaseMiddleware

func NewRateLimiterMiddlewareWithDefaults

func NewRateLimiterMiddlewareWithDefaults() common.IBaseMiddleware

NewRateLimiterMiddlewareWithDefaults 使用默认配置创建限流中间件

func NewRecoveryMiddleware

func NewRecoveryMiddleware(config *RecoveryConfig) common.IBaseMiddleware

NewRecoveryMiddleware 创建 panic 恢复中间件

func NewRecoveryMiddlewareWithDefaults

func NewRecoveryMiddlewareWithDefaults() common.IBaseMiddleware

NewRecoveryMiddlewareWithDefaults 使用默认配置创建 panic 恢复中间件

func NewRequestLoggerMiddleware

func NewRequestLoggerMiddleware(config *RequestLoggerConfig) common.IBaseMiddleware

NewRequestLoggerMiddleware 创建请求日志中间件

func NewRequestLoggerMiddlewareWithDefaults

func NewRequestLoggerMiddlewareWithDefaults() common.IBaseMiddleware

NewRequestLoggerMiddlewareWithDefaults 使用默认配置创建请求日志中间件

func NewSecurityHeadersMiddleware

func NewSecurityHeadersMiddleware(config *SecurityHeadersConfig) common.IBaseMiddleware

NewSecurityHeadersMiddleware 创建安全头中间件

func NewSecurityHeadersMiddlewareWithDefaults

func NewSecurityHeadersMiddlewareWithDefaults() common.IBaseMiddleware

NewSecurityHeadersMiddlewareWithDefaults 使用默认配置创建安全头中间件

func NewTelemetryMiddleware

func NewTelemetryMiddleware(config *TelemetryConfig) common.IBaseMiddleware

NewTelemetryMiddleware 创建遥测中间件

func NewTelemetryMiddlewareWithDefaults

func NewTelemetryMiddlewareWithDefaults() common.IBaseMiddleware

NewTelemetryMiddlewareWithDefaults 使用默认配置创建遥测中间件

Types

type CorsConfig

type CorsConfig struct {
	Name             *string        // 中间件名称
	Order            *int           // 执行顺序
	AllowOrigins     *[]string      // 允许的源
	AllowMethods     *[]string      // 允许的 HTTP 方法
	AllowHeaders     *[]string      // 允许的请求头
	ExposeHeaders    *[]string      // 暴露的响应头
	AllowCredentials *bool          // 是否允许携带凭证
	MaxAge           *time.Duration // 预检请求缓存时间
}

CorsConfig CORS 配置

func DefaultCorsConfig

func DefaultCorsConfig() *CorsConfig

DefaultCorsConfig 默认 CORS 配置

type KeyFunc

type KeyFunc func(c *gin.Context) string

type RateLimiterConfig

type RateLimiterConfig struct {
	Name      *string        // 中间件名称
	Order     *int           // 执行顺序
	Limit     *int           // 时间窗口内最大请求数
	Window    *time.Duration // 时间窗口大小
	KeyFunc   KeyFunc        // 自定义key生成函数(可选,默认按IP)
	SkipFunc  SkipFunc       // 跳过限流的条件(可选)
	KeyPrefix *string        // key前缀
}

func DefaultRateLimiterConfig

func DefaultRateLimiterConfig() *RateLimiterConfig

DefaultRateLimiterConfig 默认限流配置

type RecoveryConfig

type RecoveryConfig struct {
	Name            *string // 中间件名称
	Order           *int    // 执行顺序
	PrintStack      *bool   // 是否打印堆栈信息
	CustomErrorBody *bool   // 是否使用自定义错误响应
	ErrorMessage    *string // 自定义错误消息
	ErrorCode       *string // 自定义错误代码
}

RecoveryConfig panic 恢复配置

func DefaultRecoveryConfig

func DefaultRecoveryConfig() *RecoveryConfig

DefaultRecoveryConfig 默认 panic 恢复配置

type RequestLoggerConfig

type RequestLoggerConfig struct {
	Name            *string   // 中间件名称
	Order           *int      // 执行顺序
	Enable          *bool     // 是否启用请求日志
	LogBody         *bool     // 是否记录请求 Body
	MaxBodySize     *int      // 最大记录 Body 大小(字节),0 表示不限制
	SkipPaths       *[]string // 跳过日志记录的路径
	LogHeaders      *[]string // 需要记录的请求头
	SuccessLogLevel *string   // 成功请求日志级别(debug/info)
}

RequestLoggerConfig 请求日志配置

func DefaultRequestLoggerConfig

func DefaultRequestLoggerConfig() *RequestLoggerConfig

DefaultRequestLoggerConfig 默认请求日志配置

type SecurityHeadersConfig

type SecurityHeadersConfig struct {
	Name                    *string // 中间件名称
	Order                   *int    // 执行顺序
	FrameOptions            *string // X-Frame-Options (DENY, SAMEORIGIN, ALLOW-FROM)
	ContentTypeOptions      *string // X-Content-Type-Options (nosniff)
	XSSProtection           *string // X-XSS-Protection (1; mode=block)
	ReferrerPolicy          *string // Referrer-Policy (strict-origin-when-cross-origin, no-referrer, etc)
	ContentSecurityPolicy   *string // Content-Security-Policy
	StrictTransportSecurity *string // Strict-Transport-Security (max-age=31536000; includeSubDomains)
}

SecurityHeadersConfig 安全头配置

func DefaultSecurityHeadersConfig

func DefaultSecurityHeadersConfig() *SecurityHeadersConfig

DefaultSecurityHeadersConfig 默认安全头配置

type SkipFunc

type SkipFunc func(c *gin.Context) bool

type TelemetryConfig

type TelemetryConfig struct {
	Name  string // 中间件名称
	Order *int   // 执行顺序(指针类型用于判断是否设置)
}

TelemetryConfig 遥测配置

func DefaultTelemetryConfig

func DefaultTelemetryConfig() *TelemetryConfig

DefaultTelemetryConfig 默认遥测配置

Jump to

Keyboard shortcuts

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