78 lines
1.6 KiB
Go
78 lines
1.6 KiB
Go
package req
|
|
|
|
import "context"
|
|
|
|
// UserRuntime 请求级别的运行时信息,随 req.User 一起传递
|
|
type UserRuntime interface {
|
|
// Platform 账号运行平台
|
|
Platform() string
|
|
// SaaS 账号运行租户
|
|
SaaS() string
|
|
// TraceId 请求追踪ID
|
|
TraceId() string
|
|
// Ctx 请求级别的上下文,用于挂载不确定/未来才需要的跨切面数据(如 ResWatcher 级联深度),
|
|
// 不用于传递 platform/saas/traceId 这类已知的核心参数
|
|
Ctx() context.Context
|
|
}
|
|
|
|
type uRuntime struct {
|
|
platform string
|
|
saas string
|
|
traceId string
|
|
ctx context.Context
|
|
}
|
|
|
|
func (this *uRuntime) Platform() string {
|
|
return this.platform
|
|
}
|
|
|
|
func (this *uRuntime) SaaS() string {
|
|
return this.saas
|
|
}
|
|
|
|
func (this *uRuntime) TraceId() string {
|
|
return this.traceId
|
|
}
|
|
|
|
func (this *uRuntime) Ctx() context.Context {
|
|
if this.ctx == nil {
|
|
this.ctx = context.Background()
|
|
}
|
|
return this.ctx
|
|
}
|
|
|
|
type UserRuntimeOption func(option *uRuntime)
|
|
|
|
func UserRuntimePlatform(value string) UserRuntimeOption {
|
|
return func(option *uRuntime) {
|
|
option.platform = value
|
|
}
|
|
}
|
|
|
|
func UserRuntimeSaaS(value string) UserRuntimeOption {
|
|
return func(option *uRuntime) {
|
|
option.saas = value
|
|
}
|
|
}
|
|
|
|
func UserRuntimeTraceId(value string) UserRuntimeOption {
|
|
return func(option *uRuntime) {
|
|
option.traceId = value
|
|
}
|
|
}
|
|
|
|
func UserRuntimeCtx(ctx context.Context) UserRuntimeOption {
|
|
return func(option *uRuntime) {
|
|
option.ctx = ctx
|
|
}
|
|
}
|
|
|
|
// NewUserRuntime 构造一个 UserRuntime
|
|
func NewUserRuntime(opts ...UserRuntimeOption) UserRuntime {
|
|
rt := &uRuntime{}
|
|
for _, opt := range opts {
|
|
opt(rt)
|
|
}
|
|
return rt
|
|
}
|