-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcontext.go
61 lines (55 loc) · 1.55 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
package base
import (
"context"
"net/http"
"github.com/microsvs/base/pkg/rpc"
"github.com/microsvs/base/pkg/types"
"github.com/segmentio/ksuid"
)
// external http context
func buildContext(r *http.Request) (context.Context, error) {
var (
ctx context.Context = context.Background()
token string
user *types.User
retToken *types.Token
err error
)
ctx = context.WithValue(ctx, rpc.KeyRawRequest, r)
ctx = context.WithValue(ctx, rpc.KeyTraceID, getTraceIdFromRequest(r))
ctx = context.WithValue(ctx, rpc.KeyRPCID, getRPCIdFromRequest(r))
// token
if token = getTokenFromRequest(r); len(token) > 0 {
if retToken, err = rpc.GetUserIdFromTokenRPC(ctx, Service2Url(rpc.FGSToken), token); err != nil {
return nil, err
}
if user, err = rpc.GetUserFromIdRPC(ctx, Service2Url(rpc.FGSUser), retToken.UserId); err != nil {
return nil, err
}
ctx = context.WithValue(ctx, rpc.KeyUser, user)
}
return ctx, nil
}
func getTraceIdFromRequest(r *http.Request) string {
var traceid string
// first: url params from nonce
if traceid = r.URL.Query().Get("nonce"); len(traceid) > 0 {
return traceid
}
// second: X-Trace-ID from header
if traceid = r.Header.Get("X-Trace-ID"); len(traceid) > 0 {
return traceid
}
// finally: generate traceid
return ksuid.New().String()
}
func getRPCIdFromRequest(r *http.Request) string {
var rpcid string
if rpcid = r.Header.Get("X-Trace-RPCID"); len(rpcid) > 0 {
return rpcid
}
return ksuid.New().String()
}
func getTokenFromRequest(r *http.Request) string {
return r.URL.Query().Get("token")
}