/
ctx.go
54 lines (44 loc) · 1.27 KB
/
ctx.go
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
package api
import (
"context"
"fmt"
gcontext "golang.org/x/net/context"
)
type apiKey int
const (
originTypeKey apiKey = iota
userKey
)
// OriginType indicates where the gRPC request actually came from.
//
// e.g. Is it from REST gateway? Or somewhere else?
type OriginType int
// Known origins
const (
OriginTypeUnknown OriginType = iota
OriginTypeREST
)
// NewOriginTypeContext creates a new ctx from the OriginType.
func NewOriginTypeContext(ctx gcontext.Context, originType OriginType) context.Context {
return context.WithValue(ctx, originTypeKey, originType)
}
// GetOriginTypeFromContext returns the OriginType from context.
func GetOriginTypeFromContext(ctx gcontext.Context) OriginType {
originType, ok := ctx.Value(originTypeKey).(OriginType)
if !ok {
return OriginTypeUnknown
}
return originType
}
// NewUsernameContext creates a new ctx from username and returns it.
func NewUsernameContext(ctx gcontext.Context, username string) context.Context {
return context.WithValue(ctx, userKey, username)
}
// GetUsernameFromContext returns the Username from context.
func GetUsernameFromContext(ctx gcontext.Context) (string, error) {
username, ok := ctx.Value(userKey).(string)
if !ok {
return "", fmt.Errorf("cannot get context value")
}
return username, nil
}