From 5d2bf9a510fe6b1d5fa96807850074960a9ef4bb Mon Sep 17 00:00:00 2001 From: gazebo Date: Sun, 21 Oct 2018 19:09:11 +0800 Subject: [PATCH] - jxcontext.Context added --- business/jxutils/jxcontext/jxcontext.go | 78 +++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 business/jxutils/jxcontext/jxcontext.go diff --git a/business/jxutils/jxcontext/jxcontext.go b/business/jxutils/jxcontext/jxcontext.go new file mode 100644 index 000000000..dc26a7243 --- /dev/null +++ b/business/jxutils/jxcontext/jxcontext.go @@ -0,0 +1,78 @@ +package jxcontext + +import ( + "net/http" + "sync" + + "git.rosy.net.cn/jx-callback/business/jxutils/tasksch" +) + +type Context struct { + rootTask tasksch.ITask + token string + w http.ResponseWriter + r *http.Request + mapData map[interface{}]interface{} + locker sync.RWMutex +} + +func New(rootTask tasksch.ITask, token string, w http.ResponseWriter, r *http.Request) (ctx *Context, errCode string, err error) { + return &Context{ + rootTask: rootTask, + token: token, + w: w, + r: r, + mapData: make(map[interface{}]interface{}), + }, "", nil +} + +func (ctx *Context) GetUserName() string { + return ctx.token +} + +func (ctx *Context) GetUserID() string { + return ctx.token +} + +func (ctx *Context) GetRootTask() tasksch.ITask { + ctx.locker.RLock() + defer ctx.locker.RUnlock() + + return ctx.rootTask +} + +func (ctx *Context) TaskCreated(task tasksch.ITask) bool { + ctx.locker.Lock() + + if ctx.rootTask == nil { + ctx.rootTask = task + ctx.locker.Unlock() + return true + } + ctx.locker.Unlock() + + ctx.rootTask.AddChild(task) + return false +} + +func (ctx *Context) GetResponseWriter() http.ResponseWriter { + return ctx.w +} + +func (ctx *Context) GetRequest() *http.Request { + return ctx.r +} + +func (ctx *Context) SetValue(key, value interface{}) { + ctx.locker.Lock() + defer ctx.locker.Unlock() + + ctx.mapData[key] = value +} + +func (ctx *Context) GetValue(key interface{}) interface{} { + ctx.locker.RLock() + defer ctx.locker.RUnlock() + + return ctx.mapData[key] +}