-
Notifications
You must be signed in to change notification settings - Fork 3
/
context.go
129 lines (104 loc) · 2.91 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* Tencent is pleased to support the open source community by making Polaris available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package utils
import "context"
// requestID ctx
type requestIDCtx struct{}
// client ip ctx
type clientIPCtx struct{}
// client ip ctx
type structClientIPCtx struct{}
// client address ctx
type clientAddrCtx struct{}
// type ClientPortCtx struct{}
// user agent
type userAgent struct{}
// WithRequestID 增加requestID
func WithRequestID(ctx context.Context, requestID string) context.Context {
return context.WithValue(ctx, requestIDCtx{}, requestID)
}
// ParseRequestID 从ctx中获取requestID
func ParseRequestID(ctx context.Context) string {
if ctx == nil {
return ""
}
id, ok := ctx.Value(requestIDCtx{}).(string)
if !ok {
return ""
}
return id
}
// WithClientIP ctx增加客户端IP
func WithClientIP(ctx context.Context, ip string) context.Context {
return context.WithValue(ctx, clientIPCtx{}, ip)
}
// WithStructClientIP ctx增加客户端IP
func WithStructClientIP(ctx context.Context, ip *IPAddress) context.Context {
return context.WithValue(ctx, structClientIPCtx{}, ip)
}
// ParseClientIP 从ctx中获取客户端IP
func ParseClientIP(ctx context.Context) string {
if ctx == nil {
return ""
}
ip, ok := ctx.Value(clientIPCtx{}).(string)
if !ok {
return ""
}
return ip
}
// ParseStructClientIP ctx增加客户端IP
func ParseStructClientIP(ctx context.Context) *IPAddress {
if ctx == nil {
return &IPAddress{}
}
ip, ok := ctx.Value(structClientIPCtx{}).(*IPAddress)
if !ok {
return &IPAddress{}
}
return ip
}
// WithClientAddr ctx增加客户端连接地址
func WithClientAddr(ctx context.Context, addr string) context.Context {
return context.WithValue(ctx, clientAddrCtx{}, addr)
}
// ParseClientAddr 从ctx中获取客户端连接地址,ip:port形式
func ParseClientAddr(ctx context.Context) string {
if ctx == nil {
return ""
}
addr, ok := ctx.Value(clientAddrCtx{}).(string)
if !ok {
return ""
}
return addr
}
// WithUserAgent user agent
func WithUserAgent(ctx context.Context, agent string) context.Context {
return context.WithValue(ctx, userAgent{}, agent)
}
// ParseUserAgent parse agent
func ParseUserAgent(ctx context.Context) string {
if ctx == nil {
return ""
}
agent, ok := ctx.Value(userAgent{}).(string)
if !ok {
return ""
}
return agent
}