forked from MixinNetwork/ocean.one
/
context.go
104 lines (84 loc) 路 2.73 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
package session
import (
"context"
"net/http"
"github.com/dgrijalva/jwt-go"
"github.com/unrolled/render"
"github.com/usmbest/ocean.one/example/durable"
)
type contextValueKey int
const (
keyRequest contextValueKey = 0
keyDatabase contextValueKey = 1
keyLogger contextValueKey = 2
keyRender contextValueKey = 3
keyLimiter contextValueKey = 4
keyRemoteAddress contextValueKey = 11
keyAuthorizationInfo contextValueKey = 12
keyRequestBody contextValueKey = 13
)
func Logger(ctx context.Context) *durable.Logger {
v, _ := ctx.Value(keyLogger).(*durable.Logger)
return v
}
func Database(ctx context.Context) *durable.Database {
v, _ := ctx.Value(keyDatabase).(*durable.Database)
return v
}
func Render(ctx context.Context) *render.Render {
v, _ := ctx.Value(keyRender).(*render.Render)
return v
}
func Limiter(ctx context.Context) *durable.Limiter {
v, _ := ctx.Value(keyLimiter).(*durable.Limiter)
return v
}
func Request(ctx context.Context) *http.Request {
v, _ := ctx.Value(keyRequest).(*http.Request)
return v
}
func RemoteAddress(ctx context.Context) string {
v, _ := ctx.Value(keyRemoteAddress).(string)
return v
}
func RequestBody(ctx context.Context) string {
v, _ := ctx.Value(keyRequestBody).(string)
return v
}
func AuthorizationInfo(ctx context.Context) jwt.MapClaims {
v, _ := ctx.Value(keyAuthorizationInfo).(jwt.MapClaims)
return v
}
func WithLogger(ctx context.Context, logger *durable.Logger) context.Context {
return context.WithValue(ctx, keyLogger, logger)
}
func WithDatabase(ctx context.Context, database *durable.Database) context.Context {
return context.WithValue(ctx, keyDatabase, database)
}
func WithRender(ctx context.Context, render *render.Render) context.Context {
return context.WithValue(ctx, keyRender, render)
}
func WithLimiter(ctx context.Context, limiter *durable.Limiter) context.Context {
return context.WithValue(ctx, keyLimiter, limiter)
}
func WithRequest(ctx context.Context, r *http.Request) context.Context {
rCopy := new(http.Request)
*rCopy = *r
return context.WithValue(ctx, keyRequest, rCopy)
}
func WithRemoteAddress(ctx context.Context, remoteAddr string) context.Context {
return context.WithValue(ctx, keyRemoteAddress, remoteAddr)
}
func WithRequestBody(ctx context.Context, body string) context.Context {
return context.WithValue(ctx, keyRequestBody, body)
}
func WithAuthorizationInfo(ctx context.Context, tokenString string) context.Context {
var value jwt.MapClaims = make(jwt.MapClaims)
jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
if claims, ok := token.Claims.(jwt.MapClaims); ok {
value = claims
}
return nil, nil
})
return context.WithValue(ctx, keyAuthorizationInfo, value)
}