-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.go
More file actions
155 lines (129 loc) · 4.53 KB
/
middleware.go
File metadata and controls
155 lines (129 loc) · 4.53 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package appkit
import (
"context"
)
const (
isDevelCtx contextKey = "isDevel"
ctxPlatformKey contextKey = "platform"
ctxVersionKey contextKey = "version"
ctxMethodKey contextKey = "method"
ctxIPKey contextKey = "ip"
ctxUserAgentKey contextKey = "userAgent"
ctxCountryKey contextKey = "country"
ctxNotificationKey string = "JSONRPC2-Notification"
debugIDCtx contextKey = "debugID"
sqlGroupCtx contextKey = "sqlGroup"
maxUserAgentLength = 2048
maxVersionLength = 64
maxCountryLength = 16
EmptyDebugID = 0
)
// DebugIDFromContext returns debug ID from context.
func DebugIDFromContext(ctx context.Context) uint64 {
if ctx == nil {
return EmptyDebugID
}
if id, ok := ctx.Value(debugIDCtx).(uint64); ok {
return id
}
return EmptyDebugID
}
// NewDebugIDContext creates new context with debug ID.
func NewDebugIDContext(ctx context.Context, debugID uint64) context.Context {
return context.WithValue(ctx, debugIDCtx, debugID)
}
// NewSQLGroupContext creates new context with SQL Group for debug SQL logging.
func NewSQLGroupContext(ctx context.Context, group string) context.Context {
groups, _ := ctx.Value(sqlGroupCtx).(string)
if groups != "" {
groups += ">"
}
groups += group
return context.WithValue(ctx, sqlGroupCtx, groups)
}
// SQLGroupFromContext returns sql group from context.
func SQLGroupFromContext(ctx context.Context) string {
r, _ := ctx.Value(sqlGroupCtx).(string)
return r
}
// NewIPContext creates new context with IP.
func NewIPContext(ctx context.Context, ip string) context.Context {
return context.WithValue(ctx, ctxIPKey, ip)
}
// IPFromContext returns IP from context.
func IPFromContext(ctx context.Context) string {
r, _ := ctx.Value(ctxIPKey).(string)
return r
}
// NewUserAgentContext creates new context with User-Agent.
func NewUserAgentContext(ctx context.Context, ua string) context.Context {
return context.WithValue(ctx, ctxUserAgentKey, cutString(ua, maxUserAgentLength))
}
// UserAgentFromContext returns userAgent from context.
func UserAgentFromContext(ctx context.Context) string {
r, _ := ctx.Value(ctxUserAgentKey).(string)
return r
}
// NewNotificationContext creates new context with JSONRPC2 notification flag.
func NewNotificationContext(ctx context.Context) context.Context {
return context.WithValue(ctx, ctxNotificationKey, true) //nolint:staticcheck
}
// NotificationFromContext returns JSONRPC2 notification flag from context.
func NotificationFromContext(ctx context.Context) bool {
r, _ := ctx.Value(ctxNotificationKey).(bool)
return r
}
// NewIsDevelContext creates new context with isDevel flag.
func NewIsDevelContext(ctx context.Context, isDevel bool) context.Context {
return context.WithValue(ctx, isDevelCtx, isDevel)
}
// IsDevelFromContext returns isDevel flag from context.
func IsDevelFromContext(ctx context.Context) bool {
if isDevel, ok := ctx.Value(isDevelCtx).(bool); ok {
return isDevel
}
return false
}
// NewPlatformContext creates new context with platform.
func NewPlatformContext(ctx context.Context, platform string) context.Context {
return context.WithValue(ctx, ctxPlatformKey, cutString(platform, 64))
}
// PlatformFromContext returns platform from context.
func PlatformFromContext(ctx context.Context) string {
r, _ := ctx.Value(ctxPlatformKey).(string)
return r
}
// NewVersionContext creates new context with version.
func NewVersionContext(ctx context.Context, version string) context.Context {
return context.WithValue(ctx, ctxVersionKey, cutString(version, maxVersionLength))
}
// VersionFromContext returns version from context.
func VersionFromContext(ctx context.Context) string {
r, _ := ctx.Value(ctxVersionKey).(string)
return r
}
// NewCountryContext creates new context with country.
func NewCountryContext(ctx context.Context, country string) context.Context {
return context.WithValue(ctx, ctxCountryKey, cutString(country, maxCountryLength))
}
// CountryFromContext returns country from context.
func CountryFromContext(ctx context.Context) string {
r, _ := ctx.Value(ctxCountryKey).(string)
return r
}
// NewMethodContext creates new context with Method.
func NewMethodContext(ctx context.Context, method string) context.Context {
return context.WithValue(ctx, ctxMethodKey, method)
}
// MethodFromContext returns Method from context.
func MethodFromContext(ctx context.Context) string {
r, _ := ctx.Value(ctxMethodKey).(string)
return r
}
// cutString cuts string with given length.
func cutString(s string, length int) string {
if len(s) > length {
return s[:length]
}
return s
}