init project

This commit is contained in:
admin committed 2025-12-05 17:23:29 +08:00
commit d63cc8d9f5
33 files changed
+1529

No files matched your search

+32
View File
@@ -0,0 +1,32 @@
package jetstream
import (
"context"
"github.com/nats-io/nats.go"
)
func NewContext(msg *nats.Msg) context.Context {
ctx := context.Background()
ctx = context.WithValue(ctx, "req_id", msg.Header.Get("req_id"))
return ctx
}
func NewMsg(ctx context.Context, subject string, data []byte) *nats.Msg {
header := nats.Header{}
header.Set("req_id", getContextValue(ctx, "req_id"))
return &nats.Msg{
Subject: subject,
Data: data,
Header: header,
}
}
func getContextValue(ctx context.Context, keyword string) string {
if ctx.Value(keyword) != nil {
if v, ok := ctx.Value(keyword).(string); ok {
return v
}
}
return ""
}