-
Notifications
You must be signed in to change notification settings - Fork 617
/
mid.go
118 lines (94 loc) · 2.99 KB
/
mid.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
// Package mid provides app level middleware support.
package mid
import (
"context"
"errors"
"github.com/ardanlabs/service/app/api/auth"
"github.com/ardanlabs/service/business/api/transaction"
"github.com/ardanlabs/service/business/domain/homebus"
"github.com/ardanlabs/service/business/domain/productbus"
"github.com/ardanlabs/service/business/domain/userbus"
"github.com/google/uuid"
)
// Encoder represents data that can be encoded.
type Encoder interface {
Encode() ([]byte, error)
}
// Handler represents an api layer handler function that needs to be called.
type Handler func(context.Context) (Encoder, error)
// =============================================================================
type ctxKey int
const (
claimKey ctxKey = iota + 1
userIDKey
userKey
productKey
homeKey
trKey
)
func setClaims(ctx context.Context, claims auth.Claims) context.Context {
return context.WithValue(ctx, claimKey, claims)
}
// GetClaims returns the claims from the context.
func GetClaims(ctx context.Context) auth.Claims {
v, ok := ctx.Value(claimKey).(auth.Claims)
if !ok {
return auth.Claims{}
}
return v
}
func setUserID(ctx context.Context, userID uuid.UUID) context.Context {
return context.WithValue(ctx, userIDKey, userID)
}
// GetUserID returns the user id from the context.
func GetUserID(ctx context.Context) (uuid.UUID, error) {
v, ok := ctx.Value(userIDKey).(uuid.UUID)
if !ok {
return uuid.UUID{}, errors.New("user id not found in context")
}
return v, nil
}
func setUser(ctx context.Context, usr userbus.User) context.Context {
return context.WithValue(ctx, userKey, usr)
}
// GetUser returns the user from the context.
func GetUser(ctx context.Context) (userbus.User, error) {
v, ok := ctx.Value(userKey).(userbus.User)
if !ok {
return userbus.User{}, errors.New("user not found in context")
}
return v, nil
}
func setProduct(ctx context.Context, prd productbus.Product) context.Context {
return context.WithValue(ctx, productKey, prd)
}
// GetProduct returns the product from the context.
func GetProduct(ctx context.Context) (productbus.Product, error) {
v, ok := ctx.Value(productKey).(productbus.Product)
if !ok {
return productbus.Product{}, errors.New("product not found in context")
}
return v, nil
}
func setHome(ctx context.Context, hme homebus.Home) context.Context {
return context.WithValue(ctx, homeKey, hme)
}
// GetHome returns the home from the context.
func GetHome(ctx context.Context) (homebus.Home, error) {
v, ok := ctx.Value(homeKey).(homebus.Home)
if !ok {
return homebus.Home{}, errors.New("home not found in context")
}
return v, nil
}
func setTran(ctx context.Context, tx transaction.CommitRollbacker) context.Context {
return context.WithValue(ctx, trKey, tx)
}
// GetTran retrieves the value that can manage a transaction.
func GetTran(ctx context.Context) (transaction.CommitRollbacker, error) {
v, ok := ctx.Value(trKey).(transaction.CommitRollbacker)
if !ok {
return nil, errors.New("transaction not found in context")
}
return v, nil
}