github.com/annwntech/go-micro/v2@v2.9.5/namespace/namespace.go (about)

     1  package namespace
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/micro/go-micro/v2/metadata"
     7  )
     8  
     9  const (
    10  	// DefaultNamespace used by the server
    11  	DefaultNamespace = "micro"
    12  	// NamespaceKey is used to set/get the namespace from the context
    13  	NamespaceKey = "Micro-Namespace"
    14  )
    15  
    16  // FromContext gets the namespace from the context
    17  func FromContext(ctx context.Context) string {
    18  	// get the namespace which is set at ingress by micro web / api / proxy etc. The go-micro auth
    19  	// wrapper will ensure the account making the request has the necessary issuer.
    20  	ns, _ := metadata.Get(ctx, NamespaceKey)
    21  	return ns
    22  }
    23  
    24  // ContextWithNamespace sets the namespace in the context
    25  func ContextWithNamespace(ctx context.Context, ns string) context.Context {
    26  	return metadata.Set(ctx, NamespaceKey, ns)
    27  }