微信小程序\推拉流

This commit is contained in:
2025-12-15 21:57:21 +08:00
parent 2fca6e5c43
commit 73c2073cf1
14 changed files with 2545 additions and 421 deletions

View File

@@ -5,6 +5,7 @@
package middleware
import (
"log"
"net/http"
"xk-websocket-v2/internal/utils"
@@ -60,6 +61,9 @@ func JWTAuthMiddleware() gin.HandlerFunc {
return
}
// 调试日志打印解析出的用户ID帮助排查JWT问题
log.Printf("🔑 [JWT] 请求: %s %s | 解析出的用户ID: %s", c.Request.Method, c.Request.URL.Path, userID)
// 步骤6: 将解析出的用户ID注入到Context中
// 后续处理器可以通过 c.Get("user_id") 获取当前用户ID
c.Set("user_id", userID)

View File

@@ -40,6 +40,12 @@ var skipResponseBodyRoutes = []string{
"/static/",
}
// 需要完全跳过中间件的路由(如 WebSocket
var skipMiddlewareRoutes = []string{
"/ws",
"/api/call/ws-push",
}
// isBinaryContentType 检测是否为二进制 Content-Type
func isBinaryContentType(contentType string) bool {
contentType = strings.ToLower(contentType)
@@ -113,6 +119,16 @@ func NewRequestLogMiddleware(db *gorm.DB) *RequestLogMiddleware {
return &RequestLogMiddleware{DB: db}
}
// shouldSkipMiddleware 检测是否应该完全跳过中间件
func shouldSkipMiddleware(path string) bool {
for _, route := range skipMiddlewareRoutes {
if strings.HasPrefix(path, route) {
return true
}
}
return false
}
// Handler 中间件处理函数
func (m *RequestLogMiddleware) Handler() gin.HandlerFunc {
return func(c *gin.Context) {
@@ -122,6 +138,12 @@ func (m *RequestLogMiddleware) Handler() gin.HandlerFunc {
return
}
// 跳过 WebSocket 路由(包装 Writer 会干扰 WebSocket 升级)
if shouldSkipMiddleware(c.Request.URL.Path) {
c.Next()
return
}
// 获取请求IP
ip := getClientIP(c)