41 lines
712 B
Go
41 lines
712 B
Go
package request
|
|
|
|
import "context"
|
|
|
|
type requestInfoKey int
|
|
|
|
const (
|
|
key requestInfoKey = iota
|
|
)
|
|
|
|
type RequestInfo struct {
|
|
UserId int64
|
|
TraceId string
|
|
Permissions map[string]bool
|
|
Role string
|
|
}
|
|
|
|
func WithRequestInfo(ctx context.Context, info RequestInfo) context.Context {
|
|
return context.WithValue(ctx, key, info)
|
|
}
|
|
|
|
func GetRequestInfo(ctx context.Context) (requestInfo RequestInfo, ok bool) {
|
|
requestInfo, ok = ctx.Value(key).(RequestInfo)
|
|
return
|
|
}
|
|
|
|
type reqInfoKeyType = string
|
|
|
|
const (
|
|
reqInfoKey reqInfoKeyType = "request-info"
|
|
)
|
|
|
|
func GetReqInfo(c context.Context) RequestInfo {
|
|
info := c.Value(reqInfoKey)
|
|
if info != nil {
|
|
return info.(RequestInfo)
|
|
}
|
|
|
|
return RequestInfo{}
|
|
}
|