- 新增用户与上传错误码文件及 Recovery 中间件 - 修复 router.go 导入与中间件链,注册用户路由并补 handler 错误码导入 - 补充用户状态常量与 UserGetByWechatOpenID 查询,修订刷新令牌 dto - 移除 upload 包 OTel 调用,新增 FileUploadCos 上传封装 - 实现 UserService 微信登录/刷新令牌/我的信息/改资料/上传头像 - 裁剪 main.go 配置段并移除 OTel 桥接,补启动与优雅退出 - 新增 configs 示例配置、global 配置结构及 go.mod 依赖
39 lines
893 B
Go
39 lines
893 B
Go
package middleware
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
const requestIDKey ctxKey = "requestID"
|
|
|
|
// RequestID 从请求头取 X-Request-ID,没有则生成 UUID。
|
|
// 注入 context 并写入响应头,便于全链路追踪。
|
|
func RequestID() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
rid := c.GetHeader("X-Request-ID")
|
|
if rid == "" {
|
|
rid = uuid.New().String()
|
|
}
|
|
|
|
// 写入 gin context(供 handler 取用)
|
|
c.Set("requestID", rid)
|
|
// 写入 request context(供 service/dao 的 slog 取用)
|
|
ctx := context.WithValue(c.Request.Context(), requestIDKey, rid)
|
|
c.Request = c.Request.WithContext(ctx)
|
|
// 响应头
|
|
c.Header("X-Request-ID", rid)
|
|
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
// GetRequestID 从 context 取出 request-id。
|
|
func GetRequestID(ctx context.Context) string {
|
|
rid, _ := ctx.Value(requestIDKey).(string)
|
|
return rid
|
|
}
|