CoolFace
Apppublic

llzai/axonhub

sourceHugging Faceupdated 8mo agoView on Hugging Face
0likes
source.go35 linesDownload Raw Back to contexts
1package contexts
2
3import (
4	"context"
5
6	"github.com/looplj/axonhub/internal/ent/request"
7)
8
9// WithSource stores the request source in the context.
10func WithSource(ctx context.Context, source request.Source) context.Context {
11	container := getContainer(ctx)
12	container.Source = &source
13
14	return withContainer(ctx, container)
15}
16
17// GetSource retrieves the request source from the context.
18func GetSource(ctx context.Context) (request.Source, bool) {
19	container := getContainer(ctx)
20	if container.Source != nil {
21		return *container.Source, true
22	}
23
24	return request.SourceAPI, false
25}
26
27// GetSourceOrDefault retrieves the request source from the context, or returns the default value if it doesn't exist.
28func GetSourceOrDefault(ctx context.Context, defaultSource request.Source) request.Source {
29	if source, ok := GetSource(ctx); ok {
30		return source
31	}
32
33	return defaultSource
34}
35