1 Star 0 Fork 0

白菜林 / go-pkg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
requestid.go 610 Bytes
一键复制 编辑 原始数据 按行查看 历史
白菜林 提交于 2021-10-15 18:03 . add router middleware
package middleware
import (
"github.com/gin-gonic/gin"
"github.com/google/uuid"
// "github.com/satori/go.uuid"
)
func RequestId() gin.HandlerFunc {
return func(c *gin.Context) {
//check for incomint header,use it if exists
requestId := c.Request.Header.Get("X-Request-Id")
//Create request id with UUID4
if requestId == "" {
// u4, _ := uuid.NewV4()
// requestId = u4.String()
requestId = uuid.New().String()
}
//Expose it for use in the application
c.Set("X-Request-Id", requestId)
//Set X-Request-Id header
c.Writer.Header().Set("X-Request-Id", requestId)
c.Next()
}
}
Go
1
https://gitee.com/lyhuilin/pkg.git
git@gitee.com:lyhuilin/pkg.git
lyhuilin
pkg
go-pkg
v0.0.1

搜索帮助

53164aa7 5694891 3bd8fe86 5694891