mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-17 00:12:11 +08:00
add request id generator and middleware file
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"github.com/mayswind/lab/pkg/core"
|
||||
"github.com/mayswind/lab/pkg/requestid"
|
||||
"github.com/mayswind/lab/pkg/settings"
|
||||
)
|
||||
|
||||
const REQUEST_ID_HEADER = "X-Request-ID"
|
||||
|
||||
func RequestId(config *settings.Config) core.MiddlewareHandlerFunc {
|
||||
return func (c *core.Context) {
|
||||
if requestid.Container.Current == nil {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
|
||||
requestId := requestid.Container.Current.GenerateRequestId(c.ClientIP())
|
||||
c.SetRequestId(requestId)
|
||||
|
||||
if config.EnableRequestIdHeader {
|
||||
c.Header(REQUEST_ID_HEADER, requestId)
|
||||
}
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user