-
Notifications
You must be signed in to change notification settings - Fork 1
/
context.go
91 lines (79 loc) · 2.71 KB
/
context.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Copyright (c) 2019. Octofox.io
*/
package foundation
import (
"context"
"github.com/octofoxio/foundation/logger"
"github.com/rs/xid"
"google.golang.org/grpc/metadata"
)
const (
FoundationAccessTokenContextKey = "accesstoken"
FoundationRequestIDContextKey = "requestid"
FoundationLoggerContextKey = "logger"
FoundationUserIdContextKey = "userid"
FoundationMethodContextKey = "method"
)
func AppendMethodToContext(ctx context.Context, method string, path string) context.Context {
ctx = context.WithValue(ctx, FoundationMethodContextKey, method)
var log = GetLoggerFromContext(ctx)
log = log.WithURL(method, path)
ctx = AppendLoggerToContext(ctx, log)
return ctx
}
func AppendUserIDToContext(ctx context.Context, userID string) context.Context {
ctx = context.WithValue(ctx, FoundationUserIdContextKey, userID)
var log = GetLoggerFromContext(ctx)
log = log.WithUserID(userID)
ctx = AppendLoggerToContext(ctx, log)
return ctx
}
func GetUserIDFromContext(ctx context.Context) string {
if userID, ok := ctx.Value(FoundationUserIdContextKey).(string); ok {
return userID
} else {
return ""
}
}
func AppendLoggerToContext(ctx context.Context, log *logger.Logger) context.Context {
return context.WithValue(ctx, FoundationLoggerContextKey, log)
}
func GetLoggerFromContext(ctx context.Context) *logger.Logger {
if log, ok := ctx.Value(FoundationLoggerContextKey).(*logger.Logger); ok && log != nil {
return log
} else {
log = logger.New("foundation")
log.Warn("You are get logger from context but it empty, make sure you are using foundation.context and append logger before retrieve it")
return log
}
}
func GetAccessTokenFromContext(ctx context.Context) string {
if token, ok := ctx.Value(FoundationAccessTokenContextKey).(string); ok {
return token
} else {
return ""
}
}
func GetRequestIDFromContext(ctx context.Context) string {
if token, ok := ctx.Value(FoundationRequestIDContextKey).(string); ok {
return token
} else {
return ""
}
}
func NewContext(ctx context.Context) context.Context {
if requestID, ok := ctx.Value(FoundationRequestIDContextKey).(string); !ok || requestID == "" {
requestID = xid.New().String()
ctx = context.WithValue(ctx, FoundationRequestIDContextKey, requestID)
}
var log = logger.New("foundation").WithRequestID(GetRequestIDFromContext(ctx))
ctx = AppendLoggerToContext(ctx, log)
return ctx
}
func AppendAuthorizationToContext(ctx context.Context, accessToken string) context.Context {
return metadata.AppendToOutgoingContext(ctx, GRPC_METADATA_AUTHORIZATION_KEY, accessToken)
}
func AppendRequestIDToContext(ctx context.Context, requestID string) context.Context {
return metadata.AppendToOutgoingContext(ctx, GRPC_METADATA_REQUEST_ID_KEY, requestID)
}