22 lines
531 B
Go
22 lines
531 B
Go
package middleware
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gitea.micah.wiki/pandora/starter/pkg/requestid"
|
|
"gitea.micah.wiki/pandora/starter/pkg/uniqueid"
|
|
"github.com/cloudwego/hertz/pkg/app"
|
|
)
|
|
|
|
func RequestIDMiddleware() app.HandlerFunc {
|
|
return func(ctx context.Context, c *app.RequestContext) {
|
|
existingID := c.Request.Header.Get(requestid.KLogIDKey)
|
|
if existingID == "" {
|
|
existingID = uniqueid.GetUniqueID()
|
|
c.Request.Header.Set(requestid.KLogIDKey, existingID)
|
|
}
|
|
ctx = requestid.SaveLogID(ctx, existingID)
|
|
c.Next(ctx)
|
|
}
|
|
}
|