| package contexts
|
|
|
| import (
|
| "context"
|
| "slices"
|
|
|
| "github.com/looplj/axonhub/internal/ent"
|
| )
|
|
|
|
|
| type ContextKey string
|
|
|
| const (
|
|
|
| containerContextKey ContextKey = "context_container"
|
| )
|
|
|
|
|
| func WithAPIKey(ctx context.Context, apiKey *ent.APIKey) context.Context {
|
| container := getContainer(ctx)
|
| container.APIKey = apiKey
|
|
|
| return withContainer(ctx, container)
|
| }
|
|
|
|
|
| func GetAPIKey(ctx context.Context) (*ent.APIKey, bool) {
|
| container := getContainer(ctx)
|
| return container.APIKey, container.APIKey != nil
|
| }
|
|
|
|
|
| func GetAPIKeyString(ctx context.Context) (string, bool) {
|
| apiKey, ok := GetAPIKey(ctx)
|
| if !ok || apiKey == nil {
|
| return "", false
|
| }
|
|
|
| return apiKey.Key, true
|
| }
|
|
|
|
|
| func WithUser(ctx context.Context, user *ent.User) context.Context {
|
| container := getContainer(ctx)
|
| container.User = user
|
|
|
| return withContainer(ctx, container)
|
| }
|
|
|
|
|
| func GetUser(ctx context.Context) (*ent.User, bool) {
|
| container := getContainer(ctx)
|
| return container.User, container.User != nil
|
| }
|
|
|
|
|
| func WithTraceID(ctx context.Context, traceID string) context.Context {
|
| container := getContainer(ctx)
|
| container.TraceID = &traceID
|
|
|
| return withContainer(ctx, container)
|
| }
|
|
|
|
|
| func GetTraceID(ctx context.Context) (string, bool) {
|
| container := getContainer(ctx)
|
| if container.TraceID != nil {
|
| return *container.TraceID, true
|
| }
|
|
|
| return "", false
|
| }
|
|
|
|
|
| func WithOperationName(ctx context.Context, name string) context.Context {
|
| container := getContainer(ctx)
|
| container.OperationName = &name
|
|
|
| return withContainer(ctx, container)
|
| }
|
|
|
|
|
| func GetOperationName(ctx context.Context) (string, bool) {
|
| container := getContainer(ctx)
|
| if container.OperationName != nil {
|
| return *container.OperationName, true
|
| }
|
|
|
| return "", false
|
| }
|
|
|
|
|
| func WithRequestID(ctx context.Context, requestID string) context.Context {
|
| container := getContainer(ctx)
|
| container.RequestID = &requestID
|
|
|
| return withContainer(ctx, container)
|
| }
|
|
|
|
|
| func GetRequestID(ctx context.Context) (string, bool) {
|
| container := getContainer(ctx)
|
| if container.RequestID != nil {
|
| return *container.RequestID, true
|
| }
|
|
|
| return "", false
|
| }
|
|
|
|
|
| func WithChannelAPIKey(ctx context.Context, apiKey string) context.Context {
|
| container := getContainer(ctx)
|
| container.ChannelAPIKey = &apiKey
|
|
|
| return withContainer(ctx, container)
|
| }
|
|
|
|
|
| func GetChannelAPIKey(ctx context.Context) (string, bool) {
|
| container := getContainer(ctx)
|
| if container.ChannelAPIKey != nil {
|
| return *container.ChannelAPIKey, true
|
| }
|
|
|
| return "", false
|
| }
|
|
|
|
|
| func WithProjectID(ctx context.Context, projectID int) context.Context {
|
| container := getContainer(ctx)
|
| container.ProjectID = &projectID
|
|
|
| return withContainer(ctx, container)
|
| }
|
|
|
|
|
| func GetProjectID(ctx context.Context) (int, bool) {
|
| container := getContainer(ctx)
|
| if container.ProjectID != nil {
|
| return *container.ProjectID, true
|
| }
|
|
|
| return 0, false
|
| }
|
|
|
|
|
|
|
|
|
| func AddError(ctx context.Context, err error) {
|
| if err == nil {
|
| return
|
| }
|
|
|
| container := getContainer(ctx)
|
|
|
| container.mu.Lock()
|
| defer container.mu.Unlock()
|
|
|
| container.Errors = append(container.Errors, err)
|
| }
|
|
|
|
|
|
|
|
|
| func GetErrors(ctx context.Context) []error {
|
| container := getContainer(ctx)
|
|
|
| container.mu.RLock()
|
| defer container.mu.RUnlock()
|
|
|
| return slices.Clone(container.Errors)
|
| }
|
|
|