Repository navigation
Expand file tree
/
Copy pathcontext.go
More file actions
70 lines (60 loc) · 1.79 KB
/
Copy pathcontext.go
File metadata and controls
70 lines (60 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package warden
import "context"
type contextKey int
const (
ctxKeyAppID contextKey = iota
ctxKeyTenantID
ctxKeyNamespacePath
ctxKeyRequestIP
)
// WithTenant returns a context with the given app and tenant IDs.
// Use this for standalone mode (without Forge).
func WithTenant(ctx context.Context, appID, tenantID string) context.Context {
ctx = context.WithValue(ctx, ctxKeyAppID, appID)
ctx = context.WithValue(ctx, ctxKeyTenantID, tenantID)
return ctx
}
// WithNamespace returns a context with the given namespace path overlaid on
// top of the existing tenant scope. Pass empty string for the tenant root.
//
// Combine with WithTenant when needed:
//
// ctx := warden.WithTenant(context.Background(), "app1", "t1")
// ctx = warden.WithNamespace(ctx, "engineering/platform")
func WithNamespace(ctx context.Context, namespacePath string) context.Context {
return context.WithValue(ctx, ctxKeyNamespacePath, namespacePath)
}
func appIDFromContext(ctx context.Context) string {
v, ok := ctx.Value(ctxKeyAppID).(string)
if !ok {
return ""
}
return v
}
func tenantIDFromContext(ctx context.Context) string {
v, ok := ctx.Value(ctxKeyTenantID).(string)
if !ok {
return ""
}
return v
}
func namespacePathFromContext(ctx context.Context) string {
v, ok := ctx.Value(ctxKeyNamespacePath).(string)
if !ok {
return ""
}
return v
}
// WithRequestIP returns a context carrying the caller's IP address, used to
// populate CheckLog entries (checklog.Entry.RequestIP). Typically set once
// per inbound request by the HTTP/gRPC middleware layer.
func WithRequestIP(ctx context.Context, ip string) context.Context {
return context.WithValue(ctx, ctxKeyRequestIP, ip)
}
func requestIPFromContext(ctx context.Context) string {
v, ok := ctx.Value(ctxKeyRequestIP).(string)
if !ok {
return ""
}
return v
}