github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/model/context.go (about)

     1  package model
     2  
     3  import "context"
     4  
     5  var (
     6  	ctxUserKey   struct{}
     7  	ctxAPIKeyKey struct{}
     8  )
     9  
    10  func WithUser(ctx context.Context, user User) context.Context {
    11  	return context.WithValue(ctx, ctxUserKey, user)
    12  }
    13  
    14  func UserFromContext(ctx context.Context) (User, bool) {
    15  	user, ok := ctx.Value(ctxUserKey).(User)
    16  	return user, ok
    17  }
    18  
    19  func WithAPIKey(ctx context.Context, key APIKey) context.Context {
    20  	return context.WithValue(ctx, ctxAPIKeyKey, key)
    21  }
    22  
    23  func APIKeyFromContext(ctx context.Context) (APIKey, bool) {
    24  	key, ok := ctx.Value(ctxAPIKeyKey).(APIKey)
    25  	return key, ok
    26  }