github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/context/context.go (about)

     1  // Package context provides a context for accessing services
     2  package context
     3  
     4  import (
     5  	"context"
     6  
     7  	"github.com/tickoalcantara12/micro/v3/service/context/metadata"
     8  	"github.com/tickoalcantara12/micro/v3/util/namespace"
     9  )
    10  
    11  var (
    12  	// DefaultContext is a context which can be used to access micro services
    13  	DefaultContext = WithNamespace("micro")
    14  )
    15  
    16  // WithNamespace creates a new context with the given namespace
    17  func WithNamespace(ns string) context.Context {
    18  	return SetNamespace(context.TODO(), ns)
    19  }
    20  
    21  // SetNamespace sets the namespace for a context
    22  func SetNamespace(ctx context.Context, ns string) context.Context {
    23  	return namespace.ContextWithNamespace(ctx, ns)
    24  }
    25  
    26  // SetMetadata sets the metadata within the context
    27  func SetMetadata(ctx context.Context, k, v string) context.Context {
    28  	return metadata.Set(ctx, k, v)
    29  }
    30  
    31  // GetMetadata returns metadata from the context
    32  func GetMetadata(ctx context.Context, k string) (string, bool) {
    33  	return metadata.Get(ctx, k)
    34  }