init repo
This commit is contained in:
29
pkg/utils/context.go
Normal file
29
pkg/utils/context.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type contextKey string
|
||||
|
||||
const RequestIDKey contextKey = "request_id"
|
||||
const RequestIDHeaderKey = "X-Request-ID"
|
||||
|
||||
func NewContextWithRequestID(ctx context.Context, requestID string) context.Context {
|
||||
newCtx := context.Background()
|
||||
newCtx = context.WithValue(newCtx, RequestIDKey, requestID)
|
||||
return newCtx
|
||||
}
|
||||
|
||||
func NewUUID() string {
|
||||
return uuid.New().String()
|
||||
}
|
||||
|
||||
func GetRequestIDFromContext(ctx context.Context) string {
|
||||
if requestID, ok := ctx.Value(RequestIDKey).(string); ok {
|
||||
return requestID
|
||||
}
|
||||
return ""
|
||||
}
|
||||
Reference in New Issue
Block a user