Files
tyapi-server/internal/shared/middleware/api_auth.go
2025-07-28 01:46:39 +08:00

51 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package middleware
import (
"tyapi-server/internal/config"
"tyapi-server/internal/shared/interfaces"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
// ApiAuthMiddleware API认证中间件
type ApiAuthMiddleware struct {
config *config.Config
logger *zap.Logger
responseBuilder interfaces.ResponseBuilder
}
// NewApiAuthMiddleware 创建API认证中间件
func NewApiAuthMiddleware(cfg *config.Config, logger *zap.Logger, responseBuilder interfaces.ResponseBuilder) *ApiAuthMiddleware {
return &ApiAuthMiddleware{
config: cfg,
logger: logger,
responseBuilder: responseBuilder,
}
}
// GetName 返回中间件名称
func (m *ApiAuthMiddleware) GetName() string {
return "api_auth"
}
// GetPriority 返回中间件优先级
func (m *ApiAuthMiddleware) GetPriority() int {
return 60 // 中等优先级,在日志之后,业务处理之前
}
// Handle 返回中间件处理函数
func (m *ApiAuthMiddleware) Handle() gin.HandlerFunc {
return func(c *gin.Context) {
// 获取客户端IP地址并存入上下文
clientIP := c.ClientIP()
c.Set("client_ip", clientIP)
c.Next()
}
}
// IsGlobal 是否为全局中间件
func (m *ApiAuthMiddleware) IsGlobal() bool {
return false
}