-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
110 lines (89 loc) · 3.31 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
package teamvite
import "context"
// request context consisting of a player and a flash message.
// contextKey represents an internal key for adding context fields.
// This is considered best practice as it prevents other packages from
// interfering with our context keys.
type contextKey int
// List of context keys.
// These are used to store request-scoped information.
const (
// Stores the current logged in player in the context.
userContextKey = contextKey(iota + 1)
// Stores the "flash" in the context. This is a term used in web development
// for a message that is passed from one request to the next for informational
// purposes. This could be moved into the "http" package as it is only HTTP
// related but both the "http" and "http/html" packages use it so it is
// easier to move it to the root.
flashContextKey
// Stores the template to be rendered in the context.
templateKey
// Store the domain models in the context.
// The website is mostly built around CRUD-lite operations on these domain
// models
playerKey
teamKey
gameKey
)
// NewContextWithUser returns a new context with the given player.
func NewContextWithUser(ctx context.Context, player *Player) context.Context {
return context.WithValue(ctx, userContextKey, player)
}
// playerFromContext returns the current logged in player.
func UserFromContext(ctx context.Context) *Player {
player, _ := ctx.Value(userContextKey).(*Player)
return player
}
// playerIDFromContext is a helper function that returns the ID of the current
// logged in player. Returns zero if no player is logged in.
func UserIDFromContext(ctx context.Context) uint64 {
if player := UserFromContext(ctx); player != nil {
return player.ID
}
return 0
}
// NewContextWithFlash returns a new context with the given flash value.
func NewContextWithFlash(ctx context.Context, v string) context.Context {
return context.WithValue(ctx, flashContextKey, v)
}
// FlashFromContext returns the flash value for the current request.
func FlashFromContext(ctx context.Context) string {
v, _ := ctx.Value(flashContextKey).(string)
return v
}
// Domain model contexts
func NewContextWithPlayer(ctx context.Context, template string, player *Player) context.Context {
ctx = context.WithValue(ctx, playerKey, player)
ctx = context.WithValue(ctx, templateKey, template)
return ctx
}
func NewContextWithTeam(ctx context.Context, template string, team *Team) context.Context {
ctx = context.WithValue(ctx, teamKey, team)
ctx = context.WithValue(ctx, templateKey, template)
return ctx
}
func NewContextWithGame(ctx context.Context, template string, game *Game) context.Context {
ctx = context.WithValue(ctx, gameKey, game)
ctx = context.WithValue(ctx, templateKey, template)
return ctx
}
func NewContextWithDivision(ctx context.Context, template string, division *Division) context.Context {
ctx = context.WithValue(ctx, templateKey, template)
return ctx
}
func TemplateFromContext(ctx context.Context) string {
template, _ := ctx.Value(templateKey).(string)
return template
}
func TeamFromContext(ctx context.Context) *Team {
team, _ := ctx.Value(teamKey).(*Team)
return team
}
func GameFromContext(ctx context.Context) *Game {
game, _ := ctx.Value(gameKey).(*Game)
return game
}
func PlayerFromContext(ctx context.Context) *Player {
player, _ := ctx.Value(playerKey).(*Player)
return player
}