2017-01-26 14:46:44 -05:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
|
|
|
|
|
|
|
type key int
|
|
|
|
|
|
|
|
const (
|
|
|
|
userKey key = iota
|
2018-07-03 15:38:02 -04:00
|
|
|
requestKey
|
2017-01-26 14:46:44 -05:00
|
|
|
)
|
|
|
|
|
2018-04-02 03:17:36 -04:00
|
|
|
// ContextWithUser returns a context combined with a User.
|
2017-01-26 14:46:44 -05:00
|
|
|
func ContextWithUser(ctx context.Context, user *User) context.Context {
|
|
|
|
return context.WithValue(ctx, userKey, user)
|
|
|
|
}
|
|
|
|
|
2018-04-02 03:17:36 -04:00
|
|
|
// UserFromContext extracts a User from the given context, if any.
|
2017-01-26 14:46:44 -05:00
|
|
|
func UserFromContext(ctx context.Context) *User {
|
|
|
|
v := ctx.Value(userKey)
|
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return v.(*User)
|
|
|
|
}
|
2018-07-03 15:38:02 -04:00
|
|
|
|
|
|
|
func ContextWithRequestHeader(ctx context.Context, request *RequestHeader) context.Context {
|
|
|
|
return context.WithValue(ctx, requestKey, request)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RequestHeaderFromContext(ctx context.Context) *RequestHeader {
|
|
|
|
request := ctx.Value(requestKey)
|
|
|
|
if request == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return request.(*RequestHeader)
|
|
|
|
}
|