github.com/wfusion/gofusion@v1.1.14/common/infra/watermill/message/router_context.go (about) 1 package message 2 3 import ( 4 "context" 5 ) 6 7 type ctxKey string 8 9 const ( 10 handlerNameKey ctxKey = "handler_name" 11 publisherNameKey ctxKey = "publisher_name" 12 subscriberNameKey ctxKey = "subscriber_name" 13 subscribeTopicKey ctxKey = "subscribe_topic" 14 publishTopicKey ctxKey = "publish_topic" 15 ) 16 17 func valFromCtx(ctx context.Context, key ctxKey) string { 18 val, ok := ctx.Value(key).(string) 19 if !ok { 20 return "" 21 } 22 return val 23 } 24 25 // HandlerNameFromCtx returns the name of the message handler in the router that consumed the message. 26 func HandlerNameFromCtx(ctx context.Context) string { 27 return valFromCtx(ctx, handlerNameKey) 28 } 29 30 // PublisherNameFromCtx returns the name of the message publisher type that published the message in the router. 31 // For example, for Kafka it will be `kafka.Publisher`. 32 func PublisherNameFromCtx(ctx context.Context) string { 33 return valFromCtx(ctx, publisherNameKey) 34 } 35 36 // SubscriberNameFromCtx returns the name of the message subscriber type that subscribed to the message in the router. 37 // For example, for Kafka it will be `kafka.Subscriber`. 38 func SubscriberNameFromCtx(ctx context.Context) string { 39 return valFromCtx(ctx, subscriberNameKey) 40 } 41 42 // SubscribeTopicFromCtx returns the topic from which message was received in the router. 43 func SubscribeTopicFromCtx(ctx context.Context) string { 44 return valFromCtx(ctx, subscribeTopicKey) 45 } 46 47 // PublishTopicFromCtx returns the topic to which message will be published by the router. 48 func PublishTopicFromCtx(ctx context.Context) string { 49 return valFromCtx(ctx, publishTopicKey) 50 }