add request id generator and middleware file

This commit is contained in:
MaysWind
2020-10-17 17:47:42 +08:00
parent 187171f75c
commit 0d4493439d
5 changed files with 396 additions and 0 deletions
+27
View File
@@ -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()
}
}