30 lines
583 B
Go
30 lines
583 B
Go
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 ""
|
|
}
|