Documentation
¶
Overview ¶
Package middleware API 版本控制中间件
版本策略:
- URL 路径: /api/v1/xxx, /api/v2/xxx
- 请求头: Accept-Version: 1 / 2
- 无版本前缀的旧路由自动映射到最新版本
- 废弃版本响应头: Deprecation + Sunset
使用示例:
router := gin.Default()
router.Use(Versioning(DefaultVersion, V1Handler, V2Handler))
// 注册版本化路由
v1 := VersionedGroup(router, 1)
v1.GET("/users", V1GetUsers)
v2 := VersionedGroup(router, 2)
v2.GET("/users", V2GetUsers)
Index ¶
- Constants
- func AdminAuth() func(c *gin.Context)
- func BodyStorageCleanup() gin.HandlerFunc
- func CORS() gin.HandlerFunc
- func Cache() func(c *gin.Context)
- func ClearSecureVerification(c *gin.Context)
- func CriticalRateLimit() func(c *gin.Context)
- func DecompressRequestMiddleware() gin.HandlerFunc
- func DisableCache() gin.HandlerFunc
- func Distribute() func(c *gin.Context)
- func DownloadRateLimit() func(c *gin.Context)
- func EmailVerificationRateLimit() gin.HandlerFunc
- func GetLanguage(c *gin.Context) string
- func GlobalAPIRateLimit() func(c *gin.Context)
- func GlobalWebRateLimit() func(c *gin.Context)
- func HeaderNavModuleAuth(module string) gin.HandlerFunc
- func HeaderNavModulePublicOrUserAuth(module string) gin.HandlerFunc
- func I18n() gin.HandlerFunc
- func IsVersionDeprecated(version int) bool
- func JimengRequestConvert() func(c *gin.Context)
- func KlingRequestConvert() func(c *gin.Context)
- func ModelRequestRateLimit() func(c *gin.Context)
- func OptionalSecureVerification() gin.HandlerFunc
- func PoweredBy() gin.HandlerFunc
- func RelayPanicRecover() gin.HandlerFunc
- func RequestId() func(c *gin.Context)
- func RootAuth() func(c *gin.Context)
- func RouteTag(tag string) gin.HandlerFunc
- func SearchRateLimit() func(c *gin.Context)
- func SecureVerificationRequired() gin.HandlerFunc
- func SetUpLogger(server *gin.Engine)
- func SetupContextForSelectedChannel(c *gin.Context, channel *model.Channel, modelName string) *types.FastTokenError
- func SetupContextForToken(c *gin.Context, token *model.Token, parts ...string) error
- func StatsMiddleware() gin.HandlerFunc
- func SystemPerformanceCheck() gin.HandlerFunc
- func TokenAuth() func(c *gin.Context)
- func TokenAuthReadOnly() func(c *gin.Context)
- func TokenOrUserAuth() func(c *gin.Context)
- func TryUserAuth() func(c *gin.Context)
- func TurnstileCheck() gin.HandlerFunc
- func UploadRateLimit() func(c *gin.Context)
- func UserAuth() func(c *gin.Context)
- func ValidateTokenForWS(token string) (int, error)
- func VersionedGroup(router gin.IRouter, version int) *gin.RouterGroup
- func Versioning() gin.HandlerFunc
- type HTTPStats
- type ModelRequest
- type ResponseCaptureWriter
- type StatsInfo
- type VersionConfig
- type VersionRouter
Constants ¶
const ( EmailVerificationRateLimitMark = "EV" EmailVerificationMaxRequests = 2 // 30秒内最多2次 EmailVerificationDuration = 30 // 30秒时间窗口 )
const ( ModelRequestRateLimitCountMark = "MRRL" ModelRequestRateLimitSuccessCountMark = "MRRLS" )
const ( // SecureVerificationSessionKey 安全验证的 session key(与 controller 保持一致) SecureVerificationSessionKey = "secure_verified_at" // SecureVerificationTimeout 验证有效期(秒) SecureVerificationTimeout = 300 // 5分钟 )
const ( // DefaultVersion 默认 API 版本 DefaultVersion = 1 // CurrentVersion 当前最新 API 版本 CurrentVersion = 1 // HeaderVersion 请求版本号 HeaderVersion = "Accept-Version" // HeaderDeprecation 废弃提示头 HeaderDeprecation = "Deprecation" // HeaderSunset 废弃截止时间(ISO 8601) HeaderSunset = "Sunset" // HeaderAPIVersion 响应 API 版本头 HeaderAPIVersion = "API-Version" )
const QuotaWarnCooldownDuration = 24 * time.Hour
const RouteTagKey = "route_tag"
Variables ¶
This section is empty.
Functions ¶
func BodyStorageCleanup ¶
func BodyStorageCleanup() gin.HandlerFunc
BodyStorageCleanup 请求体存储清理中间件 在请求处理完成后自动清理磁盘/内存缓存
func CORS ¶
func CORS() gin.HandlerFunc
func ClearSecureVerification ¶
ClearSecureVerification 清除安全验证状态 用于用户登出或需要强制重新验证的场景
func CriticalRateLimit ¶
func DecompressRequestMiddleware ¶
func DecompressRequestMiddleware() gin.HandlerFunc
func DisableCache ¶
func DisableCache() gin.HandlerFunc
func Distribute ¶
func DownloadRateLimit ¶
func EmailVerificationRateLimit ¶
func EmailVerificationRateLimit() gin.HandlerFunc
func GetLanguage ¶
GetLanguage returns the current language from gin context
func GlobalAPIRateLimit ¶
func GlobalWebRateLimit ¶
func HeaderNavModuleAuth ¶
func HeaderNavModuleAuth(module string) gin.HandlerFunc
func HeaderNavModulePublicOrUserAuth ¶
func HeaderNavModulePublicOrUserAuth(module string) gin.HandlerFunc
func I18n ¶
func I18n() gin.HandlerFunc
I18n middleware detects and sets the language preference for the request
func IsVersionDeprecated ¶
IsVersionDeprecated 检查指定版本是否已废弃
func JimengRequestConvert ¶
func KlingRequestConvert ¶
func ModelRequestRateLimit ¶
ModelRequestRateLimit 模型请求限流中间件
func OptionalSecureVerification ¶
func OptionalSecureVerification() gin.HandlerFunc
OptionalSecureVerification 可选的安全验证中间件 如果用户已验证,则在 context 中设置标记,但不阻止请求继续 用于某些需要区分是否已验证的场景
func PoweredBy ¶
func PoweredBy() gin.HandlerFunc
func RelayPanicRecover ¶
func RelayPanicRecover() gin.HandlerFunc
func RouteTag ¶
func RouteTag(tag string) gin.HandlerFunc
func SearchRateLimit ¶
SearchRateLimit returns a per-user rate limiter for search endpoints. Configurable via SEARCH_RATE_LIMIT_ENABLE / SEARCH_RATE_LIMIT / SEARCH_RATE_LIMIT_DURATION.
func SecureVerificationRequired ¶
func SecureVerificationRequired() gin.HandlerFunc
SecureVerificationRequired 安全验证中间件 检查用户是否在有效时间内通过了安全验证 如果未验证或验证已过期,返回 401 错误
func SetUpLogger ¶
func SetupContextForToken ¶
func SystemPerformanceCheck ¶
func SystemPerformanceCheck() gin.HandlerFunc
SystemPerformanceCheck 检查系统性能中间件
func TokenAuthReadOnly ¶
TokenAuthReadOnly 宽松版本的令牌认证中间件,用于只读查询接口。 只验证令牌 key 是否存在,不检查令牌状态、过期时间和额度。 即使令牌已过期、已耗尽或已禁用,也允许访问。 仍然检查用户是否被封禁。
func TokenOrUserAuth ¶
TokenOrUserAuth allows either session-based user auth or API token auth. Used for endpoints that need to be accessible from both the dashboard and API clients.
func TryUserAuth ¶
func TurnstileCheck ¶
func TurnstileCheck() gin.HandlerFunc
func UploadRateLimit ¶
func ValidateTokenForWS ¶
ValidateTokenForWS 验证 WebSocket 连接用的 Token,返回用户 ID 供 controller/websocket.go 的 WebSocketHandler 调用 token 可以是:
- FastToken 用户 Token(sk-xxx 格式,通过 model.ValidateUserToken 验证)
- FastToken Access Token(通过 model.ValidateAccessToken 验证)
func VersionedGroup ¶
func VersionedGroup(router gin.IRouter, version int) *gin.RouterGroup
VersionedGroup 创建版本化的路由组 用法: v1 := VersionedGroup(engine, 1)
func Versioning ¶
func Versioning() gin.HandlerFunc
Versioning 返回版本控制中间件 自动解析 URL 路径中的版本号 (/api/v{N}/xxx) 并设置版本相关的响应头
Types ¶
type HTTPStats ¶
type HTTPStats struct {
// contains filtered or unexported fields
}
HTTPStats 存储HTTP统计信息
type ModelRequest ¶
type ResponseCaptureWriter ¶
type ResponseCaptureWriter struct {
gin.ResponseWriter
Buffer *bytes.Buffer
Truncated bool // true if the response exceeded capture limit
}
ResponseCaptureWriter wraps gin.ResponseWriter to capture the response body for caching purposes. Used by semantic cache to store responses for future hits.
func NewResponseCaptureWriter ¶
func NewResponseCaptureWriter(w gin.ResponseWriter) *ResponseCaptureWriter
func (*ResponseCaptureWriter) Write ¶
func (w *ResponseCaptureWriter) Write(data []byte) (int, error)
func (*ResponseCaptureWriter) WriteString ¶
func (w *ResponseCaptureWriter) WriteString(s string) (int, error)
type StatsInfo ¶
type StatsInfo struct {
ActiveConnections int64 `json:"active_connections"`
}
StatsInfo 统计信息结构
Source Files
¶
- auth.go
- body_cleanup.go
- cache.go
- cors.go
- disable-cache.go
- distributor.go
- email-verification-rate-limit.go
- gzip.go
- header_nav.go
- i18n.go
- jimeng_adapter.go
- kling_adapter.go
- logger.go
- model-rate-limit.go
- performance.go
- rate-limit.go
- recover.go
- request-id.go
- secure_verification.go
- semcache.go
- stats.go
- turnstile-check.go
- utils.go
- versioning.go