Introduces internal/requestid package with shared context key to avoid circular imports between server and middleware packages. Server's RequestID middleware now uses the shared key. Client middleware picks up the ID from context and sets X-Request-Id on outgoing requests. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
20 lines
561 B
Go
20 lines
561 B
Go
// Package requestid provides a shared context key for request IDs,
|
|
// allowing both client and server packages to access request IDs
|
|
// without circular imports.
|
|
package requestid
|
|
|
|
import "context"
|
|
|
|
type key struct{}
|
|
|
|
// NewContext returns a context with the given request ID.
|
|
func NewContext(ctx context.Context, id string) context.Context {
|
|
return context.WithValue(ctx, key{}, id)
|
|
}
|
|
|
|
// FromContext returns the request ID from ctx, or empty string if not set.
|
|
func FromContext(ctx context.Context) string {
|
|
id, _ := ctx.Value(key{}).(string)
|
|
return id
|
|
}
|